欢迎来到天天文库
浏览记录
ID:56773376
大小:1.06 MB
页数:17页
时间:2020-07-08
《数据库的嵌套查询实验报告.doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、实验三:数据库的嵌套查询实验实验目的:加深对嵌套查询语句的理解。实验内容:使用IN、比较符、ANY或ALL和EXISTS操作符进行嵌套查询操作。实验步骤:一.使用带IN谓词的子查询1.查询与’刘晨’在同一个系学习的学生的信息:比较select*fromstudentwheresdeptin(selectsdeptfromstudentwheresname='刘晨')与:select*fromstudentwheresdept=(selectsdeptfromstudentwheresname='刘晨')的异同比较:select*fromstudentwhe
2、resdept=(selectsdeptfromstudentwheresname='刘晨')andsname<>‘刘晨’与:selectS1.*fromstudentS1,studentS2whereS1.sdept=S2.sdeptandS2.sname='刘晨'的异同2.查询选修了课程名为’信息系统’的学生的学号和姓名:比较selectsno,snamefromstudentwheresnoin(selectsnofromscwherecnoin(selectcnofromcoursewherecname='信息系统'))与:selectsno,sn
3、amefromstudentwheresnoin(selectsnofromsc,coursewheresc.cno=course.cnoandcname='信息系统')3.查询选修了课程’1’和课程’2’的学生的学号:selectsnofromstudentwheresnoin(selectsnofromscwherecno='1')andsnoin(selectsnofromscwherecno='2')比较:查询选修了课程’1’或课程’2’的学生的sno:selectsnofromscwherecno='1'orcno='2'比较连接查询:selec
4、tA.snofromscA,scBwhereA.sno=B.snoandA.cno='1'andB.cno='2'二.使用带比较运算的子查询4.查询比’刘晨’年龄小的所有学生的信息:select*fromstudentwheresage<(selectsagefromstudentwheresname='刘晨')三.使用带Any,All谓词的子查询5.查询其他系中比信息系(IS)某一学生年龄小的学生姓名和年龄;selectsname,sagefromstudentwheresage5、IS')andsdept<>'IS'6.查询其他系中比信息系(IS)学生年龄都小的学生姓名和年龄:selectsname,sagefromstudentwheresage'IS'7.查询与计算机系(CS)系所有学生的年龄均不同的学生学号,姓名和年龄:selectsno,sname,sagefromstudentwheresage<>all(selectsagefromstudentwheresdept='CS')四.使用带Exists谓词的子查询和相关子6、查询8.查询与其他所有学生年龄均不同的学生学号,姓名和年龄:selectsno,sname,sagefromstudentAwherenotexists(select*fromstudentBwhereA.sage=B.sageandA.sno<>B.sno)9.查询所有选修了1号课程的学生姓名:selectsnamefromstudentwhereexists(select*fromscwheresno=student.snoandcno='1')10.查询没有选修了1号课程的学生姓名:selectsnamefromstudentwherenotexis7、ts(select*fromscwheresno=student.snoandcno='1')11.查询选修了全部课程的学生姓名:SQLServer中:selectsnamefromstudentwherenotexists(select*fromcoursewherenotexists(select*fromscwheresno=student.snoandcno=course.cno))11.查询至少选修了学生95002选修的全部课程的学生的学号:SQLServer中:selectdistinctsnofromscAwherenotexists(sel8、ect*fromscBwheresno='95002'andnot
5、IS')andsdept<>'IS'6.查询其他系中比信息系(IS)学生年龄都小的学生姓名和年龄:selectsname,sagefromstudentwheresage'IS'7.查询与计算机系(CS)系所有学生的年龄均不同的学生学号,姓名和年龄:selectsno,sname,sagefromstudentwheresage<>all(selectsagefromstudentwheresdept='CS')四.使用带Exists谓词的子查询和相关子
6、查询8.查询与其他所有学生年龄均不同的学生学号,姓名和年龄:selectsno,sname,sagefromstudentAwherenotexists(select*fromstudentBwhereA.sage=B.sageandA.sno<>B.sno)9.查询所有选修了1号课程的学生姓名:selectsnamefromstudentwhereexists(select*fromscwheresno=student.snoandcno='1')10.查询没有选修了1号课程的学生姓名:selectsnamefromstudentwherenotexis
7、ts(select*fromscwheresno=student.snoandcno='1')11.查询选修了全部课程的学生姓名:SQLServer中:selectsnamefromstudentwherenotexists(select*fromcoursewherenotexists(select*fromscwheresno=student.snoandcno=course.cno))11.查询至少选修了学生95002选修的全部课程的学生的学号:SQLServer中:selectdistinctsnofromscAwherenotexists(sel
8、ect*fromscBwheresno='95002'andnot
此文档下载收益归作者所有