题目:设有“学生-课程”关系数据库,它由3个关系组成,它们的模式是:学生S(学号SNO,姓名SN,所在系DEPT,年龄AGE)、课程C(课程号CNO,课程名CN,先修课号CPNO)、SC(学号SNO,课程号CNO,成绩SCORE)。
sql语言现实如下:
(1)检索学生的所有情况
SELECT * FROM S AS a
JOIN SC AS b
ON a.SNO=b.SNO;
(2)检索学生年龄大于等于20岁的学生姓名
SELECT SN FROM S
WHERE AGE>=20;
(3)检索先修课号为C2的课程号
SELECT CNO FROM C
WHERE CPNO='C2';
(4)检索选修了课程号C1并且成绩为A的所有学生姓名
SELECT SN FROM S
JOIN SC ON S.SNO=SC.SNO
WHERE SC.CNO='C1' AND SC.SCORE=A;
(5)检索学号为S1的学生修读的所有课程名及先修课号
SELECT CN,CPNO FROM C
JOIN SC ON C.CNO=SC.CNO
WHERE SC.SNO='S1';
(6)检索年龄为23岁的学生所修读的课程名
SELECT CN FROM C
JOIN SC ON C.CNO=SC.CNO
JOIN S ON S.SNO=SC.SNO
WHERE AGE=23;
(7)检索至少修读了学号为S5的学生修读的一门课的学生的姓名
SELECT DISTINCT SN
FROM S INNER JOIN SC ON S.SNO=SC.SNO
WHERE CNO = ANY
(SELECT CNO
FROM SC
WHERE SNO='S5');
(8)检索修读了学号为S4的学生所修读的所有课程的学生的姓名
SELECT SN FROM S
WHERE NOT EXISTS(SELECT * FROM SC AS SCX WHERE SCX.SNO='S4'
AND NOT EXISTS(SELECT * FROM SC AS SCY WHERE SCY.SNO=S.SNO AND SCY.CNO=SCX.CNO));
(9)检索选修了所有课程的学生的学号
SELECT SNO FROM S
WHERE NOT EXISTS (SELECT * FROM C
WHERE NOT EXISTS (SELECT * FROM SC
WHERE SC.CNO=C.CNO AND SC.SNO=S.SNO));
(10)检索不选修任何课程的学生学号
SELECT SNO FROM S
WHERE NOT EXISTS (SELECT * FROM C
WHERE EXISTS (SELECT * FROM SC
WHERE SC.CNO=C.CNO AND SC.SNO=S.SNO));
|