MySQL练习-主外键多表查询
练习:
1.建立表关系:
请创建如下表,并创建相关约束
1 USE db1; 2 CREATE TABLE class( 3 cid INT AUTO_INCREMENT PRIMARY KEY, 4 caption VARCHAR(10) 5 ); 6 CREATE TABLE teacher( 7 tid INT AUTO_INCREMENT PRIMARY KEY, 8 tname VARCHAR(10) 9 ); 10 CREATE TABLE course( 11 cid INT AUTO_INCREMENT PRIMARY KEY, 12 cname VARCHAR(10), 13 teach_id INT, 14 CONSTRAINT fk_course_teacher FOREIGN KEY(teach_id) REFERENCES teacher(tid) 15 ); 16 CREATE TABLE student( 17 sid INT AUTO_INCREMENT PRIMARY KEY, 18 sname VARCHAR(10), 19 gender VARCHAR(10), 20 class_id INT, 21 CONSTRAINT fk_student_class FOREIGN KEY(class_id) REFERENCES class(cid) 22 ); 23 CREATE TABLE score( 24 sid INT AUTO_INCREMENT PRIMARY KEY, 25 student_id INT, 26 course_id INT, 27 number INT, 28 CONSTRAINT fk_score_student FOREIGN KEY(student_id) REFERENCES student(sid), 29 CONSTRAINT fk_score_course FOREIGN KEY(course_id) REFERENCES course(cid) 30 );
1 INSERT INTO class(caption) VALUES("三年二班"); 2 INSERT INTO class(caption) VALUES("一年三班"); 3 INSERT INTO class(caption) VALUES("三年一班"); 4 5 INSERT INTO teacher(tname) VALUES("波多"); 6 INSERT INTO teacher(tname) VALUES("苍空"); 7 INSERT INTO teacher(tname) VALUES("饭岛"); 8 9 INSERT INTO student(sname,gender,class_id) VALUES("钢蛋","女",1); 10 INSERT INTO student(sname,gender,class_id) VALUES("铁锤","女",1); 11 INSERT INTO student(sname,gender,class_id) VALUES("山炮","男",2); 12 13 INSERT INTO course(cname,teach_id) VALUES("生物",1); 14 INSERT INTO course(cname,teach_id) VALUES("体育",1); 15 INSERT INTO course(cname,teach_id) VALUES("物理",2); 16 17 INSERT INTO score(student_id,course_id,number) VALUES(1,1,60); 18 INSERT INTO score(student_id,course_id,number) VALUES(1,2,59); 19 INSERT INTO score(student_id,course_id,number) VALUES(2,2,100);
1 ALTER TABLE score ADD UNIQUE sco_que(student_id,course_id);
2.操作练习:
1、自行创建测试数据
1 INSERT INTO student(sname,gender,class_id) VALUES("钢铁侠","男",4); 2 -- Cannot add or update a child row: a foreign key constraint fails (`db1`.`student`, CONSTRAINT `fk_student_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`cid`))
2、查询“生物”课程比“物理”课程成绩高的所有学生的学号;
1 SELECT s1.student_id FROM (SELECT a.student_id,a.`number` FROM score a,course b 2 WHERE a.`course_id` = b.`cid` AND b.`cname` ="生物") s1 , (SELECT a.student_id,a.`number` FROM score a,course b 3 WHERE a.`course_id` = b.`cid` AND b.`cname`="体育") s2 4 WHERE s1.`student_id` = s2.student_id AND s1.number >s2.number
3、查询平均成绩大于60分的同学的学号和平均成绩;
1 SELECT student_id,AVG(number) 2 FROM score 3 GROUP BY student_id 4 HAVING AVG(number) > 60
4、查询所有同学的学号、姓名、选课数、总成绩;
1 SELECT a.`sid`,a.`sname`,b.`cname`,c.`number`,c.`course_id`,a.`class_id` 2 FROM student a,course b,score c 3 WHERE a.`sid` = c.`student_id` AND c.`course_id` = b.`cid`
5、查询姓“李”的老师的个数;
1 SELECT count(*) as a from teacher where tname LIKE '李%'
6、查询没学过“叶平”老师课的同学的学号、姓名;
1 SELECT a.sid,a.sname FROM student a,teacher b,course c 2 WHERE a.class_id = c.cid and b.tid = c.teach_id AND b.tname != '叶平'
7、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;
1 SELECT a.`student_id`,c.`sname` FROM score a , score b ,student c 2 WHERE a.`student_id`=b.`student_id` AND a.`course_id` ='1' 3 AND b.`course_id`='2' AND c.`sid` = a.`student_id`
8、查询学过“叶平”老师所教的所有课的同学的学号、姓名;
1 SELECT c.`sid`,c.`sname` FROM student c 2 LEFT JOIN score d ON c.`sid` = d.`student_id` 3 LEFT JOIN course b ON d.`course_id` = b.`cid` 4 LEFT JOIN teacher a ON a.`tid` = b.`teach_id` 5 WHERE a.`tname`='波多' 6 GROUP BY c.`sname` 7 HAVING COUNT(c.sname) 8 <(SELECT COUNT(*) FROM teacher a 9 LEFT JOIN course b ON a.`tid` = b.`teach_id` 10 WHERE a.`tname`='波多')
9、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;
1 SELECT s1.student_id FROM (SELECT a.student_id,a.`number` FROM score a,course b 2 WHERE a.`course_id` = b.`cid` AND b.`cid` ='2') s1 , (SELECT a.student_id,a.`number` FROM score a,course b 3 WHERE a.`course_id` = b.`cid` AND b.`cid`='1') s2 4 WHERE s1.`student_id` = s2.student_id AND s1.number >s2.number
10、查询有课程成绩小于60分的同学的学号、姓名;
1 SELECT a.`sid`,a.`sname` FROM student a , score b 2 WHERE a.sid = b.student_id AND number < 60
11、查询没有学全所有课的同学的学号、姓名;
1 SELECT a.`sid`,a.`sname` FROM student a 2 LEFT JOIN score b ON a.`sid` = b.`student_id` 3 GROUP BY a.`sid` 4 HAVING COUNT(b.`student_id`) < (SELECT COUNT(*) FROM course)
12、查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名;
1 SELECT a.`sid`,a.`sname` FROM student a LEFT JOIN score b ON a.`sid` = b.`student_id` 2 LEFT JOIN course c ON c.`cid` = b.`course_id` 3 WHERE c.`cid` IN ( 4 SELECT cc.`cid` FROM student aa LEFT JOIN score bb ON aa.`sid` = bb.`student_id` 5 LEFT JOIN course cc ON cc.`cid` = bb.`course_id` WHERE aa.`sid`='1' 6 ) AND a.`sid` !='1'
13、查询至少学过学号为“001”同学所选课程中任意一门课的其他同学学号和姓名;
SELECT a.`sid`,a.`sname` FROM student a LEFT JOIN score b ON a.`sid` = b.`student_id` LEFT JOIN course c ON c.`cid` = b.`course_id` WHERE c.`cid` IN ( SELECT cc.`cid` FROM student aa LEFT JOIN score bb ON aa.`sid` = bb.`student_id` LEFT JOIN course cc ON cc.`cid` = bb.`course_id` WHERE aa.`sid`='1' ) AND a.`sid` !='1'
14、查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;
1 SELECT a.`sid`,a.`sname` FROM student a LEFT JOIN score b ON a.`sid` = b.`student_id` 2 LEFT JOIN course ac ON b.`course_id`=ac.`cid` 3 WHERE 4 ac.`cid` IN (SELECT c.`cid` FROM student a LEFT JOIN score b ON a.`sid` = b.`student_id` 5 LEFT JOIN course c ON b.`course_id`=c.`cid` WHERE a.`sid`='2') 6 AND a.`sid` != '2' 7 GROUP BY a.`sid`,a.`sname` 8 HAVING 9 (SELECT COUNT(ac.`cid`) AS countCid FROM student aa LEFT JOIN score b ON aa.`sid` = b.`student_id` 10 LEFT JOIN course ac ON b.`course_id`=ac.`cid` WHERE aa.`sid`=a.`sid`) 11 = (SELECT COUNT(c.`cid`) AS cc FROM student a LEFT JOIN score b ON a.`sid` = b.`student_id` 12 LEFT JOIN course c ON b.`course_id`=c.`cid` WHERE a.`sid`='2')
15、删除学习“叶平”老师课的SC表记录;
1 DELETE FROM score WHERE course_id IN ( 2 SELECT a.`cid` FROM course a LEFT JOIN teacher b ON a.`teach_id`=b.`tid` 3 WHERE b.`tname`='叶平' 4 )
16、向SC表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“002”课程的同学学号;②插入“002”号课程的平均成绩;
17、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;
18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
19、按各科平均成绩从低到高和及格率的百分数从高到低顺序;
20、课程平均分从高到低显示(现实任课老师);
21、查询各科成绩前三名的记录:(不考虑成绩并列情况)
22、查询每门课程被选修的学生数;
23、查询出只选修了一门课程的全部学生的学号和姓名;
24、查询男生、女生的人数;
25、查询姓“张”的学生名单;
26、查询同名同姓学生名单,并统计同名人数;
27、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
28、查询平均成绩大于85的所有学生的学号、姓名和平均成绩;
29、查询课程名称为“数学”,且分数低于60的学生姓名和分数;
30、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;
31、求选了课程的学生人数
32、查询选修“杨艳”老师所授课程的学生中,成绩最高的学生姓名及其成绩;
33、查询各个课程及相应的选修人数;
34、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;
35、查询每门课程成绩最好的前两名;
1 SELECT student_id FROM score 2 ORDER BY number DESC LIMIT 2
36、检索至少选修两门课程的学生学号;
1 SELECT a.`sid` FROM student a LEFT JOIN score b ON a.`sid` = b.`student_id` 2 GROUP BY a.`sid` 3 HAVING COUNT(a.`sid`)>=2
37、查询全部学生都选修的课程的课程号和课程名;
1 SELECT DISTINCT c.`cid`,c.`cname` FROM student a 2 LEFT JOIN score b ON a.`sid` = b.`student_id` 3 LEFT JOIN course c ON b.`course_id`=c.`cid`
38、查询没学过“叶平”老师讲授的任一门课程的学生姓名;
1 SELECT a.`sname` FROM student a 2 LEFT JOIN score b ON a.`sid` = b.`student_id` 3 LEFT JOIN course c ON c.`cid` = b.`course_id` 4 WHERE c.`cid` NOT IN ( 5 SELECT cour.`cid` FROM teacher teac 6 LEFT JOIN course cour ON teac.`tid`=cour.`teach_id` 7 WHERE teac.`tname` = '波多' 8 )
39、查询两门以上不及格课程的同学的学号及其平均成绩;
1 SELECT student_id,COUNT(sid) AS co,AVG(number)AS avgg FROM score 2 WHERE number < 60 3 GROUP BY student_id 4 HAVING co >=2
40、检索“004”课程分数小于60,按分数降序排列的同学学号;
1 SELECT b.`sid`,b.`sname`,a.`number` FROM score a LEFT JOIN student b ON a.`student_id` = b.`sid` 2 WHERE a.`course_id` = '4' AND a.`number` < '60' 3 ORDER BY a.`number` DESC
41、删除“002”同学的“001”课程的成绩;
1 DELETE FROM score WHERE student_id ='2' AND course_id = '1'