Mysql复习题

1. 创建fuxi数据库;
create database fuxi;
2. 删除fuxi数据库;
drop database fuxi;
3. 显示所有数据库;
show databases;
4. 使用/进入 fuxi这个数据库;
use fuxi;
5. 显示fuxi数据库内的所有表;
show tables;
6. 创建没有约束条件的student表
create table student(
id int(11),
name varchar(12),
age int(11));
7. 创建有约束条件的student1表;(主键约束、非空约束)
create table student1(
id int(11) not null, //id 不能为空
name varchar(12),
age int(11),
primary key(id));//主键是id;
8. 创建多字段的约束条件student2表;(复合主键)
create table student2(
id int(11),
name varchar(12),
age int(11),
primary key(id,name) );
9. 创建外键约束 table1表;table2表;
create table table1(
id int(11),
name varchar(22),
location varchar(50),
primary key(id));
create table table2(
id int(11),
name varchar(22) not null,
deptld int (11),
primary key(id),
constraint table1_table2_1 foreign key(deptld) references table1(id));
解释:table2中有一个名为table1_table2_1的外键关联了table1和table2两个表,被外键约束修饰的字段为table2中的deptid,主键字段为table1中的id
10. 创建唯一约束unique; table3表;
被唯一约束修饰了的字段,表示该字段中的值唯一,不能有相同的值。
create table table3(
id int(11) unique,
name varchar(22),
location varchar(50),
primary key(id));
11. 创建默认约束 default ;table4表;默认name为admin
create table table4(
id int(11) unique,
name varchar(22) default ‘admin’,
location varchar(50),
primary key(id));
12. 自动增加 auto_increment table5表;
一个表只能一个字段使用auto_increment,并且使用这个约束的字段只能是整数类型
create table table5(
id int(11) primary key auto_increment,
name varchar(22) not null);
13. 查看创建table1的语句;
show create table table1;
14. 修改table1表名为table1gai;
alter table table1 rename to table1gai;
15. 修改表table1gai内的name字段为newname;
alter table table1gai change name newname int(22);
16. 修改表中的数据类型
alter table table2 modify id varchar(88);
17. 添加字段 table2里面的sex字段;
alter table tabale2 add sex varchar(11);
18. 删除字段 table2中sex字段;
alter table table2 drop sex;
19. 删除表的外键约束;
alter table table2 drop foreign key(id);

单表查询

进入db-student数据库:
use db-student;
20. 查询学生表里面的所有数据;
select * from student;
21. 查询所有学生的学、姓名、性别和出生日期
select sno,sname,ssex, sbirthday from student;
22. 查询所有学生的姓名和现在的年龄; 用age表示;
select sname,year(now())-year(sbirthday) age from student;
23. 查询前 3门课程的课号及课程名称。
select cno,cname from course order by cno limit 3;
24. 查询所有年龄大于18岁的女生的学号和姓名;
select sname,sno from student where year(now())-year(sbirthday)>18 and ssex=”女”;
25. 查询所有男生的信息。
select * from student where ssex=”男”
26. 查询电子商务专业的学生姓名、性别和出生日期;
select sname,ssex,sbirthday from student where speciality=“电子商务”;
27. 查询student表中的所有系名(不重复)。
select distinct sdept from student;
28. 查询成绩在80-90分之间的学生学号和课号。
select sno,cno from sc where degree between 80 and 90;
或:select sno,cno from sc where degree> 80 and degree<90;
29. 查询在1970年1月1日之前出生的男教师信息。
select * from teacher where year(tbirthday)<1970 and tsex=”男”;
30. 输出有成绩的学生学号,按学号升序排序,学号相同的按成绩降序排列。
select sno,degree from sc order by sno,degree desc;
31. 查询所有姓“刘”的学生信息。
select * from student where sname like “刘%”;
32. 查询所有不姓“张”也不姓“王”的同学信息。
select * from student where (sname not like “张%” and sname not lie “王%”);
33. 查询成绩为79分、89分、或99分的记录;
select * from sc where degree=89 or degree=79 or degree=99;
或:select * from sc where degree in(89,79,99);
34. 查询名字中第二个字是“小”字的男生的学生姓名和地址;
select sname,saddress from student where ssex=”男” and sname like ”_小%”;
35. 查询计算机工程系和软件工程系的学生信息;
select * from student where sdept in(“计算机工程系”,“软件工程系”);
36. 统计有多少同学选了课;
select count(DISTNCT sno) 选修总人数 from sc;
37. 统计各门课程的平均成绩,输出课程号和平均成绩;
select cno,AVG(degree) avg from sc GROUP BY cno ORDER BY avg(degree);
38. 查询平均成绩大于等于85分的学生的学号和平均成绩;
select sno,AVG(degree) avg from sc GROUP BY sno HAVING avg(degree);
39. 查询选修1号课程的学生最高分数;
select max(degree) from sc where cno=”1”;
40. 查询选修了三门课以上课程的学生学号;
select sno from sc group by sno having count(*)>3;

