请创建如下表,并创建相关约束
班级表:class
cid | caption | grade_id |
---|---|---|
1 | 一年一班 | 1 |
2 | 一年二班 | 1 |
3 | 二年一班 | 2 |
4 | 三年二班 | 3 |
5 | 四年二班 | 4 |
6 | 五年二班 | 5 |
7 | 六年三班 | 6 |
学生表:class
sid | sname | gender | class_id |
---|---|---|---|
1 | 乔丹 | 女 | 1 |
2 | 艾弗森 | 女 | 1 |
3 | 科比 | 男 | 2 |
老师表:teacher
tid | tname |
---|---|
1 | 张三 |
2 | 李四 |
3 | 王五 |
课程表:course
cid | cname | teacher_id |
---|---|---|
1 | 生物 | 1 |
2 | 体育 | 1 |
3 | 物理 | 2 |
成绩表:score
sid | student_id | course_id | score |
---|---|---|---|
1 | 1 | 1 | 60 |
2 | 1 | 2 | 59 |
3 | 2 | 2 | 99 |
年级表:class_grade
gid | gname |
---|---|
1 | 一年级 |
2 | 二年级 |
3 | 三年级 |
4 | 四年级 |
5 | 五年级 |
6 | 六年级 |
班级任职表:teach2cls
tcid | tid | cid |
---|---|---|
1 | 1 | 1 |
2 | 1 | 2 |
3 | 2 | 1 |
4 | 3 | 2 |
建表代码
# 年级表
create table class_grade (
gid int unsigned primary key auto_increment,
gname char(10) not null
);
# 教师表
create table teacher (
tid int unsigned primary key auto_increment,
tname char(8) not null
);
# 班级表
create table class (
cid int unsigned primary key auto_increment,
caption char(10) not null,
grade_id int unsigned not null,
foreign key(grade_id) references class_grade(gid)
on update cascade
on delete cascade
);
# 学生表
create table student (
sid int unsigned primary key auto_increment,
sname char(8) not null,
gender enum("男", "女") not null default "男",
class_id int unsigned not null,
foreign key(class_id) references class(cid)
on update cascade
on delete cascade
);
# 课程表
create table course (
cid int unsigned primary key auto_increment,
cname char(10) not null,
teacher_id int unsigned not null,
foreign key(teacher_id) references teacher(tid)
on update cascade
on delete cascade
);
# 成绩表
create table score (
sid int unsigned primary key auto_increment,
student_id int unsigned not null,
course_id int unsigned not null,
score int,
foreign key(student_id) references student(sid)
on update cascade
on delete cascade,
foreign key(course_id) references course(cid)
on update cascade
on delete cascade
);
# 班级任职表
create table teach2cls (
tcid int unsigned primary key auto_increment,
tid int unsigned not null,
cid int unsigned not null,
foreign key(tid) references teacher(tid)
on update cascade
on delete cascade,
foreign key(cid) references class(cid)
on update cascade
on delete cascade
);
插入数据自行准备,这边只提供参考
insert class_grade(gname) values
("一年级"),
("二年级"),
("三年级"),
("四年级"),
("五年级"),
("六年级");
insert teacher(tname) values
("张三"),
("李四"),
("王五"),
("李六"),
("小明"),
("罗翔"),
("吴闲");
insert class(caption, grade_id) values
("一年一班", 1),
("一年二班", 1),
("二年一班", 2),
("三年二班", 3),
("四年一班", 4),
("五年二班", 5),
("六年三班", 6);
insert student(sname, gender, class_id) values
("乔丹", "男", 1),
("科比", "男", 1),
("小美", "女", 2),
("小明", "男", 2),
("张三", "男", 3),
("王五", "女", 3),
("李四", "女", 4),
("赵六", "女", 4),
("奥尔尼", "男", 1),
("姚明", "男", 3),
("麦迪", "男", 6),
("斯科拉", "男", 1),
("詹姆斯", "男", 2),
("韦德", "女", 3),
("费舍尔", "男", 1),
("保罗", "男", 6),
("邓肯", "男", 4),
("吉诺比利", "女", 5),
("罗斯", "女", 6),
("麦迪", "男", 5);
insert course(cname, teacher_id) values
("生物", 1),
("体育", 3),
("物理", 4),
("数学", 5),
("语文", 2),
("英语", 2),
("Python", 5),
("艺术", 1),
("医学", 1),
("刑法", 6),
("人生学", 7);
insert score(student_id, course_id, score) values
(1, 1, 60),
(1, 3, 59),
(2, 2, 99),
(2, 4, 89),
(2, 5, 6),
(2, 6, 60),
(3, 3, 89),
(1, 7, 100),
(5, 3, 66),
(6, 7, 55),
(7, 5, 67),
(8, 9, 100),
(9, 8, 99),
(8, 5, 69),
(15, 10, 76),
(10, 1, 87),
(9, 4, 88),
(13, 5, 78),
(15, 7, 71),
(17, 3, 55),
(12, 4, 58),
(1, 8, 96),
(4, 4, 81),
(16, 1, 99),
(16, 2, 59);
insert teach2cls(tid, cid) values
(1, 1),
(1, 2),
(2, 1),
(2, 4),
(3, 3),
(4, 2),
(5, 5),
(6, 6);
select count(*) as "学生总人数" from student;
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 = "物理"));
先去掉严格模式
set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
select class_grade.gname, count(class.cid) as "班级数" from class join class_grade on class.grade_id = class_grade.gid group by class_grade.gname order by count(class.cid) desc, class_grade.gid limit 3;
(select student.sid, student.sname, avg(score.score) as "平均成绩" from student join score on student.sid = score.student_id group by student.sname order by avg(score.score) desc limit 1)
UNION
(select student.sid, student.sname, avg(score.score) as "平均成绩" from student join score on student.sid = score.student_id group by student.sname order by avg(score.score) limit 1);
select gname, count(sname) as "年级人数" from student join class on student.class_id = class.cid join class_grade on class.grade_id = class_grade.gid group by gname;
select student.sid, student.sname, count(cname) as "选课数", avg(score.score) as "平均成绩" from student join score on student.sid = score.student_id join course on course.cid = score.course_id group by student.sname;
(select student.sname, course.cname, score.score from student join score on student.sid = score.student_id join course on course.cid = score.course_id where student.sid = 2 order by score.score desc limit 1)
UNION
(select student.sname, course.cname, score.score from student join score on student.sid = score.student_id join course on course.cid = score.course_id where student.sid = 2 order by score.score limit 1);
select teacher.tname, count(teach2cls.cid) as "所带班级数" from teacher left join teach2cls on teacher.tid = teach2cls.tid where teacher.tname regexp "^李.*$" group by teacher.tname;
select class_grade.gid, class_grade.gname, count(class.caption) as "班级数" from class_grade join class on class_grade.gid = class.grade_id group by class_grade.gname having count(class.caption) < 5;
select class.cid, class.caption, class_grade.gname,
case
when class_grade.gid between 1 and 2 then "低年级"
when class_grade.gid between 3 and 4 then "中年级"
when class_grade.gid between 5 and 6 then "高年级"
else null
end as "年级级别"
from class join class_grade on class.grade_id = class_grade.gid;
方法一、
select student.sid, student.sname from student join score on score.student_id = student.sid join course on score.course_id = course.cid join teacher on course.teacher_id = teacher.tid where teacher.tname = "张三" group by student.sname having count(teacher.tname) >= 2;
方法二、
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 = "张三")) group by student_id having count(student_id) >= 2);
select * from teacherwhere tid in (select teacher_id from course group by teacher_id having count(teacher_id) > 2);
select sid, sname from student where sid in (select student_id from score where course_id = 1 or course_id = 2);
select * from teacher where tid in (select tid from teach2cls where cid in (select cid from class where grade_id not in (select gid from class_grade where gid between 5 and 6)));
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 = "张三")));
select * from teacherwhere tid in (select tid from teach2cls group by tid having count(cid) >= 2);
select sid, sname from student where sid in (select score.student_id from score join (select * from score where course_id = 1) as t_1 on t_1.student_id = score.student_id and score.course_id = 2 where t_1.score > score.score);
select * from teacher where tid in (select tid from teach2cls group by tid order by count(cid) desc) limit 1;
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));
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 student_id != 1);
select sid, sname from student where sid in (select student_id from score group by student_id having count(course_id) = (select count(course_id) from score where student_id = 2) and student_id != 2);
delete from score where course_id in (select cid from course where teacher_id in (select tid from teacher where tname = "张三"));
insert score(student_id, course_id, score)
select student_id, course_id, avg(score) from score where course_id != 2 group by student_id;
select score.student_id as "学号", c1 as "语文", c2 as "数学", c3 as "英语", count(score.course_id) as "课程数", avg(score.score) as "平均分" from score
left join
(select course_id, student_id, score as c1 from score where course_id in (select cid from course where cname = "语文")) as t_1
on score.student_id = t_1.student_id
left join
(select course_id, student_id, score as c2 from score where course_id in (select cid from course where cname = "数学")) as t_2
on score.student_id = t_2.student_id
left join
(select course_id, student_id, score as c3 from score where course_id in (select cid from course where cname = "英语")) as t_3
on score.student_id = t_3.student_id group by score.student_id order by avg(score.score);
select course_id as "课程ID", max(score) as "最高分", min(score) as "最低分" from score group by course_id;
select course_id, avg(score) as "平均分", round(100*(case when score >=60 then 1 else 0 end)/count(sid), 2) as "及格率" from score group by course_id order by avg(score), (case when score >=60 then 1 else 0 end)/count(sid) desc;
select teacher.tname, course.cname, avg(score.score) as "平均分" from score join course on score.course_id = course.cid join teacheron course.teacher_id = teacher.tid group by course.cname order by avg(score.score) desc;
select t_1.sid, t_1.student_id, t_1.course_id, t_1.score from score as t_1 join score as t_2 on t_1.sid = t_2.sid and t_1.score <= t_2.score group by t_1.student_id, t_1.course_id having count(t_2.student_id) <= 3 order by t_1.course_id, t_1.score desc;
select course.cname as "课程", count(sid) as "学生数" from score join course on score.course_id = course.cid group by course.cname;
select sid, sname from student where sid in (select student_id from score group by student_id having count(course_id) > 2);
select gender, count(gender) as "人数" from student group by gender order by count(gender) desc;
select * from student where sname regexp "^张";
select sname, count(*) "同名人数" from student group by sname having count(*) > 1;
select course.cid, course.cname, avg(score.score) as "平均分" from course join score on course.cid = score.course_id group by course.cname order by avg(score.score), course.cid desc;
select student.sname, score.score from student join score on student.sid = score.student_id where score.course_id in (select cid from course where cname = "数学") and score.score < 60;
select sid, sname from student where sid in (select student_id from score where course_id = 3 and score > 80);
select count(*) as "以选课程人数" from student where sid in (select distinct(student_id) from score);
(select student.sname, score.score from student join score on student.sid = score.student_id where score.course_id in (select cid from course where teacher_id in (select tid from teacherwhere tname = "王五")) order by score.score desc limit 1)
UNION
(select student.sname, score.score from student join score on student.sid = score.student_id where score.course_id in (select cid from course where teacher_id in (select tid from teacher where tname = "王五")) order by score.score limit 1);
select c.cname, count(*) as "选修人数" from score as s join course as c on s.course_id = c.cid group by c.cname;
select distinct(t_1.student_id) as "学号", t_1.course_id as "课程一", t_2.course_id as "课程二", t_1.score from score as t_1, score as t_2 where t_1.score = t_2.score and t_1.course_id != t_2.course_id order by t_1.score desc;
select student.sid, student.sname, cid as "课程号", sc as "分数" from student join (select t1.student_id as sid,t1.score as sc,t1.course_id as cid from score as t1 where 2 > (select count(*) from score as t2 where t1.course_id = t2.course_id and t2.score > t1.score)) as tt on student.sid = tt.sid order by cid, sc desc;
select student_id as "学号", count(course_id) as "课程数" from score group by student_id having count(course_id) >= 2;
select cid, cname from course where cid not in (select course_id from score);
46、查询没带过任何班级的老师id和姓名;
select tid, tname from teacher where tid not in (select tid from teach2cls);
select student.sid,avg(score.score) as "平均成绩", count(score.course_id) as "课程数" from student join score on student.sid = score.student_id where score.score > 80 group by student.sname having count(score.course_id) >= 2;
select student_id from score where course_id = 3 and score < 60 order by score desc;
delete from score where course_id = 1 and student_id = 2;
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);
练习题答案最好是用于参考,不要直接抄,题目先自己思考,实在做不出来再去看答案,看完后试着自己做一边,这样才会有学习的效果