实验六 嵌套查询.doc_第1页
实验六 嵌套查询.doc_第2页
实验六 嵌套查询.doc_第3页
实验六 嵌套查询.doc_第4页
实验六 嵌套查询.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实 验 五一、实验名称:数据库嵌套查询二、实验目的1. 掌握嵌套查询的表示及使用;2. 掌握使用IN、比较符;3. 掌握ANY或ALL;4. 掌握EXISTS操作符进行嵌套查询操作。三、实验环境已安装SQL Server 2005 企业版的计算机;具有局域网环境,有固定IP;四、实验学时2学时五、实验要求1. 了解连接查询的表示及使用;2. 完成实验报告。实验内容及步骤: 在WHERE子句中包含一个形如SELECT-FROM-WHERE的查询块,此查询块称为子查询或嵌套查询,包含子查询的语句称为父查询或外部查询。一、返回一个值的子查询 当子查询的返回值只有一个时,可以使用比较运算符(=, , =, ALL(SELECT SAL FROM T WHERE DEPT=电力) AND DEPT!= 电力4. 使用EXISTS EXISTS表示存在量词,带有EXISTS的子查询不返回任何实际数据,它只得到逻辑值“真”或“假”。当子查询的的查询结果集合为非空时,外层的WHERE子句返回真值,否则返回假值。【例5】查询讲授课程号为C5的教师姓名SELECT TNFROM TWHERE EXISTS(SELECT * FROM TC WHERE TNO=T.TNO AND CNO=C5) 综合实例:【实例1】在数据库Student_test中完成以下查询:1. 查询与刘晨在同一个系学习的学生的信息select * from student where sdept in (select sdept from student where sname=刘晨)2. 查询选修了课程名为信息系统 的学生的学号和姓名select no, sname from student where sno in(select sno from sc where cno in (select cno from course where cname=信息系统)3. 查询选修了课程1和课程2的学生的学号select sno from student where sno in (selectsnofrom sc where cno=1) and sno in (select sno from sc where cno=2)4. 查询比刘晨年龄小的所有学生的信息select * from student where sage (select sage from student where sname=刘晨)5. 查询其他系中比信息系(IS)某一学生年龄小的学生姓名和年龄select sname, sage from student where sage Any(select sage from student where sdept=IS) and sdeptIS6. 查询其他系中比信息系(IS)学生年龄都小的学生姓名和年龄select sname, sage from student where sage ALL(select sage from student where sdept=IS) and sdeptIS7. 查询与计算机系(CS)系所有学生的年龄均不同的学生学号, 姓名和年龄select sno,sname,sage from student where sageall(select sage from student where sdept=CS)8. 查询与其他所有学生年龄均不同的学生学号, 姓名和年龄select sno,sname,sage from student A where not exists(select * from student B where A.sage=B.sage and A.snoB.sno) 9. 查询所有选修了1号课程的学生姓名select sname fromstudent where exists(select * from sc where sno=student.sno and cno=1)10. 查询没有选修了1号课程的学生姓名select sname from student where not exists(select * from sc where sno=student.sno and cno=1)11. 查询选修了全部课程的学生姓名select sname from student where not exists(select * from course where not exists( select * from sc where sno=student.sno and cno=o)11. 查询至少选修了学生95002选修的全部课程的学生的学号select distinct sno from sc A where not exists (select * from sc B where sno=95002 and not exists(select * from sc C where sno=A.sno and cno=B.cno)12. 求没有人选修的课程号cno和cnamecnameselect cno,cname from course C where not exists(select * from sc where o=C.cno )【实例2】在数据库School_will中完成以下查询:1. 查询考分在600分及以上的考生志愿Select *From EwillWhereExno in(select ExnoFrom ExamineWhereExgrade=600)2. 查询考分大于考号为05140200271233的考生的考生信息Select *From ExamineWhereExgrade(select ExgradeFrom ExamineWhereExno= 05140200271233)3. 查询平均分大于校代码为10459的学校信息Select *From SchoolWhereaverageall(select averageFrom SchoolWhereSccode= 10459)4. 查询报考志愿为10730的考生信息Select *From ExamineWhereexists(select *From EwillWhereExno=Examine.Exno and Sccode= 10730)实验习题:【习题1】在School_Will中,用Transact-SQL语句实现下列查询操作。1) 查看所有考生的信息;2) 查看每位考生的考分;3

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论