您好,欢迎来到微智科技网。
搜索
您的当前位置:首页实验报告13p

实验报告13p

来源:微智科技网
欢迎阅读 实验名称:SQL数据查询 一、实验目的: 数据查询语句是SQL语句的重要组成部分,合理使用数据查询语句,可以极大的简化应用程序编制、快速的定位分析数据库系统的故障,查询语句是编程人员与数据库管理人员必不可少的工具,通过实验达到以下目的: (1)加深学生对查询语句基本概念的理解与掌握,最终达到灵活应用。 (2)掌握SELECT语句的基本语法。 (3)掌握简单单表查询、连接查询、嵌套查询。 (4)学会使用聚函数和进行分组查询。 二、实验内容: 1、单表查询: 2、连接查询 3、嵌套查询 三、实验环境 Windowsxp系统SQLServer2000服务器 四、程序源码与运行结果 1、单表查询: 设计查询语句完成对*、distinct、like(%和_)、in、notin、betweenand、orderby、groupby等的应用。 (1)检索出学生信息表中所有女生的记录。 Select*fromstudentwheresex=’女’ (2)从选课成绩表中检索出所有学生的成绩,并去除重复值。 selectdistinctgradefromcs (3)从课程表中检索出全部数据的信息。 select*fromcoursewherecnamelike'数据%' (3)从学生信息表中检索出姓王的学生的信息。 select*fromstudentwheresnamelike'王_' (4)从成绩表中找出成绩等于60分的学生的性别。 selectsexfromstudentwheresnoin(selectsnofromcswheregrade='60') (5)找出不在成绩表中的学生的所有信息。 select*fromstudentwheresnonotin(selectsnofromcs) 欢迎阅读 (6)在成绩表中找出成绩从70到85分的所有信息。 select*fromcswheregradebetween70and85 (7)将学生表中的所有学生的年龄按升序排列。 select*fromstudentorderbyage (8)检索出没门课程的平均分。 selectcno,avg(grade)fromcsgroupbycno 2、连接查询 设计查询语句,分别用两种方式(where+连接条件和join…on)表示连接条件实现连接查询 (1)找出成绩大于90分的姓名和他们所在的专业。(where+连接条件) selectdistinctsname,deptfromstudent,cswhere(grade>90) (2)找出成绩大于85分的姓名和他们所在的专业。(join…on) selectdistinctsname,dept fromstudentjoincson(student.sno=cs.sno)where(grade>85) 3、嵌套查询 具体要完成的任务如下: 1. 查询全体学生的学号与姓名 selectsno,snamefromstudent 2. 查询全体学生的全部信息,并为学生表命名别名。 select*fromstudentW,courseE,csBwhereW.sno=B.snoandE.cno=B.cno 3. 查全体学生的出生年份,并为年份加上标题 select出生日期fromstudent 4. 查询选修了课程的学生学号,要求消除重复行 selectsnofromstudentwheresnoin(selectsnofromcs) 5. 查询所有年龄在20岁以下的学生姓名及其年龄。 selectsname,agefromstudentwhereage<20 6. 查询年龄在20~23岁的学生信息(要求至少使用两种方式完成查询) select*fromstudentwhereagebetween20and23(第一种) select*fromstudentwhere(age>=20andage<=23)(第二种) 7. 使用IN关键字查询信息系(IS)、数学系(MA)和计算机科学系(CS)的学生 select*fromstudent wheresnoin(selectsnofromstudentwheredept='IS') select*fromstudent wheresnoin(selectsnofromstudentwheredept='MA') 欢迎阅读 select*fromstudent wheresnoin(selectsnofromstudentwheredept='CS') 8. 查询既不是信息系、数学系,也不是计算机科学系的学生的姓名和性别。 selectsname,sexfromstudent wheredept!='MA'anddept!='CS'anddept!='IS' 9. 查询所有姓刘学生的姓名、学号和性别。 selectsname,sno,sexfromstudentwheresnamelike'刘%' 10. 查询名字中第2个字为\"阳\"字的学生的姓名和学号。 selectsname,snofromstudentwheresnamelike'_阳' 11. 查询DB_Design课程的课程号和学分(先在Course表中插入“DB_Design”课程信息)。 selectcname,scorefromcoursewherecname='DB_Design' 12. 查询没有考试成绩的学生学号和课程号。 selectsno,cnofromcswheregradeisnull 13. 查询计算机系年龄在20岁以下的学生姓名。 selectsnamefromstudentwhereage<20anddept='CS' 14. 查询全体学生情况,查询结果按所在系的系号升序排列,同一系中的学生按年龄降序排列。 select*fromstudentorderbydept,agedesc 15. 查询学生总人数。 selectcount(sno)fromstudent 16. 查询选修了课程的学生人数。 selectcount(distinctsno)fromcs 17. 计算1号课程的学生平均成绩。 selectavg(grade)fromcswherecno='C001' 18. 查询选修1号课程的学生最高分数。 selectmax(grade)fromcswherecno='C001' 19. 求各个课程号及相应的选课人数。 selectcno,count(sno)fromcsgroupbycno 20. 查询选修了3门以上课程的学生学号(提示用Having字句) selectsno,count(cno)fromcsgroupbysnohavingcount(cno)>=3 21、查询有3门以上课程是90分以上的学生的学号及(90分以上的)课程数。 selectsno,count(cno)fromcswheregrade>=90 groupbysnohavingcount(cno)>=3 欢迎阅读 22、查询全体学生与选课表的笛卡尔积。 select*fromstudentcrossjoincourse 23、查询每个学生及其选修课程的情况。 selectdistinct*fromstudentcrossjoincswherestudent.sno=cs.sno 24、查询每个学生及其选修课程的情况(去掉重复属性) selecta.sno,sname,sex,dept,age,b.cno,cname,score,c.grade fromstudenta,courseb,cscwherea.sno=c.snoandb.cno=c.cno 25、查询某门课程考试成绩相同的学生学号和课程信息 selecta.sno,b.cno,b.cname,b.scorefromcsa,courseb wherea.cno=b.cnoand(selectcount(*)fromcswherecno=cnoandgrade=grade)>=2 26、查询每个学生的选修课程包括没有选修课程的学生(外连接) select*fromstudenta,csbwherea.sno*=b.sno 27、查询每个学生的选修课程包括没有被学生选修的课程(外连接) select*fromstudent,cswherestudent.sno=*cs.sno 28、查询每个学生的选修课程即包括没有被学生选修的课程又包括没有被学生选修的课程(全连接) select*fromstudentfulljoincsonstudent.sno=cs.sno 29、查询选修2号课程且成绩在90分以上的所有学生的学号、姓名 selectsno,snamefromstudent wheresnoin(selectsnofromcswheregrade>='90'andcno='C002') 30、查询每个学生的学号、姓名、选修的课程名及成绩 selectstudent.sno,sname,cname,gradefromstudent,course,cs where(student.sno=cs.sno)and(course.cno=cs.cno) 31、查询与“张三”在一个系学习的学生(IN) select*fromstudent wheredeptin(selectdeptfromstudentwheresname='张三') 32、查询选修了课程名为“信息系统”的学生学号和姓名。 selectsno,snamefromstudent wheresnoin(selectsnofromcswherecnoin(selectcnofromcoursewherecname='信息系统')) 33、查询与“张三”在同一个系学习的学生 select*fromstudent wheredeptin(selectdeptfromstudentwheresname='张三') 34、查询选修了课程1或者选修了课程2的学生(要求消除重复组UNION) 欢迎阅读 (selectsnofromcswherecno='C001')UNION (selectsnofromcswherecno='C002') 35、查询选修了课程1或者选修了课程2的学生(要求不消除重复组UNIONALL) (selectsnofromcswherecno='C001')UNIONall (selectsnofromcswherecno='C002') 五、实验总结 通过本次试验,掌握了使用SQL语句查询的技巧。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 7swz.com 版权所有 赣ICP备2024042798号-8

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务