数据库实验五
14.检索所学课程包含学生‘张向东’所学课程的学生学号、姓名;
15.使用嵌套查询列出选修了“数据结构”课程的学生学号和姓名;
16.使用嵌套查询查询其它系中年龄小于CS系的某个学生的学生姓名、年龄和院系;
17.使用ANY、ALL查询,列出其他院系中比CS系所有学生年龄小的学生;
18.分别使用连接查询和嵌套查询,列出与‘张力’在一个院系的学生的信息;
数据库概论课程实验报告(五)
课程名期
2012-12-19
姓名
魏莉莉
学号
12010243032
实验成绩
实验名称
数据库综合查询
实
验
目
的
及
要
求
1.了解select语句的基本语法格式和执行方法;
2.了解连接查询的表示及使用;
3.了解嵌套查询的表示及使用;
4.了解集合查询的表示及使用;
(selectcno
from course
where cname='大学英语'or cname='数学'
)
and sc.sno=student.sno
and o=o;
4.查询缺少成绩的所有学生的详细情况
selectsname,student.sno,Sdept,sage
fromstudent,sc
and o=o;
(cname='大学英语'or cname='数学')可以替换为cname in('大学英语','数学');
selectsname,student.sno,Sdept,o,grade
fromstudent,sc,course
where o in
3.列出选修了‘数学’或者‘大学英语’的学生学号、姓名、所在院系、选修课程号及成绩
selectsname,student.sno,Sdept,o,grade
fromstudent,sc,course
where (cname='大学英语'or cname='数学')
and sc.sno=student.sno
5查询与‘张力’(假设姓名唯一)年龄不同的所有学生的信息
select*
fromstudent
where sage not in
(selectsage
from student
where sname='张力'
) ;
6.查询所选课程的平均成绩大于张力的平均成绩的学生学号、姓名及平均成绩
selectsc.sno,sname,avg(grade)
where o=o
group by o
having count(*)=1
14.检索所学课程包含学生‘张向东’所学课程的学生学号、姓名
selectstudent.sno,sname
from student
where exists(
selectsno
from sc
where sno in(
过
程
及
实
验
结
果
(详细记录在调试过程中出现的问题及解决方法。记录实验执行的结果)
1.查询以‘DB_’开头,且倒数第3个字符为‘s’的课程的详细情况
说明:用'PA_'开头,倒数第三个为'L'的代替题目要求的
select*
from course
where cname like 'PA%L__';
2.查询名字中第2个字为‘阳’的学生姓名和学号及选修的课程号、课程名
where sc.sno=student.sno and
o in
(selectdistinct cno
from sc,student
where sname='张立'
group by sc.sno)
10.
11.至少选修“数据库”或“数据结构”课程的学生的基本信息
selectsc.sno,sname,sdept,sage,ssex
selectstudent.sno学号,sname姓名,sdept所在院系,sum(ccredit)已修学分
from student,sc,course
where sc.sno=student.sno
and o=o
and grade>=60
group by sc.sno
8.列出只选修一门课程的学生的学号、姓名、院系及成绩
18.分别使用连接查询和嵌套查询,列出与‘张力’在一个院系的学生的信息
select*
from student
where sdept =any(
selectsdept
from student
where sname='张力')
selects1.*
from student s1,student s2
where s1.sno=s2.sno
and sdept!='CS';
17.使用ANY、ALL查询,列出其他院系中比CS系所有学生年龄小的学生
selectsno, sname,sage,sdept
from student
where sage < all(
selectsage
from student
where sdept='CS')
and sdept!='CS';
19.使用集合查询列出CS系的学生以及性别为女的学生名单;
20.使用集合查询列出CS系的学生与年龄不大于19岁的学生的交集、差集;
21.使用集合查询列出选修课程1的学生集合与选修课程2的学生集合的交集;
22.思考题:按照课程名顺序显示各个学生选修的课程(如200515001数据库数据结构数学);
调
试
and s1.sdept in(
selectsdept
from student
where sname='张力')
19.使用集合查询列出CS系的学生以及性别为女的学生名单
select*
from student
where sdept='CS'
union
select*
from student
where ssex='女';
from sc,course
where cname='数据结构')
16.使用嵌套查询查询其它系中年龄小于CS系的某个学生的学生姓名、年龄和院系
selectsname,sage,sdept
from student
where sage <any(
selectsage
from student
where sdept='CS')
where student.sno in
(selectsno
from sc
where grade is null
)
and sc.sno=student.sno ;
selectsname,student.sno,Sdept,sage
fromstudent,sc
where grade is null
and sc.sno=student.sno ;
20.使用集合查询列出CS系的学生与年龄不大于19岁的学生的交集、差集
交集
select*
from student
where sdept='CS'
intersect
select*
from student
where sage!>19';
差集
select*
from student
where sdept='CS'
and o=o;
连接查询方式:
selectsname,student.sno,o,cname
fromstudent,sc,course
where sname like '_菁%'and sc.sno=student.sno
and o=o;
9.查找选修了至少一门和张力选修课程一样的学生的学号、姓名及课程号;
10.只选修“数据库”和“数据结构”两门课程的学生的基本信息;
11.至少选修“数据库”或“数据结构”课程的学生的基本信息;
12.列出所有课程被选修的详细情况,包括课程号、课程名、学号、姓名及成绩;
13.查询只被一名学生选修的课程的课程号、课程名;
from sc,student
where sc.sno=student.sno
group by sno
having avg(grade)>(
selectavg(grade)
from sc,student
where sname='张立')
7.按照“学号,姓名,所在院系,已修学分”的顺序列出学生学分的获得情况。其中已修学分为考试已经及格的课程学分之和
union
selectstudent.sno,sname,sdept,sage
from student,sc
where sc.sno=student.sno and cno='2'
总
结
(对实验结果进行分析,问题回答,实验心得体会及改进意见)
不会做的题有:10、12、22
有疑问的是14,题中用的是exists,我觉得用“in”或“=any”也可以,但是为什么出现的结果完全不对?
selectsno
from student
where student.sno=sc.sno
and sname='张向东' ));