《年整理数据库嵌套查询实验报告》由会员分享,可在线阅读,更多相关《年整理数据库嵌套查询实验报告(7页珍藏版)》请在金锄头文库上搜索。
1、年整理数据库嵌套查询实验报告实验三:数据库的嵌套查询实验实验目的 :加深对嵌套查询语句的理解。实验内容:使用 IN、比较符、ANY 或 ALL 和 EXISTS 操作符进行嵌套查询操作。实验步骤:一 .使用带 N IN 谓词的子查询1.查询与刘晨在同一个系学习的学生的信息: 比较 select * from student where sdept in(select sdept from student where sname=刘晨) 与: select * from student where sdept =(select sdept from student where sname=刘晨)
2、 的异同比较: select * from student where sdept =(select sdept from student where sname=刘晨) and sname 刘晨 与: select S1.* from student S1, student S2 where S1.sdept=S2.sdept and S2.sname=刘晨的异同 2.查询选修了课程名为信息系统 的学生的学号和姓名: 比较 select sno, sname from student where sno in (select sno from sc where cno in(select c
3、no from course where cname=信息系统) 与: select sno, sname from student where sno in 3.查询选修了课程1和课程2的学生的学号: select sno from student where sno in (select sno from sc where cno=1) and sno in (select sno from sc where cno=2)比较: 查询选修了课程1或课程2的学生的 sno: select sno from sc where cno=1 or cno=2比较连接查询:二 .使用带比较运算的子查
4、询 4.查询比刘晨年龄小的所有学生的信息: select * from student where sageIS 6.查询其他系中比信息系(IS)学生年龄都小的学生姓名和年龄: select sname, sage from student where sage IS 7.查询与计算机系(CS)系所有学生的年龄均不同的学生学号, 姓名和年龄: select sno,sname,sage from student where sageall (select sage from student where sdept=CS) 四 .使用带 s Exists 谓词的子查询和相关子查询8.查询与其他所
5、有学生年龄均不同的学生学号, 姓名和年龄: 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 from student where exists (select * from sc where sno=student.sno and cno=1) 10.查询没有选修了 1 号课程的学生姓名: select sname from student whe
6、re not exists (select * from sc where sno=student.sno and cno=1) 11.查询选修了全部课程的学生姓名: SQL Server 中:11.查询至少选修了学生 95002 选修的全部课程的学生的学号: SQL Server 中: select distinct sno from sc A where not exists14*.查询每个学生的课程成绩最高的成绩信息(sno,cno,grade): select * from sc A where grade= (select max(grade) from sc where sno=A
7、.sno )思考: 如何查询所有学生都选修了的课程的课程号 cno? select cnofrom scgroup by cnohaving count(*)=(select count(*) from student) 中心突出,论据充足。很成熟的语言。实验五数据库的嵌套查询实验1、实验目的本实验的目的是使学生进一步掌握SQL Server查询分析p 器的使用方法,加深SQL语言的嵌套查询语句的理解2、实验时数2学时3、实验内容本实验的主要内容是:在SQL Server查询分析p 器中使用IN、比较符、ANY或ALL和EXISTS操作符进行嵌套查询操作。具体完成以下例题。将它们用SQL语句表
8、示,在学生数据库中实现其数据嵌套查询操作。2、查询1号课程的成绩高于刘晨的1号课程成绩的学生学号和成绩 select SC.Sno,Grade from SC,Student where Cno=1 and SC.Sno=Student.Sno and Gradeall(select Grade from SC,Student where SC.Sno=Student.Sno and Sname=刘晨)3、查询其他系中比cs系某一学生年龄小的学生(即年龄小于计算机系年龄最大者的学生) select * from Student where Sdept!=cs and Sage4、查询其他系中比
9、cs系所有学生年龄都小的学生 select * from Student where Sdept!=cs and Sage5、查询选修了2号课程的学生姓名 select Sname from Student where Sno in (select Student.Sno from SC,Student where SC.Sno=Student.Sno and Cno=2)6、查询没有选修2号课程的学生姓名 elect Sname from Student where exists (select SC.Sno from SC,Student where Cno!=2 group by SC.
10、Sno) 7、查询选修了全部课程的学生姓名 Select * from SC where Cno=all(select Cno from Course)8、求至少选修了学号为“00215122”的学生所选修全部课程的学生学号和姓名 select Sno,Sname from Student where Sno in (select Student.Sno from Student,SC where Student.Sno=SC.Sno and Cno=any(select Cno from SC where Sno=00215122)4、实验方法将查询需求用SQL语言表示:在SQL Server查询分析p 器的输入区中输入SQL查询语句:设置查询分析p 器的结果区为Standard Execute(标准执行)或Execute to Grid(网格执行)方式;发布执行命令,并在结果区中查看查询结果;如果结果不正确,要进行修改,直到正确为止。5、实验心得体会本次实验还是进行查询操作,只是在第四次试验的基础上加入了嵌套,嵌套查询是在我们以后进行软件开发涉及数据库常用的查询操作,通过实验有助于我们对嵌套查询的理解,对我们熟练的使用SQL查询语句进行查询操作有很大帮助。第 页 共 页