2022年《数据库原理》实验 .pdf
实验四:数据库综合查询一、实验目的1.掌握 SELECT 语句的基本语法和查询条件表示方法;2.掌握查询条件种类和表示方法;3.掌握连接查询的表示及使用;4.掌握嵌套查询的表示及使用;5.了解集合查询的表示及使用。二、实验环境已安装 SQL Server 2008 企业版的计算机;具有局域网环境 , 有固定 IP; 三、实验学时2 学时四、实验要求1.了解 SELECT 语句的基本语法格式和执行方法;2.了解连接查询的表示及使用;3.了解嵌套查询的表示及使用;4.了解集合查询的表示及使用;5.完成实验报告;五、实验内容及步骤以数据库原理实验2 数据为基础,请使用T-SQL 语句实现进行以下操作:1.查询以 DB_ 开头 , 且倒数第 3 个字符为 s的课程的详细情况;2.查询名字中第 2 个字为阳的学生姓名和学号及选修的课程号、课程名;3.列出选修了数学或者大学英语的学生学号、姓名、所在院系、选修课程号及成绩;4.查询缺少成绩的所有学生的详细情况;5.查询与张力 (假设姓名唯一 ) 年龄不同的所有学生的信息;6.查询所选课程的平均成绩大于张力的平均成绩的学生学号、姓名及平均成绩;7.按照?学号, 姓名,所在院系, 已修学分? 的顺序列出学生学分的获得情况。其中已修学分为考试已经及格的课程学分之和;8.列出只选修一门课程的学生的学号、姓名、院系及成绩;9.查找选修了至少一门和张力选修课程一样的学生的学号、姓名及课程号;10. 只选修?数据库?和?数据结构 ?两门课程的学生的基本信息;名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 5 页 - - - - - - - - - 11. 至少选修 ?数据库?或?数据结构 ?课程的学生的基本信息;12. 列出所有课程被选修的详细情况,包括课程号、 课程名、学号、姓名及成绩;13. 查询只被一名学生选修的课程的课程号、课程名;14. 检索所学课程包含学生张向东所学课程的学生学号、姓名;15. 使用嵌套查询列出选修了 ?数据结构 ?课程的学生学号和姓名;16. 使用嵌套查询查询其它系中年龄小于CS系的某个学生的学生姓名、年龄和院系;17. 使用 ANY 、ALL 查询,列出其他院系中比CS系所有学生年龄小的学生;18. 分别使用连接查询和嵌套查询,列出与张力在一个院系的学生的信息;19. 使用集合查询列出CS系的学生以及性别为女的学生名单;20. 使用集合查询列出CS系的学生与年龄不大于19岁的学生的交集、差集;21. 使用集合查询列出选修课程1 的学生集合与选修课程2 的学生集合的交集;22. 思考题: 按照课程名顺序显示各个学生选修的课程(如 200515001 数据库 数据结构 数学) ;六、出现问题及解决办法如:某些查询操作无法执行,如何解决?1、查询以 DB_开头,且倒数第三个字符为s的课程的详细情况select * from course where cname like DB_%s_ 2 、 查 询 名 字 中 第 二 个 字 为 “ 阳 ” 的 学 生 姓 名 和 学 号 及 选 修 的 课 程 号 、 课 程名 select student.sno ,student.sname ,o,cname from student,course,sc where sname like _阳 %and student.sno=sc.sno and sc.Cno=o 3、列出选修了数学或大学英语的学生学号、姓名、select student.sno,sname,sdept,sc.Cno,cname,grade from student,sc,course where student.sno=sc.sno and sc.Cno=o and sc.sno in(select sc.sno from sc,course where (cname=大学英语or cname=数学)and sc.Cno=o group by sc.sno) select student.sno,sname,sdept,cno,grade from student,sc where Cno in (select Cno from course where cname=数学 or cname=大学英语 )and sc.sno=student.sno 4、查询缺少成绩的所有学生的详细情况; select * from student,sc where Grade is null and student.sno=sc.sno 5、查询与张力(假设姓名唯一 )年龄不同的所有学生的信息; select * from student where sage (select sage from student where sname=张力 ) 6、 查 询 所 选 课 程 的 平 均 成 绩 大 于 张 力 的 平 均 成 绩 的 学 生 学 号 、 姓 名 及 平 均 成绩 select student.sno,sname, 平均成绩 =AVG(grade) from student ,sc 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 5 页 - - - - - - - - - where student.sno=sc.sno group by student.sno,sname having AVG(Grade)(select A VG(Grade)from sc,student where sname=张力and student.sno=sc.sno) 7、按照 ?学号,姓名,所在院系,已修学分? 的顺序列出学生学分的获得情况。其中已修学分为考试已经及格的课程学分之和;select student.sno,sname,sdept, 已修学分 =SUM(ccredit) from student,sc,course where Grade60and student.sno=sc.sno and sc.Cno=o group by student.sno,sname,sdept8 、列出只选修一门 课程的学生的学号 、姓名、院系及成绩 select student.sno,sname,sdept,Cno,grade from student,sc where student.sno=sc.sno and sc.sno in (select sno from sc group by sc.sno having COUNT(distinct Cno)=1) 9、查找选修了至少一门和张力选修课程一样的学生的学号、姓名及课程号; select student.sno,sname,cno from student,sc where student.sno=sc.sno and sc.sno in(select sc.sno from sc ,student where cno in (select cno from student,sc where sname=张力 and student.sno=sc.sno) ) 10 、 只 选 修 ? 数 据 库 ? 和 ? 数 据 结 构 ? 两 门 课 程 的 学 生 的 基 本 信息; select student.sno,sname from student,sc,course where student.sno=sc.sno and sc.Cno=o and sc.sno in(select sc.sno from sc,course where (cname=数据库or cname=数学)and sc.Cno=o group by sc.sno having COUNT(*)=2) group by student.sno,sname having COUNT(*)=2 11、至少选修?数据库?或 ?数据结构 ? 课程的学生的基本信息;只包括其中一门或两门:select student.sno,sname,sdept,sc.Cno,cname,grade from student,sc,course where student.sno=sc.sno and sc.Cno=o and sc.sno in(select sc.sno from sc,course where (cname=数据库or cname=数据结构)and sc.Cno=o ) 两门课全部包括:select student.sno,sname,sdept,sc.Cno,cname,grade from student,sc,course where student.sno=sc.sno and sc.Cno=o and sc.sno in(select sc.sno from sc,course where (cname=数据库or cname=数据结构)and sc.Cno=o group by sc.sno having COUNT(sc.Cno)=2) 12 、 列 出 所 有 课 程 被 选 修 的 详 细 情 况 , 包 括 课 程 号 、 课 程 名 、 学 号 、 姓 名 及 成绩 select o,cname,student.sno,sname,grade from student,sc,course where student.sno=sc.sno and sc.Cno=o order by o 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 5 页 - - - - - - - - - 13、查询只被一名学生选修的课程的课程号、课程名 select o,cname from sc,course where sc.Cno=o group by sc.Cno,cname having COUNT(sc.sno)=1 14、检索所学课程包含学生张向东所学课程的学生学号、姓名至少包含一门张向东所选都的课程:select student.sno,sname,cno from student,sc where student.sno=sc.sno and student.sno in (select sno from sc,course where sc.Cno=o and sc.Cno in (select cno from sc,student where sname= 张向东 and student.sno=sc.sno) 包括张向东所选的全部课程: select *from student where sno in (select sno from sc,course where sc.Cno=o and sc.Cno in (select cno from sc,student where sname=张向东and student.sno=sc.sno) group by sc.sno having COUNT(*)=(select COUNT(cno)from sc,student where sname=张向东and student.sno=sc.sno) ) 15 、 使 用 嵌 套 查 询 列 出 选 修 了 ? 数 据 结 构 ? 课 程 的 学 生 学 号 和 姓名 select sno,sname from student where sno in (select sno from sc,course where cname=数据结构 and sc.Cno=o) 16、使用嵌套查询查询其它系中年龄小于CS 系的某个学生的学生姓名、年龄和院系;select sname,sage,sdept from student where sageany(select sage from student where sdept=CS )and sdeptCS 17、 使 用ANY 、ALL 查询,列出其他院系中比CS 系所有学生年龄小的学生 select * from student where sageall(select sage from student where sdept=CS)and sdeptCS; 18 、分别使用连接查询和嵌套查询,列出与张力在一个院系的学生的信息; select * from student where sdept in(select sdept from student where sname=张力 ) 19、使用集合查询列出CS 系的学生以及性别为女的学生名单select * from student where sdept=CS intersect select * from student where ssex= 女 20、使用集合查询列出CS 系的学生与年龄不大于19 岁的学生的交集、差集select * from student where sdept=CS intersect select * from student where sage19 select * from student where sdept=CS except select * from student where sage19 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 5 页 - - - - - - - - - 21、使用集合查询列出选修课程1 的学生集合与选修课程2 的学生集合的交集;select sno from sc where Cno=1 intersect select sno from sc where Cno=2 22、思考题:按照课程名顺序显示各个学生选修的课程(如200515001 数据库 数据结构 数学)select o,sno,cname from sc,course where sc.Cno=o order by sno,sc.Cno asc 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 5 页 - - - - - - - - -