多表查询

41. 查询信息工程系女学生的学生学号、姓名及考试成绩;
select x.sno,sname,degree from student x,sc where sdept=” 信息工程系” and ssex=”女” and x.sno=sc.sno;
42. 查询“李勇”同学所选课程的成绩。(不考虑重名)
select degree from sc,student x where x.sname=”李勇” and sc.sno=x.sno;
43. 查询“李新”老师所授课程的课程名称
select x.cname from course x,teacher y,teaching z where x.cno=z.cno and y.tno=z.tno and y.tname=”李新”;
44. 查询女教师所授课的课程号及课程名称;
select course.cno,course.cname from teaching,teacher,course where teacher.tno=teaching.tno and teacher.tsex=”女” and course.cno=teaching.cno;
45. 查询至少选修一门课程的女学生姓名
select x.sname from student x,sc y where x.ssex=”女” and x.sno=y.sno group by x.sno having count(cno)>=1;
46. 查询姓“王”的学生所学的课程名称;
select x.cname from course x,student y,sc z where y.sname like “王%” and y.sno=z.sno and z.cno=x.cno;
47. 查询选修“数据库”课程且成绩再80-90分之间的学生学号及成绩;
select sno,degree from sc,course x where cname=”数据库” and degree between 80 and 90 and sc.cno=x.cno;
48. 查询课程成绩及格的男同学的学生信息及课程号与成绩;
select x.,cno,degree from student x,sc y where degree >=60 and ssex=”男” and x.sno=y.sno;
49. 查询选修“c04” 课程的学生的平均年龄;
select avg(year(now())-year(sbirthday)) age from student x, sc where sc.cno=”c04” and sc.sno=x.sno;
50. 查询学习课程名为“数学”的学生学号和姓名。
select x.sno,x.sname from student x,sc,course y where x.sno=sc.sno and sc.cno=y.cno and y.cname=”数学”;
51. 查询“钱军“教师任课的课程号,选修其课程的学生的学号和成绩、姓名;
select a.tno,y.sno,degree,x.sname from student x,sc y,teaching a,teacher b where b.tname=”钱军” and a.tno=b.tno and a.cno=y.cno and x.sno=y.sno;
52. 查询再第3学期所开课程的课程名称及成绩;
select cname,degree from sc,course a,teaching b where b.cterm=3 and b.cno=a.cno and sc.cno=a.cno;
53. 查询”c02”号课程不及格的学生信息;
select a.
from student a,sc where sc.cno=”c02” and degree<60 and a.sno=sc.sno;
54. 查询信息工程系成绩再90分以上的学生姓名、性别和课程名称
select a.sname,a.ssex,b.cname from student a,course b,sc where sdept=” 信息工程系” and a.sno=sc.sno and degree>90 and sc.cno=b.cno;
55. 查询同时选修了“c04“和”c02”课程的学生姓名和成绩;
select student.sname,a.degree,b.degree from sc a, sc b,student where student.sno=a.sno and student.sno=b.sno and a.sno=b.sno and a.cno=”c04” and b.cno=”c02”;
56. 查询与“张华“ 同一个系的同学姓名;
select sname from student where sdept in(select sdept from student where sname=”张华”) and sname <>“张华“;
57. 查询学号比“刘晨“ 同学大,而出生日期比他小的学生姓名;
select sname from student where(sno>(select sno from student where sname=”刘晨”)) and (sbirthday>(select sbirthday from student where sname=”刘晨”));
58. 查询出生日期大于所有女同学出生日期的男同学的姓名及系别;
select sname,sdept from student where(sbirthday<all(select sbirthday from student where ssex=”女”)) and ssex=”男”;
59. 查询成绩比该课程平均成绩高的学生的学号及成绩;
select sno,degree from sc x where degree>=(select avg(degree)avg from sc y where x.cno=y.cno);
60. 查询不讲授“c01“课的教师姓名;
select tname from teacher x where not exists (select * from teaching y where x.tno=y.tno and cno=”c01”);
61. 查询没有选修“c02“课程的学生学号及姓名
select x.sno,sname from student x where not exists(select * from sc y where x.sno=y.sno and cno=”c02”);
62. 查询一下哪门课没有人选(用子查询)
select course.cname from course where not exists(select * from sc where sc.cno=course.cno);
63. 查询选修了“数据库“课程的学生学号、姓名及系别;
select sname,sno,sdept from student x where x.sno in(select sc.sno from sc where exists(select * from course where course.cname=”数据库” and sc.cno=course.cno));
64. 查询“c02“号课程不及格的学生信息;
select * from student x where x.sno=(select y.sno from sc y where cno=”c02” and degree<60);
65. 查询只被一名学生选修的课程的课程号、课程名;
select cno,cname from course a where cno in(select cno from sc b where a.cno=b.cno group by b.cno having count(b.cno)=1);

