北邮大三数据库实验三数据查询实验.doc_第1页
北邮大三数据库实验三数据查询实验.doc_第2页
北邮大三数据库实验三数据查询实验.doc_第3页
北邮大三数据库实验三数据查询实验.doc_第4页
北邮大三数据库实验三数据查询实验.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

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

文档简介

实验三 数据查询实验实验目的通过对实验二中建立的学生数据库关系表和视图的各种查询的操作,加深对SQL查询语言的了解,掌握相关查询语句的语法和使用方法。实验步骤1、在简单查询实验中,在sql语句完成以下查询操作:(1)查询“数据库原理”课程的学分; select credit from course where course_name=数据库原理(2)查询选修了课程编号为“C01”的学生的学号和成绩,并将成绩按降序输出;select student_id,scorefrom scwhere course_id=C01order by score desc一共25条查询结果(3) 查询学号为“31401”的学生选修的课程编号和成绩;select course_id,scorefrom sc where student_id=31401一共3条查询结果(4)查询选修了课程编号为“C01”且成绩高于85分的学生的学号和成绩。select student_id ,scorefrom sc where course_id=C01 and score 85一共5条查询结果2、在多表连接的查询实验中,在SQLSERVER提供的交互式语言环境下用Transact SQL语句完成以下查询操作:(1)查询选修了课程编号为“C01”且成绩高于85分的学生的学号、姓名和成绩;select sc.student_id ,student_name,scorefrom sc,student where sc.course_id =C01 and sc.student_id =student.student_id and sc.score 85一共5条查询结果(2) 查询所有学生的学号、姓名、选修的课程名称和成绩;select sc.student_id,student_name,course_name,scorefrom sc,student,course where sc.student_id =student.student_id and sc.course_id =course.course_id 一共142条查询结果3、在复杂查询实验中,用 SQL语句完成以下查询操作:(1)查询至少选修了三门课程的学生的学号和姓名;select sc.student_id,student.student_name,COUNT(sc.course_id )from sc,student where sc.student_id =student.student_id group by sc.student_id,student_name having COUNT(sc.course_id )=3一共39条查询结果(2) 查询所有学生的学号和他选修课程的最高成绩,要求他的选修课程中没有成绩为空的。select sc .student_id,student_name,max(score ) as max_scorefrom sc,studentwhere sc.student_id in(select sc .student_idfrom sc,student where sc.student_id =student.student_id)except(select sc .student_idfrom sc,student where sc.student_id =student.student_id and score is NULL)and sc.student_id =student.student_idgroup by sc .student_id,student_name一共52条查询结果,成绩为NULL的30401和30402号同学有成绩为空,不在查询结果中4、 在嵌套查询实验中,在SQLServer提供的交互式语言环境下用iSQL语句完成以下查询操作,要求写嵌套查询语句:(1) 查询选修了数据库原理的学生的学号和姓名;select student_id ,student_name from studentwhere student_id in (select student_id from sc,course where sc.course_id =course.course_id and course_name=数据库原理 )一共23条查询结果(2) 查询没有选修数据库原理的学生的学号和姓名;select student_id ,student_name from studentwhere student_id not in (select student_id from sc,course where sc.course_id =course.course_id and course_name=数据库原理 )一共31条查询结果(3)查询至少选修了学号为“31401”的学生所选修的所有课程的学生的学号和姓名。select student_id ,student_name from student as Swhere not exists( (select course_id from sc where sc.student_id =31401) except (select course_id from sc as T ,student as R where T.student_id =R.student_id and S.student_name=R.student_name )一共20条查询结果视图查询:对实验二建立的视图进行相关的查询操作,如:(1) 查询选修了课程编号为“C01”的学生的学号和成绩;select student_id,student_namefrom studentviewwhere course_id=C01一共25条查询结果(2) 查询所有学生的学号、姓名、选修的课程名称和成绩;select student_id,student_name,course_id,scorefrom studentview一共142条查询结果(3)查询选修了数据库原理的学生的学号和姓名。select student_id,student_name,course_namefrom studentviewwhere course_name=数据库原理一共23条查询结果实验总结:本次实验中遇到的一些问题:1、 首先遇到的问题是Sybase和SQLServer都不能用了,不管怎么重启都不行,查了一些资料是因为IP地址被修改了所以连接不上,在对Sybase做了一些设置以后还是用不了,但是SQLServer可以用localhost连接服务器,所以本次实验只好用SQLServer2、 在查询所有学生的学号和他选修课程的最高成绩,要求他的选修课程中没有成绩为空的这个查询操作的时候出现了问题,我开始的时候想到把所有同学除去成绩有空同学就可以得到正确的查询结果,但是实际上这样做的结果是不正确的,我把except之后的

温馨提示

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

评论

0/150

提交评论