实验4:数据库的嵌套查询

实验4:数据库的嵌套查询

ID:9339225

大小:43.00 KB

页数:4页

时间:2018-04-28

实验4:数据库的嵌套查询_第1页
实验4:数据库的嵌套查询_第2页
实验4:数据库的嵌套查询_第3页
实验4:数据库的嵌套查询_第4页
资源描述:

《实验4:数据库的嵌套查询》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、实验四:数据库的嵌套查询实验学号:姓名:实验四:数据库的嵌套查询实验实验目的:加深对嵌套查询语句的理解。实验内容:使用IN、比较符、ANY或ALL和EXISTS操作符进行嵌套查询操作。实验步骤:一.使用带IN谓词的子查询1.查询与’刘晨’在同一个系学习的学生的信息:select*fromstudentwheresdeptin(selectsdeptfromstudentwheresname='刘晨')比较:select*fromstudentwheresdept=(selectsdeptfromstudentwheresname='刘

2、晨')的异同比较:select*fromstudentwheresdept=(selectsdeptfromstudentwheresname='刘晨')andsname<>'刘晨’比较:selectS1.*fromstudentS1,studentS2whereS1.sdept=S2.sdeptandS2.sname='刘晨'结果:2.查询选修了课程名为’信息系统’的学生的学号和姓名:答:selectsno,snamefromstudentwheresnoin(selectsnofromscwherecnoin(selectcnof

3、romcoursewherecname='信息系统'))3.查询选修了课程’1’和课程’2’的学生的学号(姓名):selectsnofromstudentwheresnoin(selectsnofromscwherecno='1')andsnoin(selectsnofromscwherecno='2')selectx.snofromSCx,SCywherex.sno=y.snoandx.cno='1'andy.cno='2'selectsnofromSCwherecno='1'andsnoin(selectsnofromSCwher

4、ecno='2')比较:查询选修了课程’1’或课程’2’的学生的sno:selectsnofromscwherecno='1'orcno='2'比较连接查询:selectA.snofromscA,scBwhereA.sno=B.snoandA.cno='1'andB.cno='2'结果:第一类的查询结果为’95001’和’95004’;第二类查询结果有7种(包含重复);最后一类利用自连接查询结果有两种。二.使用带比较运算的子查询1.查询比’刘晨’年龄小的所有学生的信息:答:select*fromstudentwheresage<(se

5、lectsagefromstudentwheresname='刘晨')三.使用带Any,All谓词的子查询(对于ALL全称量词,建议改成否定之否定存在量词)2.查询其他系中比信息系(IS)某一学生年龄小的学生姓名和年龄;答:selectsname,sagefromstudentwheresdept<>'IS'andsage

6、dept<>'IS'andsageall(selectsagefromstudentwheresdept='IS')四.使用带Exists谓词的子查询和相关子查询5.查询与其他所有学生年龄均不同的学生学号,姓名和年龄:selectsno,sname,sagefromstudentAwhereno

7、texists(select*fromstudentBwhereA.sage=B.sageandA.sno<>B.sno)6.查询所有选修了1号课程的学生姓名:答:selectsnamefromstudentwhereexists(select*fromscwheresc.sno=student.snoandcno='1')1.查询没有选修了1号课程的学生姓名:答:selectsnamefromstudentwherenotexists(select*fromscwheresc.sno=student.snoandcno='1')2.

8、查询选修了全部课程的学生姓名:答:selectsnamefromstudentwherenotexists(select*fromcoursewherenotexists(select*fromscwheresc.sno

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。