最新版数据库课后习题答案.docx
《最新版数据库课后习题答案.docx》由会员分享,可在线阅读,更多相关《最新版数据库课后习题答案.docx(16页珍藏版)》请在冰豆网上搜索。
最新版数据库课后习题答案
数据库课后习题SQL上机答案,最新、最全(经测试,可用)
createtableT
(T#CHAR(4)NOTNULL,
TNAMECHAR(8)NOTNULL,
TITLECHAR(10),
PRIMARYKEY(T#));
CREATETABLEC
(C#CHAR(4),
CNAMECHAR(10)NOTNULL,
T#CHAR(4),
PRIMARYKEY(C#),
FOREIGNKEY(T#)REFERENCEST(T#));
CREATETABLES
(S#CHAR(4)NOTNULL,
SNAMECHAR(8)NOTNULL,
AGESMALLINT,
SEXCHAR
(1),
PRIMARYKEY(S#));
CREATETABLESC
(S#CHAR(4),
C#CHAR(4),
SCORESMALLINT,
PRIMARYKEY(S#,C#),
FOREIGNKEY(S#)REFERENCESS(S#),
FOREIGNKEY(C#)REFERENCESC(C#));
INSERTINTOT
VALUES('T1','LIU','PROFESSER');
INSERTINTOT
VALUES('T2','LI','PROFESSER');
INSERTINTOT
VALUES('T3','HU','PROFESSER');
INSERTINTOT
VALUES('T4','LIN','PROFESSER');
INSERTINTOS
VALUES('S1','HU',22,'F');
INSERTINTOS
VALUES('S4','CHEN',15,'F');
INSERTINTOS
VALUES('S2','LIU',21,'M');
INSERTINTOS
VALUES('S3','ZHANG',10,'M');
INSERTINTOS
VALUES('S5','WANG',27,'M');
INSERTINTOS
VALUES('S6','LOU',10,'F');
INSERTINTOS
VALUES('S7','SONF',27,'M');
INSERTINTOS
VALUES('S8','MA',10,F');
INSERTINTOS
VALUES('S9','PANG',27,'M');
INSERTINTOS
VALUES('S10','LEE',22,'F');
INSERTINTOS
VALUES('S11','JIANG',15,'F');
INSERTINTOS
VALUES('S12','QIAN',28,'F');
INSERTINTOS
VALUES('S13','MIAO',27,'M');
INSERTINTOS
VALUES('S14','SUN',24,'M');
INSERTINTOS
VALUES('S15','GOU',26,'F');
INSERTINTOC
VALUES('C1','YUWEN','T1');
INSERTINTOC
VALUES('C2','MATHS','T2');
INSERTINTOC
VALUES('C3','YINGYU','T3');
INSERTINTOC
VALUES('C4','KEXUE','T4');
INSERTINTOC
VALUES('C5','SHUJVKU','T1');
INSERTINTOC
VALUES('C6','DANPIANJI','T2');
INSERTINTOC
VALUES('C7','PLC','T3');
INSERTINTOSC
VALUES('S1','C2',85);
INSERTINTOSC
VALUES('S1','C1',66);
INSERTINTOSC
VALUES('S2','C4',77);
INSERTINTOSC
VALUES('S2','C1',85);
INSERTINTOSC
VALUES('S2','C2',66);
INSERTINTOSC
VALUES('S3','C2',45);
INSERTINTOSC
INSERTINTOSC
VALUES('S3','C1',85);
INSERTINTOSC
VALUES('S3','C2,85);
INSERTINTOSC
VALUES('S3','C4',85);
VALUES('S4','C1',78);
INSERTINTOSC
VALUES('S4','C2',85);
INSERTINTOSC
VALUES('S4','C4',85);
INSERTINTOSC
VALUES('S4','C3',85);
INSERTINTOSC
VALUES('S5','C2',84);
INSERTINTOSC
VALUES('S5','C1',84);
INSERTINTOSC
VALUES('S5','C3',81);
INSERTINTOSC
VALUES('S5','C4',77);
INSERTINTOSC
VALUES('S6','C2',85);
INSERTINTOSC
VALUES('S7','C2',85);
INSERTINTOSC
VALUES('S8','C4',85);
INSERTINTOSC
VALUES('S8','C2',85);
INSERTINTOSC
VALUES('S9','C2',85);
INSERTINTOSC
VALUES('S10','C2',85);
INSERTINTOSC
VALUES('S11','C2',85);
INSERTINTOSC
VALUES('S12','C2',85);
INSERTINTOSC
VALUES('S13','C2',85);
INSERTINTOSC
VALUES('S14','C2',85);
INSERTINTOSC
VALUES('S15','C2',85);
INSERTINTOSC
VALUES('S15','C2',85);
INSERTINTOSC
VALUES('S15','C3',);
INSERTINTOSC
VALUES('S14','C3',);
INSERTINTOSC
VALUES('S13','C3',);
INSERTINTOSC
VALUES('S12','C3',);
SELECTS#,SNAME
FROMS
WHEREAGE<17ANDSEX='F';
1.SELECTS#,SNAME
FROMS
WHEREAGE<17ANDSEX=’F’;
2.SELECTC#,CNAME
FROMC
WHEREC#IN
(SELECTC#
FROMSC
WHERES#IN
(SELECTS#
FROMS
WHERESEX=’M’));
或
SELECTC.C#,CNAME
FROMS,SC,C
WHERES,S#=SC.S#ANDSC.C#=C.C#ANDSEX=’M’;
3.SELECTT.T#,TNAME
FROMS,SC,C,T
WHERES.S#=SC.S#ANDSC.C#=C.C#
ANDC.T#=T.T#ANDSEX=’M’;
4.SELECTDISTINCTX.S#
FROMSCASX,SCASY
WHEREX.S#=Y.S#ANDX.C#!
=Y.C#;
5.SELECTDISTINCTX.C#
FROMSCASX,SCASY
WHEREX.S#=’S2’ANDY.S#=’S4’ANDX.C#=Y.C#;
6.SELECTC#
FROMC
WHERENOTEXISTS
(SELECT*
FROMS,SC
WHERES.S#=SC.S#ANDSC.C#=C.C#ANDSNAME=’WANG’);
7.SELECTC#,CNAME
FROMC
WHERENOTEXISTS
(SELECT*
FROMS
WHERENOTEXISTS
(SELECT*
FROMSC
WHERES#=S.S#ANDC#=C.C#));
8.SELECTDISTINCTS#
FROMSCASX
WHERENOTEXISTS
(SELECT*
FROMC,T
WHEREC.T#=T.T#ANDTNAME=’LIU’ANDNOTEXISTS
(SELECT*
FROMSCASY
WHEREY.S#=X.S#ANDY.C#=C.C#));
3.2
①SELECTC#,CNAME
FROMC
WHERETNAME=’LIU’;
2SELECTS#,SNAME
FROMS
WHEREAGE>23ANDSEX=’M’;
③SELECTCNAME,TEACHER
FROMSC,C
WHERESC.C#=C.C#ANDS#=’S3’;
④SELECTSNAME(联接查询方式)
FROMS,SC,C
WHERES.S#=SC.S#ANDSC.C#=C.C#
ANDSEX=’F’ANDTNAME=’LIU’;
或:
SELECTSNAME(嵌套查询方式)
FROMS
WHERESEX=’F’
ANDS#IN(SELECTS#
FROMSC
WHEREC#IN(SELECTC#
FROMC
WHERETNAME=’LIU’));
或:
SELECTSNAME(存在量词方式)
FROMS
WHERESEX=’F’
ANDEXISTS(SELECT*
FROMSC
WHERESC.S#=S.S#
ANDEXISTS(SELECT*
FROMC
WHEREC.C#=SC.C#
ANDTNAME=’LIU’));
⑤SELECTC#
FROMC
WHERENOTEXISTS
(SELECT*
FROMS,SC
WHERES.S#=SC.S#ANDSC.C#=C.C#
ANDSNAME=’WANG’);
6SELECTDISTINCTX.S#
FROMSCASX,SCASY
WHEREX.S#=Y.S#ANDX.C#!
=Y.C#;
7SELECTC#,CNAME
FROMC
WHERENOTEXISTS
(SELECT*
FROMS
WHERENOTEXISTS
(SELECT*
FROMSC
WHERES#=S.S#ANDC#=C.C#));
8SELECTDISTINCTS#
FROMSCASX
WHERENOTEXISTS
(SELECT*
FROMC
WHERETNAME=’LIU’
ANDNOTEXISTS
(SELECT*
FROMSCASY
WHEREY.S#=X.S#ANDY.C#=C.C#));
3.7
SELECTCOUNT(DISTINCTTEACHER)
FROMC;
SELECTAVG(AGE)
FROMS,SC
WHERES.S#=SC.S#ANDC#=’C4’ANDSEX=’F’;
SELECTC.C#,AVG(GRADE)
FROMSC,C
WHERESC.C#=C.C#ANDTEACHER=‘LIU’
GROUPBYC.C#;
SELECTS#,COUNT(C#)
FROMSC
GROUPBYS#
HAVINGCOUNT(*)>5
ORDERBY2DESC,1;
SELECTSNAME
FROMS
WHERES#>ALL(SELECTS#
FROMS
WHERESNAME=’WANG’)
ANDAGEFROMS
WHERESNAME=’WANG’);
SELECTS#,C#
FROMSC
WHEREGRADEISNULL;
SELECTSNAME,AGE
FROMS
WHERESNAMELIKE’L%’;
SELECTSNAME,AGE
FROMS
WHERESEX=’M’
ANDAGE>(SELECTAVG(AGE)
FROMS
WHERESEX=’F’);
SELECTSNAME,AGE
FROMS
WHERESEX=’M’
ANDAGE>ALL(SELECTAGE
FROMS
WHERESEX=’F’);
3.12
INSERTINTOC
VALUES('C8','VC++','BAO');
②INSERTINTOFACULTY(TNAME)
SELECTDISTINCTTEACHER
FROM(SELECTTEACHER,C.C#,AVG(GRADE)
FROMS,SC
WHERESC.C#=C.C#
GROUPBYTEACHER,C.C#)
ASRESULT(TEACHER,C#,AVG_GRADE)ASX
WHERE80<=ALL(SELECTAVG_GRADE
FROMRESULTASY
WHEREY.TEACHER=X.TEACHER);
③DELETEFROMSC
WHEREGRADEISNULL;
④DELETEFROMSC
WHERES#IN(SELECTS#FROMSWHERESEX='F')
ANDC#IN(SELECTC#FROMCWHERETEACHER='LIU');
⑤UPDATESC
SETGRADE=60
WHEREGRADE<60
ANDC#IN(SELECTC#FROMCWHERECNAME='MATHS');
⑥UPDATESC
SETGRADE=GRADE*1.05
WHERES#IN(SELECTS#FROMSWHERESEX='F')
ANDGRADE<(SELECTAVG(GRADE)FROMSC);
⑦用两个UPDATE语句实现:
UPDATESC
SETGRADE=GRADE*1.04
WHEREC#='C4'ANDGRADE>70;
UPDATESC
SETGRADE=GRADE*1.05
WHEREC#='C4'ANDGRADE<=70;
(这两个UPDATE语句的顺序不能颠倒。
)
用一个UPDATE语句实现:
UPDATESC
SETGRADE=GRADE*CASE
WHENGRADE>70THEN1.04
ELSE1.05
END
WHEREC#='C4';
⑧UPDATESC
SETGRADE=GRADE*1.05
WHEREGRADE<(SELECTAVG(GRADE)
FROMSC);
3.15
创建视图:
CREATEVIEWS_GRADE(S#,C_NUM,AVG_GRADE)
ASSELECTS#,COUNT(C#),AVG(GRADE)
FROMSC
GROUPBYS#;
判断是否可以查询:
1.2.3.
①允许查询。
相应的操作如下:
SELECTS#,COUNT(C#)ASC_NUM,AVG(GRADE)ASAVG_GRADE
FROMSC
GROUPBYS#;
②允许查询。
相应的操作如下:
SELECTS#,COUNT(C#)ASC_NUM
FROMSC
GROUPBYS#
HAVINGAVG(GRADE)>80;
③允许查询。
相应的操作如下:
SELECTS#,AVG(GRADE)ASAVG_GRADE
FROMSC
GROUPBYS#
HAVINGCOUNT(C#)>(SELECTCOUNT(C#)
FROMSC
GROUPBYS#
HAVINGS#='S4');
④不允许。
C_NUM是对SC中的学生选修门数进行统计,在未更改SC表时,要在视图S_GRADE中更改门数,是不可能的。
⑤不允许。
在视图S_GRADE中删除选修门数在4门以上的学生元组,势必造成SC中这些学生学习元组的删除,这不一定是用户的原意,因此使用分组和聚合操作的视图,不允许用户执行更新操作。
[3.7]试用SQL查询语句表达下列对第3.2题中4个基本表T、C、S、SC的查询。
解:
①统计有学生选修的课程门数。
SELECTCOUNT(DISTINCTC#)
FROMSC;
②求选修C4课程的女学生的平均年龄。
SELECTAVG(AGE)
FROMS,SC
WHERES.S#=SC.S#ANDC#=’C4’ANDSEX=’F’:
③求LIU老师所授的每门课程的平均成绩。
SELECTC.C#.AVG(SCORE)
FROMSC,C,T
WHERESC.C#=C.C#ANDC.T#=T.T#ANDTNAME=’LIU’
GROUPBYC.C#;
④统计选修每门课程的学生人数(超过l0人的课程才统计)。
要求显示课程号和人数,查询结果按人数降序排列,若人数相同,则按课程号升序排列。
SELECTC#.COUNT(S#)
FROMSC
GROUPBYC#
HAVINGCOUNT(*)>10
ORDERBY2DESC。
1:
⑤检索学号比WANG同学大,而年龄比他小的学生姓名。
SELECTSNAME
FROMS
WHERES#>ALL(SELECTS#
FROMS
WHERESNAME=’WANG’)
ANDAGEFROMS
WHERESNAME=’WANG);
⑥在表SC中检索成绩为空值的学生的学号和课程号。
SELECTS#.C#
FROMSC
WHERESCOREISNULL;
⑦检索姓名以L开头的所有学生的姓名和年龄。
SELECTSNAME.AGE
FROMS
WHERESNAMELIKE’L%’;
⑧求年龄大于女同学平均年龄的男学生的姓名和年龄。
SELECTSNAME.AGE
FROMS
WHERESEX=’M’
ANDAGE>(SELECTAVG(AGE)
FROMS
WHERESEX=’F’):
⑨求年龄大于所有女同学年龄的男学生的姓名和年龄。
SELECTSNAME,AGE
FROMS
WHERESEX=-‘M’
ANDAGE>ALL(SELECTAGE
FROMS
WHERESEX=’F’);
[3.12]
解:
①INSERTINTOC
VALUES(‘C8’,’VC++’,’T6’);
②INSERTINTOFACULTY(TNAME)
SELECTDISTINCTTNAME
FROM(SELECTTNAME,C.C#,AVG(SCORE)
FROMT,C,SC
WHERET.T#=C.T#ANDC.C#=SC.C#
GROUPBYTNAME,C.C#)
ASRESULT(TNAME,C#,AVG—SCORE)ASX
WHERE80<=ALL(SELECTAVG—SCORE
FROMRESULTASY
WHEREY.TNAME=X.TNAME);
③DELETEFROMSC
WHERESCOREISNULL;
④DELETEFROMSC
WHERES#IN(SELECTS#FROMSWHERESEX=’F’)
ANDC#IN(SELECTC#FROMC.TWHEREC.T#=T.T#ANDTNA
⑤UPDATESC
SETSCORE=60
WHERESCORE<60
ANDC#IN(SELECTC#FROMCWHERECNAME=’MATHS’);
⑥UPDATESC
SETSCORE=SCORE*1.05
WHERES#IN(SELECTS#FROMSWHERESEX='F’)
ANDSCORE<(SELECTAVG(SCORE)FROMSC);
⑦用两个UPDATE语句实现:
UPDATESC
SETSCORE=SCORE*1.O4
WHEREC#=’C4’ANDSCORE>70;
UPDATESC
SETSCORE=SCORE*1.05
WHEREC#=’C4’ANDSCORE<=70;
这两个UPDATE语句的顺序不能颠倒。
用一个UPDATE语句实现:
UPDATESC
SETSCORE=SCORE*CASE
WHENSCORE>70THEN1.O4
ELSEl.05
END
WHEREC#=’C4’:
⑧UPDATESC
SETSCORE=SCORE*1.05
WHERESCORE<(SELECTAVO(SCORE)
FROMSC);