《《数据库原理》实验(共5页).doc》由会员分享,可在线阅读,更多相关《《数据库原理》实验(共5页).doc(5页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上实验四:数据库综合查询一、实验目的1. 掌握SELECT语句的基本语法和查询条件表示方法;2. 掌握查询条件种类和表示方法;3. 掌握连接查询的表示及使用;4. 掌握嵌套查询的表示及使用;5. 了解集合查询的表示及使用。二、实验环境已安装SQL Server 2008 企业版的计算机;具有局域网环境,有固定IP;三、实验学时2学时四、实验要求1. 了解SELECT语句的基本语法格式和执行方法;2. 了解连接查询的表示及使用;3. 了解嵌套查询的表示及使用;4. 了解集合查询的表示及使用;5. 完成实验报告;五、实验内容及步骤以数据库原理实验2数据为基础,请使用T-SQ
2、L 语句实现进行以下操作:1. 查询以DB_开头,且倒数第3个字符为s的课程的详细情况;2. 查询名字中第2个字为阳的学生姓名和学号及选修的课程号、课程名;3. 列出选修了数学或者大学英语的学生学号、姓名、所在院系、选修课程号及成绩;4. 查询缺少成绩的所有学生的详细情况;5. 查询与张力(假设姓名唯一)年龄不同的所有学生的信息;6. 查询所选课程的平均成绩大于张力的平均成绩的学生学号、姓名及平均成绩;7. 按照“学号,姓名,所在院系,已修学分”的顺序列出学生学分的获得情况。其中已修学分为考试已经及格的课程学分之和;8. 列出只选修一门课程的学生的学号、姓名、院系及成绩;9. 查找选修了至少一
3、门和张力选修课程一样的学生的学号、姓名及课程号;10. 只选修“数据库”和“数据结构”两门课程的学生的基本信息;11. 至少选修“数据库”或“数据结构”课程的学生的基本信息;12. 列出所有课程被选修的详细情况,包括课程号、课程名、学号、姓名及成绩;13. 查询只被一名学生选修的课程的课程号、课程名;14. 检索所学课程包含学生张向东所学课程的学生学号、姓名;15. 使用嵌套查询列出选修了“数据结构”课程的学生学号和姓名;16. 使用嵌套查询查询其它系中年龄小于CS系的某个学生的学生姓名、年龄和院系;17. 使用ANY、ALL 查询,列出其他院系中比CS系所有学生年龄小的学生;18. 分别使用
4、连接查询和嵌套查询,列出与张力在一个院系的学生的信息;19. 使用集合查询列出CS系的学生以及性别为女的学生名单;20. 使用集合查询列出CS系的学生与年龄不大于19岁的学生的交集、差集;21. 使用集合查询列出选修课程1的学生集合与选修课程2的学生集合的交集;22. 思考题:按照课程名顺序显示各个学生选修的课程(如 数据库 数据结构 数学);六、出现问题及解决办法如:某些查询操作无法执行,如何解决?1、查询以DB_开头,且倒数第三个字符为s的课程的详细情况select*fromcoursewherecnamelikeDB_%s_2、查询名字中第二个字为“阳”的学生姓名和学号及选修的课程号、课
5、程名selectstudent.sno,student.sname,o,cnamefromstudent,course,scwheresnamelike_阳%andstudent.sno=sc.snoandsc.Cno=o3、列出选修了数学或大学英语的学生学号、姓名、selectstudent.sno,sname,sdept,sc.Cno,cname,gradefromstudent,sc,coursewherestudent.sno=sc.snoandsc.Cno=oandsc.snoin(selectsc.snofromsc,coursewhere(cname=大学英语orcname=数学
6、)andsc.Cno=ogroupbysc.sno)selectstudent.sno,sname,sdept,cno,gradefromstudent,scwhereCnoin(selectCnofromcoursewherecname=数学orcname=大学英语)andsc.sno=student.sno4、查询缺少成绩的所有学生的详细情况;select*fromstudent,scwhereGradeisnullandstudent.sno=sc.sno5、查询与张力(假设姓名唯一)年龄不同的所有学生的信息;select*fromstudentwheresage(selectsagef
7、romstudentwheresname=张力)6、查询所选课程的平均成绩大于张力的平均成绩的学生学号、姓名及平均成绩selectstudent.sno,sname,平均成绩=AVG(grade)fromstudent,scwherestudent.sno=sc.snogroupbystudent.sno,snamehavingAVG(Grade)(selectAVG(Grade)fromsc,studentwheresname=张力andstudent.sno=sc.sno)7、按照学号,姓名,所在院系,已修学分的顺序列出学生学分的获得情况。其中已修学分为考试已经及格的课程学分之和;sele
8、ctstudent.sno,sname,sdept,已修学分=SUM(ccredit)fromstudent,sc,coursewhereGrade60andstudent.sno=sc.snoandsc.Cno=ogroupbystudent.sno,sname,sdept8、列出只选修一门课程的学生的学号、姓名、院系及成绩selectstudent.sno,sname,sdept,Cno,gradefromstudent,scwherestudent.sno=sc.snoandsc.snoin(selectsnofromscgroupbysc.snohavingCOUNT(distinct
9、Cno)=1)9、查找选修了至少一门和张力选修课程一样的学生的学号、姓名及课程号;selectstudent.sno,sname,cnofromstudent,scwherestudent.sno=sc.snoandsc.snoin(selectsc.snofromsc,studentwherecnoin(selectcnofromstudent,scwheresname=张力andstudent.sno=sc.sno)10、只选修数据库和数据结构两门课程的学生的基本信息;selectstudent.sno,snamefromstudent,sc,coursewherestudent.sno=
10、sc.snoandsc.Cno=oandsc.snoin(selectsc.snofromsc,coursewhere(cname=数据库orcname=数学)andsc.Cno=ogroupbysc.snohavingCOUNT(*)=2)groupbystudent.sno,snamehavingCOUNT(*)=211、至少选修数据库或数据结构课程的学生的基本信息;只包括其中一门或两门:selectstudent.sno,sname,sdept,sc.Cno,cname,gradefromstudent,sc,coursewherestudent.sno=sc.snoandsc.Cno=
11、oandsc.snoin(selectsc.snofromsc,coursewhere(cname=数据库orcname=数据结构)andsc.Cno=o)两门课全部包括:selectstudent.sno,sname,sdept,sc.Cno,cname,gradefromstudent,sc,coursewherestudent.sno=sc.snoandsc.Cno=oandsc.snoin(selectsc.snofromsc,coursewhere(cname=数据库orcname=数据结构)andsc.Cno=ogroupbysc.snohavingCOUNT(sc.Cno)=2)
12、12、列出所有课程被选修的详细情况,包括课程号、课程名、学号、姓名及成绩selecto,cname,student.sno,sname,gradefromstudent,sc,coursewherestudent.sno=sc.snoandsc.Cno=oorderbyo13、查询只被一名学生选修的课程的课程号、课程名selecto,cnamefromsc,coursewheresc.Cno=ogroupbysc.Cno,cnamehavingCOUNT(sc.sno)=114、检索所学课程包含学生张向东所学课程的学生学号、姓名至少包含一门张向东所选都的课程:selectstudent.sno
13、,sname,cnofromstudent,scwherestudent.sno=sc.snoandstudent.snoin(selectsnofromsc,coursewheresc.Cno=oandsc.Cnoin(selectcnofromsc,studentwheresname=张向东andstudent.sno=sc.sno)包括张向东所选的全部课程:select*fromstudentwheresnoin(selectsnofromsc,coursewheresc.Cno=oandsc.Cnoin(selectcnofromsc,studentwheresname=张向东ands
14、tudent.sno=sc.sno)groupbysc.snohavingCOUNT(*)=(selectCOUNT(cno)fromsc,studentwheresname=张向东andstudent.sno=sc.sno)15、使用嵌套查询列出选修了数据结构课程的学生学号和姓名selectsno,snamefromstudentwheresnoin(selectsnofromsc,coursewherecname=数据结构andsc.Cno=o)16、使用嵌套查询查询其它系中年龄小于CS系的某个学生的学生姓名、年龄和院系;selectsname,sage,sdeptfromstudentw
15、heresageany(selectsagefromstudentwheresdept=CS)andsdeptCS17、使用ANY、ALL查询,列出其他院系中比CS系所有学生年龄小的学生select*fromstudentwheresageall(selectsagefromstudentwheresdept=CS)andsdeptCS;18、分别使用连接查询和嵌套查询,列出与张力在一个院系的学生的信息;select*fromstudentwheresdeptin(selectsdeptfromstudentwheresname=张力)19、使用集合查询列出CS系的学生以及性别为女的学生名单s
16、elect*fromstudentwheresdept=CSintersectselect*fromstudentwheressex=女20、使用集合查询列出CS系的学生与年龄不大于19岁的学生的交集、差集select*fromstudentwheresdept=CSintersectselect*fromstudentwheresage19select*fromstudentwheresdept=CSexceptselect*fromstudentwheresage1921、使用集合查询列出选修课程1的学生集合与选修课程2的学生集合的交集;selectsnofromscwhereCno=1intersectselectsnofromscwhereCno=222、思考题:按照课程名顺序显示各个学生选修的课程(如数据库数据结构数学)selecto,sno,cnamefromsc,coursewheresc.Cno=oorderbysno,sc.Cnoasc专心-专注-专业