数据库原理及应用(第2版)习题参考答案

答:select s.sno,sname,cno,grade from student s

left join sc on s.sno = sc.sno

16. 查询哪些课程没有人选,要求列出课程号和课程名。

答:select c.cno,cname from course c left join sc on c.cno = sc.cno

where sc.cno is null

17.查询计算机系没有选课的学生,列出学生姓名。

答:select sname from student s left join sc on s.sno = sc.sno Where sdept = '计算机系' and sc.sno is null

18. 列出“数据库基础”课程考试成绩前三名的学生的学号、姓名、所在系和考试成绩。 答:select top 3 s.sno, sname, sdept, grade from Student s join SC on s.Sno = SC.Sno join Course c on c.Cno = SC.Cno where cname = '数据库基础' order by grade desc

19.查询VB考试成绩最低的学生的姓名、所在系和VB成绩。

答:select top 1 with ties sname,sdept,grade from student s join sc on s.sno = sc.sno join course c on c.cno = sc.cno where cname = 'VB' order by grade asc

·13· 20. 查询有考试成绩的所有学生的姓名、修课名称及考试成绩,要求将查询结果放在一张新的永久表中,假设新表

名为new_sc。

答:select sname, cname, grade into new_sc from student s join sc on s.sno = sc.sno join course c on c.cno = sc.cno where grade is not null

21. 分别查询信息管理系和计算机系的学生的姓名、性别、修课名称、修课成绩,并要求将这两个查询结果合并成

一个结果集,并以系名、姓名、性别、修课名称、修课成绩的顺序显示各列。

答:select sdept 系名, sname 姓名, ssex 性别, cname 修课名称, grade 修课成绩 from student s join sc on s.sno=sc.sno join course c on c.cno=sc.cno where sdept = '信息管理系'

UNION

select sdept , sname, ssex, cname, grade from student s join sc on s.sno=sc.sno join course c on c.cno=sc.cno where sdept = '计算机系'

22.查询选了VB的学生学号、姓名、所在系和成绩,并对所在系进行如下处理:

13

·14· 当所在系为“计算机系”时,显示“CS”; 当所在系为“信息管理系”时,显示“IS”; 当所在系为“通信工程系”时,显示“CO”;

对其他系,均显示“OTHER”。

答:select s.sno 学号,sname 姓名, case sdept when '计算机系' then 'CS' when '信息系' then 'IS' when '数学系' then 'CO' else 'OTHER'

end as 所在系,grade 成绩

from student s join sc on s.sno = sc.sno join course c on c.cno = sc.cno where cname = 'vb'

23. 用子查询实现如下查询:

(1)查询选了“C001”课程的学生姓名和所在系。

答:select sname,sdept from student where sno in( select sno from sc where cno = ' C001')

(2)查询通信工程系成绩80分以上的学生学号和姓名。 答:select sno,sname from student where sno in( select sno from sc where grade > 80) and sdept = '通信工程系'

(3)查询计算机系考试成绩最高的学生姓名。

答:select sname from student s join sc on s.sno = sc.sno where sdept = '计算机系' and grade = (

select max(grade) from sc join student s on s.sno = sc.sno where sdept = '计算机系')

(4)查询年龄最大的男生的姓名和年龄。 答:select sname,sage from student

Where sage = (select max(sage) from student and ssex = '男') and ssex = '男'

(5)查询“C001”课程的考试成绩高于“C001”课程的平均成绩的学生的学号和“C001”课程成绩。 答:select sno,grade from sc where cno = ' C001'

And grade > (select avg(grade) from sc where cno = ' C001')

24. 创建一个新表,表名为test_t,其结构为:(COL1, COL 2, COL 3),其中:

COL1:整型,允许空值。

COL2:字符型,长度为10 ,不允许空值。 COL3:字符型,长度为10 ,允许空值。

试写出按行插入如下数据的语句(空白处表示空值)。

14

COL1 1 2 COL1 int,

COL2 char(10) not null, COL3 char(10) )

insert into test_t values(NULL, 'B1', NULL) insert into test_t values(1, 'B2', 'C2') insert into test_t(COL1, COL2) values(2, 'B3')

25. 将 “C001”课程的考试成绩加10分。

答:update sc set grade = grade + 10 where cno = 'C001'

B1 B2 B3 COL2 C2 COL3 ·15· 答:create table test_t (

26. 将计算机系所有选修了“计算机文化学”课程的学生成绩加10分,分别用子查询和多表连接形式实现。 答:(1)子查询

update sc set grade = grade + 10 where sno in(

select sno from student where sdept = '计算机系') and cno in(

select cno from course where cname = '计算机文化学')

(2)多表连接

update sc set grade = grade + 10

from sc join student s on sc.sno = s.sno join course c on c.cno = sc.cno

where sdept = '计算机系' and canem = '计算机文化学'

27. 删除修课成绩小于50分的学生的选课记录。 答:delete from sc where grade < 50

28. 删除信息管理系考试成绩小于50分的学生的该门课程的修课纪录,分别用子查询和多表连接形式实现。 答:(1) 用连接查询实现

delete from sc from sc join student s on s.sno=sc.sno where sdept = '信息管理系' and grade < 50 (2)用子查询实现

delete from sc where sno in (

select sno from student where sdept = '信息管理系' ) and grade < 50

29.删除VB考试成绩最低的学生的VB修课记录。 答:delete from sc

15

·16· where grade = (

select min(grade) from sc

join course c on c.cno = sc.cno where cname = 'vb') and cno in(

select cno from course where cname = 'vb')

16

联系客服:779662525#qq.com(#替换为@)