mysql常用命令

以下内容是本人根据方立勋老师的课件总结的,向方老师致敬!

1.创建一个使用utf-8字符集,并带校对规则的mydb3数据库。

create database mydb3 character set utf8 collate utf8_general_ci;

2.查看前面创建的mydb2数据库的定义信息

show create database mydb2;

3.删除前面创建的mydb1数据库

drop database mydb1;

4.修改数据库的字符集为gb2312

alter database mydb2 character set gb2312;

5.备份mydb3库中的数据,并恢复

进入到命令行窗口
5.1. 备份:

mysqldump -uroot -proot mydb3>c:\1.sql

5.2. 恢复:

5.2.1. 恢复mydb3库方式1

create database mydb3;
source c:\1.sql

5.2.2. 恢复mydb3库方式2

create database mydb3;
mysql -uroot -proot mydb3<c:\1.sql (window命令)

6. 创建一个表

use mydb3;
create table employee
(
id int,
name varchar(20),
gender char(1),
birthday date,
entry_date date,
job varchar(40),
salary decimal(8,2),
resume text
)character set utf8;

7. 在上面员工表的基本上增加一个image列。

alter table employee add image blob;
show create table employee; (显示表的创建语句)
desc employee; (显示表结构)

8. 修改job列,使其长度为60。

alter table employee modify job varchar(60);

9. 删除sex列。

alter table employee drop gender;

10. 表名改为user。

rename table employee to user;

11. 修改表的字符集为utf-8

alter table user character set utf8;

12. 列名name修改为username

alter table user change column name username varchar(40);

13. 使用insert语句向表中插入三个员工的信息。

create table employee(
id int,
name varchar(20),
sex bit(1),
birthday date,
salary decimal(8,2),
entry_date date,
resume text
);

insert into employee(id,name,sex,birthday,salary,entry_date,resume) values(1,'aaa',1,'1980-09-09',1000,'1980-09-09','aaaaaaaa');
select * from employee;
insert into employee(id,name,sex,birthday,salary,entry_date,resume) values('2','bbb',1,'1980-09-09','1000','1980-09-09','aaaaaaaa');
insert into employee values('3','ccc',1,'1980-09-09','1000','1980-09-09','aaaaaaaa');

insert into employee(id,name) values(4,'张三');

14. 若想成功插入中文:

14.1. 查看mysql涉及到编码的所有变量

show variables like 'chara%';

14.2. 改变client的字符集

set character_set_client=gb2312;

14.3. 成功插入中文

insert into employee(id,name) values(4,'张三');

14.4 查看若想不乱码

show variables like 'chara%';
set character_set_client=gb2312;
set character_set_results=gb2312;
select * from employee;

15. 将所有员工薪水修改为5000元。

update employee set salary=5000;

16. 将姓名为’zs’的员工薪水修改为3000元。

update employee set salary=3000 where name='aaa';

17. 将姓名为’aaa’的员工薪水修改为4000元,job改为ccc。

update employee set salary=4000,entry_date='1980-08-08' where name='aaa';

18. 将wu的薪水在原有基础上增加1000元。

update employee set salary=salary+1000 where name='aaa';

19. 删除表中名称为’zs’的记录。

delete from employee where name='aaa';

20. 删除表中所有记录。

delete from employee;
truncate table employee;

21. 查询表中所有学生的信息。

select * from student;

22. 查询表中所有学生的姓名和对应的英语成绩。

select name,english from student;

23. 过滤表中重复数据。

select distinct english from student;

24. 在所有学生的数学分数上加10分特长分。

select name,math+10 from student;

25. 统计每个学生的总分。

select name,(chinese+english+math) from student;

26. 使用别名表示学生分数。

select name as 姓名,(chinese+english+math) as 总分 from student;
select name 姓名,(chinese+english+math) 总分 from student;

27. 查询姓名为wu的学生成绩

select * from student where name='王五';

28. 查询英语成绩大于90分的同学

select * from student where english>90;

29. 查询总分大于200分的所有同学

select * from student where (chinese+math+english)>200;

30. 查询英语分数在 80-90之间的同学。

select * from student where english>80 and english<90;
select * from student where english between 80 and 90;

31. 查询数学分数为89,90,91的同学。

select * from student where math in(89,90,91);

32. 查询所有姓李的学生成绩。

select * from student where name like '李_';

33. 查询数学分>80,语文分>80的同学。

select * from student where math>80 and chinese>80;

34. 对数学成绩排序后输出。

select name,math from student order by math;

35. 对总分排序后输出,然后再按从高到低的顺序输出

select name 姓名,(chinese+math+english) 总分 from student order by 总分 desc;

36. 对姓李的学生成绩排序输出

select name,(chinese+math+english) 总分 from student where name like '李%' order by 总分 desc;

37. 统计一个班级共有多少学生?

select count(*) from student;
select count(name) from student;

38. 统计数学成绩大于90的学生有多少个?

select count(math) from student where math>90;

39. 统计总分大于250的人数有多少?

select count(*) from student where (chinese+math+english)>250;

40. 统计一个班级数学总成绩?

select sum(math) from student;

41. 统计一个班级语文、英语、数学各科的总成绩

select sum(chinese),sum(math),sum(english) from student;

42. 统计一个班级语文、英语、数学的成绩总和

select sum(chinese+math+english) from student;

43. 统计一个班级语文成绩平均分

select sum(chinese)/count(chinese) from student;

44. 求一个班级语文平均分?

select avg(chinese) from student;

45. 求一个班级总分平均分

select avg(chinese+math+english) from student;

46. 求班级最高分和最低分

select max(chinese+math+english),min(chinese+math+english) from student;

47. 对订单表中商品归类后,显示每一类商品的总价

select product,sum(price) from orders group by product;

48. 查询购买了几类商品,并且每类总价大于100的商品

select product from orders group by product having sum(price)>100;

49. 定义主键约束

create table a1
(
id int primary key,
name varchar(40)
);

create table a2
(
id int primary key auto_increment,
name varchar(40)
);

50. 定义非空、唯一约束

create table a3
(
id int primary key auto_increment,
name varchar(40) unique not null
);

51. 定义外键约束

create table husband
(
id int primary key,
name varchar(40) not null
);

create table wife
(
id int primary key,
name varchar(40) not null,
husband_id int,
constraint husband_id_FK foreign key(husband_id) references husband(id)
);

52. 一对多或多对一的表的设计

create table department
(
id int primary key,
name varchar(40)
);

create table employee
(
id int primary key,
name varchar(40),
salary decimal(8,2),
department_id int,
constraint department_id_FK foreign key(department_id) references department(id)
);

53. 多对多数据的保存

create table teacher
(
id int primary key,
name varchar(40),
salary decimal(8,2)
);

create table student
(
id int primary key,
name varchar(40)
);

create table teacher_student
(
teacher_id int,
student_id int,
primary key(teacher_id,student_id),
constraint teacher_id_FK foreign key(teacher_id) references teacher(id),
constraint student_id_FK foreign key(student_id) references student(id) 
);

54. 一对一数据的保存

create table person
(
id int primary key,
name varchar(40)
);

create table idcard
(
id int primary key,
city varchar(100),
constraint id_FK foreign key(id) references person(id)
);

55. 设计category表,保存无限级分类数据

create table category
(
id int primary key,
name varchar(40),
parent_id int,
constraint parent_id_FK foreign key(parent_id) references category(id)
);

 

posted on 2014-05-15 17:03  leocook  阅读(449)  评论(0编辑  收藏  举报

导航