数据库作业题讲解(第四章)

  • 格式:ppt
  • 大小:1.87 MB
  • 文档页数:16

下载文档原格式

  / 16
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

6
习题4.6 习题
(1)统计有学生选修的课程门数 ) select count(distinct(cno)) cc from sc (2)求选修 课程的学生的平均年龄 )求选修C4课程的学生的平均年龄 select avg(age) avg_s from sc,s where sc.cno='C4' and sc.sno=s.sno
14
(5)把选修 )把选修MATHS课不及格的成绩全改为空值 课不及格的成绩全改为空值 update sc set grade = NULL where grade<60 and cno in (select cno from c where cname = ‘MATHS’) (6)把低于总平均成绩的女同学成绩提高 )把低于总平均成绩的女同学成绩提高5% update sc set grade = grade*1.05 where grade < (select avg(grade) from sc) and sno in (select sno from s where sex ='女') 女
15
中修改C4课程的成绩 (7)在基本表 中修改 课程的成绩,若成绩小于 )在基本表SC中修改 课程的成绩, 等于75分时提高 分时提高5%,若成绩大于75分时提高 分时提高4%( 等于 分时提高 ,若成绩大于 分时提高 (用 两个UPDATE语句实现) 语句实现) 两个 语句实现 update sc set grade = grade*1.05 where grade<=75 and cno=‘C4’ update sc set grade = grade*1.04 where grade>75 and cno=‘C4’
4
(7)检索全部学生都选修的课程的课程号与课程名 ) select cno,cname from c where not exists (select * from s where not exists (select * from sc where sc.sno = s.sno and sc.cno = c.cno))
8
同学大, (5)检索学号比 )检索学号比WANG同学大,而年龄比他小的 同学大 学生姓名 select x.sname from s x,s y where y.sname like 'WANG%' and x.sno>y.sno and x.age<y.age (6)检索姓名以 )检索姓名以WANG打头的所有学生的姓名和年龄 打头的所有学生的姓名和年龄 select sname,age from s where s.sname like 'WANG%'
Hale Waihona Puke Baidu
10
(9)求年龄大于所有女同学年龄的男学生姓名和年龄 ) select x.sname, x.age from s x where x.sex='男' and 男 x.age > all(select y.age from s y where y.sex ='女') 女 select x.sname, x.age from s x 第二种写法 where x.sex='男' and 男 not exists(select * from s y where y.sex ='女‘ 女 and y.age>=x.age)
3
(5)检索 )检索WANG同学不学的课程的课程号 同学不学的课程的课程号 SELECT cno FROM c WHERE cno not in (select sc.cno from s,sc where s.sname like 'WANG%'and s.sno = sc.sno) (6)检索至少选修两门课程的学生学号 ) select distinct x.sno from SC X, SC Y where x.sno = y.sno and x.cno <> y.cno select sc.sno from sc 第二种写法 group by sc.sno having count(sc.sno)>=2
11
习题4.7 习题
中插入一个学生元组(‘S9’,’WU’,18) (1)向S中插入一个学生元组 ) 中插入一个学生元组 insert into S(SNO,SNAME,AGE) values(‘S9’,’WU’,18)
12
中检索每一门课程成绩都大于等于80 (2)在基本表 中检索每一门课程成绩都大于等于 )在基本表S中检索每一门课程成绩都大于等于 分的学生学号、姓名和性别, 分的学生学号、姓名和性别,并将检索结果保存到另 一个已存在的基本表STUDENT(SNO,SNAME,SEX)中。 一个已存在的基本表 中 insert into STUDENT(SNO,SNAME,SEX) select sno,sname,sex from s where sno in ( select sno from sc where not exists (select * from sc where grade<80 and s.sno =sc.sno));
16
13
(3)在基本表 中删除尚无成绩的选课元组 )在基本表SC中删除尚无成绩的选课元组 delete from sc where grade is NULL
(4)把WANG同学的学习选课和成绩全部删去 ) 同学的学习选课和成绩全部删去 delete from sc where sc.sno in (select sno from s where s.sname='WANG')
5
(8)检索选修课程包含LIU老师所授课程的学生学号 )检索选修课程包含 老师所授课程的学生学号 select distinct s.sno from s where not exists (select * from c where c.tname like ‘LIU%’ and not exists (select * from sc where sc.cno=c.cno and sc.sno=s.sno))
9
(7)在SC中检索成绩为空值的学生学号和课程号 ) 中检索成绩为空值的学生学号和课程号 select sno,cno from sc where grade is NULL (8)求年龄大于女同学平均年龄的男学生姓名和年龄 ) select x.sname, x.age from s x where x.sex='男' and 男 x.age > (select avg(y.age) from s y where y.sex ='女') 女
7
(3)求LIU老师所授课程的每门课程的学生平均成绩 ) 老师所授课程的每门课程的学生平均成绩 select avg(grade) avg_g from sc,c where c.tname like'LIU%' and sc.cno=c.cno group by c.cno
(4)统计每门课程的学生选修人数 ) select cno,count(sc.sno) c_s from sc group by cno
2
(3)检索学号为 的学生所学课程的课程名和 )检索学号为S3的学生所学课程的课程名和 任课教师名 SELECT c.cname, c.tname FROM sc, c WHERE sc.cno = c.cno and sc.sno = 'S3' (4)检索至少选修 )检索至少选修LIU老师所授课程中一门课程的 老师所授课程中一门课程的 女学生姓名 SELECT s.sname FROM s, sc WHERE s.sex = '女' and s.sno = sc.sno and 女 sc.cno in (select cno from c where c.tname like 'LIU%')
作 业 题 讲 解 第四章) (第四章)
1
习题4.2 习题
(1)检索 )检索LIU老师所授课程的课程号和课程名 老师所授课程的课程号和课程名 SELECT cno, cname FROM c WHERE tname LIKE 'LIU%' (2)检索年龄大于 岁的男学生的学号和姓名 )检索年龄大于23岁的男学生的学号和姓名 SELECT sno, sname FROM s WHERE (age > 23) AND (sex = '男') 男