4.2 对于教学数据库的三个基本表
S(SNO,SNAME,AGE,SEX,SDEPT)
SC(SNO,CNO,GRADE)
C(CNO,CNAME,CDEPT,TNAME)
试用SQL的查询语句表达下列查询:
①检索LIU老师所授课程的课程号和课程名。
②检索年龄大于23岁的男学生的学号和姓名。
③检索至少选修LIU老师所授课程中一门课程的女学生姓名。
④检索WANG同学不学的课程的课程号。
⑤检索至少选修两门课程的学生学号。
select distinct from⑥检索全部学生都选修的课程的课程号与课程名。
⑦检索选修课程包含LIU老师所授(全部)课程的学生学号。
参考答案:
①SELECT CNO,CNAME
FROM    C
WHERE TNAME=‘LIU’;
②SELECT SNO,SNAME
FROM S
WHERE AGE>23 AND SEX=‘M’;
③SELECT SNAME (联接查询方式)
FROM S,SC,C
WHERE S.SNO=SC.SNO AND SC.CNO=C.CNO
AND SEX=’F’ AND TNAME=’LIU’;
或:SELECT SNAME (嵌套查询方式)
FROM S
WHERE SEX=‘F’
AND SNO IN(SELECT SNO
FROM SC
WHERE CNO IN (SELECT CNO
FROM C
WHERE TNAME=’LIU’));
或:SELECT SNAME (存在量词方式)
FROM S
WHERE SEX=‘F’
AND EXISTS(SELECT *
FROM SC
WHERE SC.SNO=S.SNO
AND EXISTS(SELECT *
FROM C
WHERE C.CNO=SC.CNO
AND TNAME=’LIU’));
④SELECT CNO
FROM C
WHERE NOT EXISTS
(SELECT *
FROM S,SC
WHERE S.SNO=SC.SNO AND SC.CNO=C.CNO
AND SNAME=’WANG’);
或:SELECT CNO
FROM C
WHERE CNO NOT IN
(SELECT SC.CNO
FROM S,SC
WHERE S.SNO=SC.SNO AND SNAME=’WANG’);
或:SELECT CNO
FROM C
WHERE CNO NOT IN
(SELECT CNO
FROM SC
WHERE SNO IN
(SELECT SNO
FROM S
WHERE SNAME=’WANG’));
⑤SELECT DISTINCT X..SNO
FROM SC AS X,SC AS Y
WHERE X.SNO=Y.SNO AND X.CNO<>Y.CNO;
或:SELECT SNO
FROM SC
GROUP BY SNO HAVING COUNT(CNO)>=2;
⑥SELECT CNO,CNAME
FROM C
WHERE NOT EXISTS
(SELECT *
FROM S
WHERE NOT EXISTS
(SELECT *
FORM SC
WHERE SC.SNO=S.SNO AND SC.CNO=C.CNO));
⑦SELECT DISTINCT SNO
FROM SC AS X
WHERE NOT EXISTS
(SELECT *
FORM C
WHERE TNAME=’LIU’
AND NOT EXISTS
(SELECT *
FROM SC AS Y
WHERE Y.SNO=X.SNO AND Y.CNO=C.CNO));
4.3 设有两个基本表R(A,B,C)和S(D,E,F),试用SQL查询语句表达下列关系代数表达式:
①SELECT A FROM R;
②SELECT * FROM R WHERE B=17;
③SELECT * FROM R,S;
④SELECT A,F FROM R,S WHERE C=D;
4.4 设有两个基本表R(A,B,C)和S(A,B,C),试用SQL查询语句表达下列关系代数表达式:
1.(SELECT * FROM R)
UNION
(SELECT * FROM S);
2.(SELECT * FROM R)
INTERSECT
(SELECT * FROM S);
3.(SELECT * FROM R)
EXCEPT
(SELECT * FROM S);
4.(SELECT A,B FROM R)
NATURAL INNER JOIN
(SELECT B,C FROM S);
或:SELECT R.A, R.B, S.C

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。