Upload
donald
View
179
Download
0
Embed Size (px)
DESCRIPTION
对于教学数据库的 3 个基本表 S(S#,SNAME,AGE,SEX) SC(S#,C#,GRADE) C(C#,CNAME,TEACHER) 试用 SQL 的查询语句表达下列查询 1 检索 LIU 老师所授课程的课程号和课程名 2 检索年龄大于 23 岁的男生的学号和姓名 3 检索学号为 S3 学生所学课程的课程名与任课教师名 4 检索至少选修 LIU 老师所授课程中一门课程的女学生姓名. 5 检索 WANG 同学不学的课程的课程号 6 检索至少选修两门课程的学生学号 7 检索全部学生都选修的课程的课程号与课程名. 8 统计有学生选修的课程门数 - PowerPoint PPT Presentation
Citation preview
• 对于教学数据库的 3个基本表• S(S#,SNAME,AGE,SEX)• SC(S#,C#,GRADE)• C(C#,CNAME,TEACHER)• 试用 SQL的查询语句表达下列查询
– 1检索 LIU老师所授课程的课程号和课程名– 2检索年龄大于 23岁的男生的学号和姓名– 3检索学号为 S3学生所学课程的课程名与任课教师名– 4检索至少选修 LIU老师所授课程中一门课程的女学生姓名
• 5检索WANG同学不学的课程的课程号• 6检索至少选修两门课程的学生学号• 7检索全部学生都选修的课程的课程号与课程名
• 8统计有学生选修的课程门数• 9求选修 C4课程的女学生的平均年龄• 10求 LIU老师所授课程的每门课程的平均成绩• 11统计每门课程的学生选修人数(超过 10人的课程才统计)。显示课程号和人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
• 12检索学号比 wang同学大而年龄比他小的学生姓名• 13在表 SC中检索成绩为空值的学生学号和课程号• 14检索姓名以 L打头的所有学生的姓名和年龄• 15求年龄大于女学生平均年龄的男学生姓名和年龄• 16求年龄大于所有女学生年龄的男学生姓名和年龄
• 17. 往关系 c中插入一个课程元组(’ c8’,’VC++’,’BAO’)
• 18. 检索所授每门课程平均成绩均大于 80分的教师姓名,并把检索到的值送往另一个已存在的表 Faculty( Tname)
• 19. 在 SC中删除尚无成绩的选课元组• 20。把选修 LIU老师课程的女同学选课元组全部删除
• 21。把MATHS课不及格的成绩全改为 60分
• 22。把低于所有课程总平均成绩的男同学成绩提高 5%
• 23.在表 SC中修改 C4课程的成绩,若成绩小于等于 70分时提高 5%,若成绩大于70分时提高 4%
• 解答• 1. select C#, CName, from c where teache
r =‘liu’
• 2. select S#, Sname from s where age>23 and sex=‘M’
• 3. select cname, teacher from sc,c where sc.c#=c.c# and s#=‘s3’
• 4. Select sname from s, sc, c where s.s#=sc.s# and sc.c#=c.c# and sex=‘F’ and teacher=‘LIU’
• Or: select Sname from s where sex=‘F’ and s# in (select s# from sc where c# in (select c# from c where teacher =‘LIU’))
• Or : select sname form s where sex=‘F’ and exists (select * from sc where sc.s#=s.s# and exists (select * from c where c.c#=sc.c# and teacher =‘LIU’))
• 5.Select c# from c where not exists (select * from s, sc where s.s#=sc.s# and sc.c#=c.c# and sname=‘WANG’)
• 6.Select distinct x.s# from sc as x, sc as y where x.s#=y.s# and x.c#<>y.c#
• 7.Select c#, cname from c where not exists(select * from s where not exists (select * from sc where s#=s.s# and c#=c.c#))
• 8.select count(distinct c#) from sc• 9. select avg(age) from s, sc where s.s#=s
c.s# and c#=‘c4’ and sex=‘F’• 10. select c.c#, avg(grade) from sc, c wher
e sc.c#=c.c# and teacher=‘LIU’ group by c.c#
• 11.select c#, count(s#) from sc group by c# having count(*) >10 order by count(s#) DESC, c#
• 12.select sname from s where s#>all (select s# from s where sname=’WANG’) and age<all(select age from s where sname=‘WANG’)
• 13.select s#, c# from sc where grade IS NULL
• 14. select sname, age from s where sname like ‘L%’
• 15. select sname, age from s where sex=‘M’ and age>(select avg(age) from s where sex=‘F’)
• 16.Select sname, age from s where sex=‘M’ and age>all (select age from s where sex=‘F’)
• 17.insert into c values(‘c8’,’vc++’,’BAO’)
• 18.insert into faculty (tname) select distinct teacher from (select teacher,c.c#,avg(grade) from s,sc where sc.c#=c.c# group by teacher,c.c#) as result (teacher, c#,avg_grade) as x where 80<=all(select avg_grade from result as y where y.teacher=x.eacher)
• 19.delete from sc where grade is null• 20. delete from sc where s# in (select s#from s
where sex=‘F’) and c# in(select c# from c where teacher=‘LIU’)
• 21.update sc set grade=60 where grade<60 and c# in(select c# from c where cname=‘MATHS’)
• 22.update sc set grade=grade*1.05 wehre s# in(select s# from s where sex=‘F’) and grade<(select avg(grade) from sc)
• 23. update sc set grade=grade*1.04 where c#=‘c4’ and grade>70;
update sc sc set grade=grade*1.05 where c#=‘c4’ and grade<=70;
• 针对教学库基本表 SC,建立一个视图:• Create view s_grade (s#, c_num,avg_grad
e) as select s#, count (c#), avg (grade) from sc group by s#;
• 试判断下列查询和更新操作是否允许执行。允许的话写出转换到基本表 sc上的相应操作。
• 1. select * from s_grade;
• 2. select s#,c_num from s_grade where avg_grade>80;
• 3. select s#,avg_grade from s_grade where c_num>(select c_num from s_grade where s#=‘s4’);
• 4.Update s_grade set c_num=c_num+2 where s#=‘s4’
• 5.Update delete from s_grade where c_num>4;
• 解答• 1.允许。相应操作:
Select s#, count(c#) as c_num, avg(grade) as avg_grade from sc group by s#
• 2. 允许。select s#,count(c#) as c_num from sc group by s# having avg(grade) >80
• 3. 允许select s#,avg(grade) as avg_grade from sc group by s# having count(c#) >(select count (c#) from sc group by s# having s#=‘s4’)
• 4.不允许• 5.不允许
• 对于下面的关系 R和S,求出下列各种连接操作的执行结果
• 1. 自然连接• 2. 右外连接• 3. 全外连接 A B C
a1 b1 c1
a2 b2 c2
a3 b3 c3
B C D
b1 c1 d1
b2 c2 d2
b4 c4 d4