mysql cmd 创表查表练习

it2022-07-04  163

创表 :

    格式:字段名  数据类型        可否为空            主键    含义

      (起的名字) varchar()   null(可以为空) /no null(不可为空)    primary key  comment 注释  

#学生表 create table student( sno varchar(20) not null primary key comment '学号', sname varchar(20) not null comment '学生姓名', ssex varchar(20) not null comment '学生性别', sbirthday Datetime comment '学生出生年月', clsaa varchar(20) comment '学生所在班级' ); insert into student values ('108','曾华','','1977-09-01','95033'), ('105','匡明','','1975-10-02','95031'), ('107','王丽','','1976-01-23','95033'), ('101','李军','','1976-02-20','95033'), ('109','王芳','','1975-02-10','95031'), ('103','陆君','','1974-06-03','95031'); #课程表 create table course( cno varchar(20) not null primary key comment '课程号', cname varchar(20) not null comment '课程表名称', tno varchar(20) not null comment '教工编号' ); insert into course values ('3-105','计算机导论','825'), ('3-245','操作系统','804'), ('6-166','数字电路','856'); insert into course values ('9-888','高等数学','831'); #表三 drop table if exists score; create table score( sno varchar(20) not null comment '学号', cno varchar(20) not null comment '课程号', degree Decimal(4,1) comment '成绩', primary key(sno,cno) ); insert into score values ('103','3-245',86), ('105','3-245',75), ('109','3-245',68), ('103','3-105',92), ('105','3-105',88), ('109','3-105',76), ('101','3-105',64), ('107','3-105',91), ('108','3-105',78), ('101','6-166',85), ('107','6-166',79), ('108','6-166',81); #表四 create table teacher( tno varchar(20) not null primary key comment '教工编号', tname varchar(20) not null comment '教工姓名', tsex varchar(20) not null comment '教工性别', tbirthday datetime comment '教工出生年月', prof varchar(20) comment '职称', depart varchar(20) not null comment '教工所在部门' ); insert into teacher values ('804','李诚','','1958-12-02','副教授','计算机系'), ('856','张旭','','1969-03-12','讲师','电子工程系'), ('825','王萍','','1972-05-05','助教','计算机系'), ('831','刘冰','','1977-08-14','助教','电子工程系 '); 创表和插入数据

查表练习:

    select clsaa,year(now())-year(sbirthday) from student group by clsaa desc,sbirthday; //根据出生年月来排序年龄的顺序

    select clsaa from student group by clsaa,ssex having ssex='男' and count(ssex)>=2; //查询至少有2名男生的班号

    ( group by 是分组,如果用分组,后面的条件要用 having ,不能用where;    count(ssex)统计一组男生中的数量)

    select cno,sno,degree from score where cno='3-105' and degree>(select min(degree) from score where cno='3-245') order by degree desc;

    //查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。

 

#20、查询score中选学多门课程的同学中分数为非最高分成绩的记录。 select * from Score a where Sno in (select Sno from Score group by Sno having count(*)>1 ) and Degree not in (select max(Degree) from Score b where b.Cno = a.Cno); #21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 select * from score where cno = '3-105' and degree>(select degree from score where sno = '109'and cno='3-105'); #22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列 select sno,sname,sbirthday from student where year(sbirthday)=(select year(sbirthday) from student where sno='107'); #23、查询“张旭“教师任课的学生成绩(姓名)。 select sname,degree from teacher join course on teacher.tno = course.tno join score on course.cno = score.cno join student on student.sno = score.sno where tname = '张旭'; select sname,degree from score join course on score.cno = course.cno join teacher on teacher.tno = course.tno join student on score.sno = student.sno where tname = '张旭'; #24、查询考计算机导论的学生成绩 select sname,degree from course join score on course.cno=score.cno join student on student.sno=score.sno where course.cname='计算机导论'; 24.1、查询李诚老师教的课程名称 select cname from course join teacher on course.tno=teacher.tno where tname='李诚'; 24.2、教高等数学的老师是哪个系的 select depart from teacher join course on teacher.tno= course.tno where cname='高等数学'; 24、查询选修某课程的同学人数多于5人的教师姓名。 25、查询95033班和95031班全体学生的记录。 select * from student where clsaa='95033' or clsaa='95031'; 26、  查询存在有85分以上成绩的课程Cno. select cno from score where degree>'85'; 27、查询出“计算机系“教师所教课程的成绩表。 select degree from score join course on score.cno=course.cno join teacher on teacher.tno=course.tno where depart='计算机系'; 29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 select cno,sno,degree from score where cno='3-105' and degree>(select min(degree) from score where cno='3-245') order by degree desc; 30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree. select cno,sno,degree from score where cno='3-105' and degree>(select max(degree) from score where cno='3-245'); 31、 查询所有教师和同学的name、sex和birthday. select tname,tsex,tbirthday from teacher union select sname,ssex,sbirthday from student; 32、查询所有“女”教师和“女”同学的name、sex和birthday. select tname,tsex,tbirthday from teacher where tsex='' union select sname,ssex,sbirthday from student where ssex=''; 33、 查询成绩3-105课程平均成绩低的同学的成绩表。 select * from score where degree<(select avg(degree) from score where cno = '3-105'); 34、 查询所有任课教师的Tname和Depart. select tname,depart from teacher; 35、查询所有未讲课的教师的Tname和Depart. select tname,depart from teacher where prof !='助教'; 35、查询所有未讲课的教师的Tname和Depart. select tname,depart from teacher where prof ='助教'; 36、查询至少有2名男生的班号。 select clsaa from student group by clsaa,ssex having ssex='' and count(ssex)>=2; 37、查询Student表中不姓“王”的同学记录。 select * from student where sname not like '王_'; 38、查询Student表中每个学生的姓名和年龄。 select sname,year(now())-year(sbirthday) from student; 39、查询Student表中最大和最小的Sbirthday日期值。 select min(sbirthday),max(sbirthday) from student; 40、以班号和年龄从大到小的顺序查询Student表中的全部记录。 select clsaa,year(now())-year(sbirthday) from student group by clsaa desc,sbirthday; 41、查询“男”教师及其所上的课程。 select teacher.*,cname from teacher join course on teacher.tno=course.tno where tsex=''; 42、查询最高分同学的Sno、Cno和Degree列。 select sno,cno,degree from score where degree in (select max(degree) from score); 43、查询和“李军”同性别的所有同学的Sname. select sname from student where ssex in (select ssex from student where sname = '李军'); 44、查询和“李军”同性别并同班的同学Sname. select sname from student where ssex in (select ssex from student where sname = '李军') and clsaa in (select clsaa from student where sname = '李军'); 45、查询所有选修“计算机导论”课程的“男”同学的成绩表。 select degree from score where cno in (select cno from course where cname='计算机导论') and sno in (select sno from student where ssex=''); 查表练习20-45

 

转载于:https://www.cnblogs.com/cp123/p/8939955.html

相关资源:DirectX修复工具V4.0增强版

最新回复(0)