数据更新

66. 向 student 表中插入记录(“20050203”,“张静”,“1981-3-21”,“女”,“CS”,“电子商务”)
insert into student (sno,sname,sbirthday,ssex,sdept,speciality)
values (“20050203”,“张静”,“1981-3-21”,“女”,“CS”,“电子商务”);
67. 插入学号为“20050302”、姓名为“李四”的学生信息
insert into student(sno,sname) values (“20050302”,“李四”);
68. 把计算机系的学生记录保存到表TS中(TS表已存在,表结构与Student表相同)
create table ts like student;
insert into ts select * from student where sdept=“计算机系”
69. 将学号为“20050202”的学生姓名改为“张华”,系别改为“CS”,专业改为“多媒体技术”
update student set sname=“张华”,sdept=“CS”,speciality=“多媒体技术” where sno=“20050202”;
70. 将“李勇”同学的专业改为“计算机信息管理”
update student set sdept=“计算机信息管理” where sname=“李勇”;
71. 将“20050201”学生选修“C03”号课程的成绩改为该课的平均成绩
update sc set degree=(select * from (select avg(degree) from sc a where a.cno=“C03”) b) where sno=“20050201” and cno=“C03”;
72. 把成绩低于总平均成绩的女同学的成绩提高5%
update sc set degree = degree*(1+0.05) where sc.sno in(select * from (select sc.sno from sc,student a where degree<all (select avg(degree) from sc group by cno) and a.ssex=“女” and a.sno=sc.sno) b);
73. 把选修了“数据库”课程而成绩不及格的学生的成绩全改为空值(NULL)
update sc b set degree=null where degree<60 and b.cno in(select * from (select a.cno from course a where a.cname=“数据库”) c);
74. 删除学号为“20050302”的学生记录
delete from student where sno=“20050302”;
75. 删除“计算机系”所有学生的选课记录
delete from sc where “计算机系” = (select sdept from student a where a.sno=sc.sno);
76. 删除SC表中尚无成绩的选课记录
delete from sc where degree is null;
77. 把“张晨”同学的成绩全部删除
delete from sc where sc.sno in (select * from (select b.sno from student b where b.sname=“张晨”) c);

发布了35 篇原创文章 · 获赞 19 · 访问量 5210

猜你喜欢

转载自blog.csdn.net/zhangpen130/article/details/90319995