一、表关系
请创建如下表,并创建相关约束
| 班级表:class | 学生表:student | ||||||
| cid | caption | grade_id | sid | sname | gender | class_id | |
| 1 | 一年一班 | 1 | 1 | 乔丹 | 女 | 1 | |
| 2 | 二年一班 | 2 | 2 | 艾弗森 | 女 | 1 | |
| 3 | 三年二班 | 3 | 3 | 科比 | 男 | 2 | |
| 老师表:teacher | 课程表:course | ||||||
| tid | tname | cid | cname | teacher_id | |||
| 1 | 张三 | 1 | 生物 | 1 | |||
| 2 | 李四 | 2 | 体育 | 1 | |||
| 3 | 王五 | 3 | 物理 | 2 | |||
| 成绩表:score | 年级表:class_grade | ||||||
| sid | student_id | course_id | score | gid | gname | ||
| 1 | 1 | 1 | 60 | 1 | 一年级 | ||
| 2 | 1 | 2 | 59 | 2 | 二年级 | ||
| 3 | 2 | 2 | 99 | 3 | 三年级 | ||
| 班级任职表:teach2cls | |||||||
| tcid | tid | cid | |||||
| 1 | 1 | 1 | |||||
| 2 | 1 | 2 | |||||
| 3 | 2 | 1 | |||||
| 4 | 3 | 2 |
二、操作表
1、自行创建测试数据;
#创建数据库: create database work; use work; #class_grade表: create table class_grade(gid int primary key auto_increment, gname char(3)); insert into class_grade(gname) values("一年级"),("二年级"),("三年级"),('四年级'),("五年级"),("六年级"); #teacher表: create table teacher(tid int primary key auto_increment,tname char(4)); insert into teacher(tname) values("alex"),("张三"),("李四"),("王五"); #class表: create table class(cid int primary key auto_increment,caption char(4),grade_id int, foreign key(grade_id) references class_grade(gid) on delete cascade on update cascade ); insert into class(caption,grade_id) values ("一年一班",1),('二年一班',2),('三年一班',3),("四年一班",4),("五年一班",5),("六年一班",6); #course表: create table course(cid int primary key auto_increment,cname char(2),teacher_id int, foreign key(teacher_id) references teacher(tid) on delete cascade on update cascade ); insert into course(cname,teacher_id) values ("语文",1),('数学',2),("英语",3),('生物',4),('物理',1),("化学",2),('政治',4),('体育',4); #student表: create table student(sid int primary key auto_increment,sname char(5),gender enum("男","女"),class_id int, foreign key(class_id) references class(cid) on delete cascade on update cascade ); insert into student(sname,gender,class_id) values ('乔丹', '女', 1), ('艾弗森' ,'女' ,2), ('科比', '女 ',3), ('奥尼尔', '男', 4), ('姚明', '男' ,5), ('麦迪', '男' ,6), ('斯科拉' ,'男', 1), ('詹姆斯', '男' ,2), ('韦德' ,'女', 3), ('费舍尔', '男', 1), ('保罗', '男', 4), ('邓肯', '男', 4), ('吉诺比利', '女', 5), ('罗斯' ,'女' ,6), ('霍华德' ,'女', 5), ('梅西', '男' ,2), ('刘翔' ,'男', 3), ('张三', '男' ,4), ('张四', '女', 4); #teacher2cls表: create table teacher2cls(tcid int primary key auto_increment,tid int,cid int, foreign key(tid) references teacher(tid) on delete cascade on update cascade , foreign key(cid) references class(cid) on delete cascade on update cascade ); insert into teacher2cls(tid,cid) values (2, 1), (2, 2), (3 ,3), (3 ,4), (4, 5), (4 ,6); #score表: create table score(sid int primary key auto_increment,student_id int,course_id int,score int, foreign key(student_id) references student(sid) on delete cascade on update cascade , foreign key(course_id) references course(cid) on delete cascade on update cascade ); insert into score(student_id,course_id,score) values (1 ,1, 60), (1 ,2, 80), (1 ,3, 89), (1 ,4, 90), (2 ,1, 80), (2 ,3, 90), (3 ,2, 81), (4 ,3, 98), (5 ,1, 90), (5 ,2, 100), (5 ,3, 98), (5 ,4, 97), (5 ,5, 95), (5 ,6, 99), (6 ,1, 72), (6, 5, 80), (7 ,5, 40), (7 ,6, 87), (8 ,5, 80), (9, 1, 81), (10 ,2, 30), (10 ,3, 65), (10 ,4, 80), (11 ,1, 67), (11 ,2, 81), (11 ,3, 38), (11 ,4, 78), (12 ,2, 28), (12, 3, 98), (13, 5, 95), (14 ,4, 81), (14 ,5, 82), (15 ,1, 78), (16, 3, 68), (16 ,4, 79), (17, 1, 83);
2、查询学生总人数;
#思路:学生整体为一组.利用聚合函数count(sid)即可拿到总人数
select count(sid) from student;
3、查询“生物”课程和“物理”课程成绩都及格的学生id和姓名;
select sid,sname from student where sid in( select student_id from score where score>=60 and course_id in ( select cid from course where cname='生物' or cname='物理') group by student_id having count(course_id)=2) ;
select gname,count(class.cid) from class inner join class_grade on class.grade_id=class_grade.gid group by gname order by count(class.cid) desc limit 3;
(select student_id,sname,avg(score) from score score inner join student on score.student_id=student.sid group by student_id order by avg(score) desc limit 1) union (select student_id,sname,avg(score) from score score inner join student on score.student_id=student.sid group by student_id order by avg(score) limit 1);
select grade_id,count(student.sid) from student inner join class on student.class_id=class.cid group by grade_id;
select student_id,sname,a,b from (select student_id,count(course_id) as a,avg(score) as b from score group by student_id) as b1 inner join student on student.sid=b1.student_id;
8、查询学生编号为“2”的学生的姓名、该学生成绩最高的课程名、成绩最低的课程名及分数;
select b1.sname,b1.cname,b2.cname,score from (select sname ,cname from score inner join course on score.course_id=course.cid inner join student on student.sid=score.student_id where student_id=2 order by score desc limit 1) as b1 inner join (select sname ,cname ,score from score inner join course on score.course_id=course.cid inner join student on student.sid=score.student_id where student_id=2 order by score limit 1 ) as b2 on b1.sname=b2.sname;
9、查询姓“李”的老师的个数和所带班级数;
select count(cid) from class where cid in( select cid from teacher2cls where tid in( select tid from teacher where tname like"李%"));
select * from class_grade where gid in ( select grade_id from class group by grade_id having count(cid)<5);
11、查询班级信息,包括班级id、班级名称、年级、年级级别(12为低年级,34为中年级,56为高年级)
#思路:建一个年级与年级级别的对应表
create table gradelev(id int primary key auto_increment,name char(3),lev char(3) ); insert into gradelev(name,lev) values ("一年级","低"),("二年级","低"),("三年级","中"),("四年级","中"),("五年级","高"),("六年级",'高'); select cid as "班级id",caption as "班级名称",gname as "年级",lev as "年级级别" from class inner join class_grade on class.grade_id=class_grade.gid inner join gradelev on class_grade.gname=gradelev.name ;
12、查询学过“张三”老师2门课以上的同学的学号、姓名;
select sid,sname from student where sid in( select student_id from score where class_id in( select cid from course where teacher_id =( select tid from teacher where tname="张三")) group by student_id having count(class_id)>2) ;
13、查询教授课程超过2门的老师的id和姓名;
#在课程表中以老师分组
select * from teacher where tid in( select teacher_id from course group by teacher_id having count(cid)>2);
14、查询学过编号“1”课程和编号“2”课程的同学的学号、姓名;
#在课程表中过滤出符合条件的学生id
select sid,sname from student where sid in( select student_id from score where course_id=1 or course_id=2 group by student_id having count(class_id)=2);
15、查询没有带过高年级的老师id和姓名;
select * from teacher where tid in( select tid from teacher2cls where cid in( select cid from class where grade_id in( select gid from class_grade inner join gradelev on gradelev.name=class_grade.gname where lev!="高")));
16、查询学过“张三”老师所教的所有课的同学的学号、姓名;
select sid,sname from student where sid in( select student_id from score where course_id in ( select cid from course where teacher_id in( select tid from teacher where tname="张三")));
17、查询带过超过2个班级的老师的id和姓名;
#在任职表中查询即可
select * from teacher where tid in( select tid from teacher2cls group by tid having count(cid)>2);
select sid,sname from student where sid in( select b1.student_id from (select student_id,score from score where course_id=1) as b1 inner join (select student_id,score from score where course_id=2) as b2 on b1.student_id=b2.student_id where b2.score<b1.score ) ;
select * from teacher where tid =( select tid from teacher2cls group by tid order by count(cid) desc limit 1);
#思路:在成绩表过滤<60点纪录即可
select sid,sname from student where sid in ( select student_id from score where score<60);
select sid,sname from student where sid in( select student_id from score group by student_id having count(course_id)<( select count(cid) from course));
select sid,sname from student where sid in( select student_id from score where course_id in ( select course_id from score where student_id=1));
23、查询至少学过学号为“1”同学所选课程中任意一门课的其他同学学号和姓名;
#在上一提的基础上删除id=1的记录即可
select sid,sname from student where sid in( select student_id from score where course_id in ( select course_id from score where student_id=1)) and sid!=1;
24、查询和“2”号同学学习的课程完全相同的其他同学的学号和姓名;
25、删除学习“张三”老师课的score表记录;
drop from score where tid =( select tid from teacher where tname="张三");
26、向score表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“2”课程的同学学号;②插入“2”号课程的平均成绩;
insert into score (student_id,corse_id,number) select student_id,2,(select avg(score) from score where course_id =2) as a from score where student_id not in (select student_id from score where course_id=2) group by student_id;
27、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;
28、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
select course_id,max(score),min(score) from score group by course_id;
29、按各科平均成绩从低到高和及格率的百分数从高到低顺序;
30、课程平均分从高到低显示(现实任课老师);
31、查询各科成绩前三名的记录(不考虑成绩并列情况)
32、查询每门课程被选修的学生数;
select course_id,count(student_id) from score group by course_id;
33、查询选修了2门以上课程的全部学生的学号和姓名;
select gender,count(sid) from student group by gender order by count(sid) desc;
35、查询姓“张”的学生名单
select * from student where sname like"张%";
select b1.sname,count(b1.sid) from student as b1 inner join student as b2 on b1.sid=b2.sid where b1.sname=b2.sname and b1.sid!=b2.sid;
37、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
select course_id,avg(score) from score group by course_id order by avg(score) ,course_id desc;
38、查询课程名称为“数学”,且分数低于60的学生姓名和分数;
select sname,score from score inner join student on score.student_id=student.sid where score<60 and course_id in( select cid from course where cname='数学');
39、查询课程编号为“3”且课程成绩在80分以上的学生的学号和姓名;
select sid,sname from student where sid in( select student_id from score where course_id=3 and score>80);
40、求选修了课程的学生人数
select count(sid) from student where sid in( select student_id from score );
41、查询选修“王五”老师所授课程的学生中,成绩最高和最低的学生姓名及其成绩;
(select student_id,score from score where course_id in( select cid from course where teacher_id =( select tid from teacher where tname="王五")) order by score desc limit 1) union (select student_id,score from score where course_id in( select cid from course where teacher_id =( select tid from teacher where tname="王五")) order by score limit 1) ;
42、查询各个课程及相应的选修人数;
select course_id,count(student_id) from score group by course_id;
select b1.student_id,b1.course_id,b2.course_id,b1.score from score as b1 inner join score as b2 on b1.student_id=b2.student_id where b1.score=b2.score and b1.course_id !=b2.course_id;
45、检索至少选修两门课程的学生学号;
select student_id from score group by student_id having count(course_id)>1;
46、查询没有学生选修的课程的课程号和课程名;
select cid,cname from course where cid not in( select course_id from score );
47、查询没带过任何班级的老师id和姓名;
select tid,tname from teacher where tid not in ( select tid from teacher2cls);
select b1.student_id,s from (select student_id from score where score>80 group by student_id having count(course_id)>2) as b1 inner join (select student_id,avg(score) as s from score group by student_id) as b2 on b1.student_id=b2.student_id;
49、检索“3”课程分数小于60,按分数降序排列的同学学号;
select student_id from score where score<60 and course_id=3 order by score desc;
50、删除编号为“2”的同学的“1”课程的成绩;
drop from score where student_id=2 and class_id=1;
51、查询同时选修了物理课和生物课的学生id和姓名;
select sid,sname from student where sid in( select student_id from score where course_id in ( select cid from course where cname='生物' or cname='物理') group by student_id having count(course_id)=2) ;
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/231403.html原文链接:https://javaforall.net
