数据库1数据查询

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

实验一数据库查询课程名称:数据库原理实验实验类型:验证型实验名称数据库查询学时4学时实验目的:使学生掌握SQLServerQueryAnalyzer的使用方法,加深对SQL和T-SQL语言的查询语句的理解。熟练掌握表的基本查询,连接查询和嵌套查询,以及掌握数据排序和数据分组的操作方法。实验原理:SELECT[ALL|DISTINCT]目标列表达式[,目标列表达式]…FROM表名或视图名[,表名或视图名]…[WHERE条件表达式][GROUPBY列名1[HAVING条件表达式]][orderby列名2[ASC|DESC]];实验方法:将查询需求用T-SQL语言表示;在SQLServerQueryAnalyzer的输入区中输入T-SQL查询语句;设置QueryAnalyzer的结果区为StandardExecute(标准执行)或ExecutetoGrid(网格执行)方式;发布执行命令,并在结果区中查看查询结果;如果结果不正确,要进行修改,直到正确为止。实验内容:1.分别用带DISTINCT和不带DISTINCT关键字的SELELCT在student中进行查询.带distinct:Selectclass_idfromstudent不带distinct:selectdistinctclass_idfromstudent2.将teacher表中各教师的姓名、教工号及工资按95%发放的信息,并将工资按95%发放后的列名改为‘预发工资’selectteacher_id,teacher_name,salary*0.95as预发工资fromteacher3.查询course表中所有学分大于2并且成绩不及格的学生的信息.selectdistinctstudent.*fromstudent,course,scwherestudent.student_id=sc.student_idandsc.course_id=course.course_idandcourse.credit2andsc.grade604.查询学分在4~8之间的学生信息.(用between..and和复合条件分别实现)selectdistinctstudent.*fromstudent,course,scwherestudent.student_id=sc.student_idandsc.course_id=course.course_idandcourse.creditbetween4and85.从student_course表中查询出学生为“g9940201”,“g9940202”的课程号、学生号以及学分,并按学分降序排列(用in实现)select*fromsc,coursewherestudent_idin('g9940201','g9940202')andcourse.course_id=sc.course_idorderbycourse.creditdesc6.从teacher表中分别检索出姓王的教师的资料,或者姓名的第2个字是远或辉的教师的资料select*fromteacherwhereteacher_namelike'王%'orteacher_namelike'_远%'orteacher_namelike'_辉%'7.查询每个学生及其选修课情况selectstudent_name,course_namefromstudent,sc,coursewheresc.student_id=student.student_idandsc.course_id=course.course_id8.以student表为主体列出每个学生的基本情况及其选课情况,如果学生没有选课,只输出其基本情况selectstudent.*,course.*fromstudentjoinsconstudent.student_id=sc.student_idleftjoincourseoncourse.course_id=sc.course_id9.查询选修dep04_s001号课程且成绩在80分以上的学生信息。(分别用连接,in和exists实现)连接:selectstudent.*fromsc,studentwheresc.student_id=student.student_idandsc.course_id='dep04_s001'andsc.grade80In:select*fromstudentwherestudent_idin(selectstudent_idfromscwherecourse_id='dep04_s001'andgrade80)exists:select*fromstudentwhereexists(selectstudent_idfromscwherestudent.student_id=sc.student_idandcourse_id='dep04_s001'andgrade80)10.查询所有上计算机基础课程的学生的学号、选修课程号以及分数(分别用连接,in和exists实现)连接:selectsc.*fromsc,coursewherecourse.course_name='计算机基础'andcourse.course_id=sc.course_idIn:selectstudent_id,course_id,gradefromscwherecourse_idin(selectcourse_idfromcoursewherecourse_name='计算机基础')exists:selectstudent_id,course_id,gradefromscwhereexists(select*fromcoursewherecourse_id=sc.course_idandcourse_name='计算机基础')11.查询选修了课程名为“数据库基础”的学生学号和姓名(分别用连接,in和exists实现)连接:selectstudent.student_id,student.student_namefromsc,student,coursewherecourse.course_name='数据库开发技术'andsc.student_id=student.student_idandcourse.course_id=sc.course_idIn:selectstudent_id,student_namefromstudentwherestudent_idin(selectstudent_idfromscwherecourse_id=(selectcourse_idfromcoursewherecourse_name='数据库开发技术'))exists:selectstudent_id,student_namefromstudentwhereexists(select*fromscwheresc.student_id=student.student_idandsc.course_id=(selectcourse_idfromcoursewherecourse_name='数据库开发技术'))12.查询所有计算机系学生的学号、选修课程号以及分数(分别用连接,in和exists实现)连接:selectsc.*fromsc,department,studentwheredepartment.department_name='计算机科学'anddepartment.department_id=student.department_idandsc.student_id=student.student_idIn:select*fromscwherestudent_idin(selectstudent_idfromstudentwheredepartment_id=(selectdepartment_idfromdepartmentwheredepartment_name='计算机科学'))exists:select*fromscwhereexists(select*fromstudentwherestudent.student_id=sc.student_idandstudent.department_id=(selectdepartment_idfromdepartmentwheredepartment_name='计算机科'))13.查询每个dep_04系学生的总成绩、平均成绩,仅显示平均成绩及格的学生的记录。selectstudent.student_name,sum(grade)as总成绩,avg(grade)as平均成绩fromsc,student,departmentwheresc.student_id=student.student_idandstudent.department_id=department.Department_idanddepartment.department_id='dep_04'groupbystudent.student_id,student_namehavingavg(grade)6014.查询“数据库开发技术”的平均成绩selectavg(grade)as数据库开发平均成绩fromscwherecourse_idin(selectcourse_idfromcoursewherecourse_name='数据库开发技术')15.按职称查询教师的平均工资,并按总工资降序排列selectprofession,avg(salary)as平均工资,sum(salary)as总工资fromteachergroupbyprofessionorderbysum(salary)desc附录1:教务管理数据库jwgl结构student表结构列名称数据类型长度允许空值说明Student_idChar8否学生学号Student_nameVarchar8否学生姓名SexBit1否性别AgeInt4否年龄Class_idChar6否班级号Department_idChar6否系编号Addressvarchar50否家庭住址Course表字段名称数据类型长度允许空说明Course_idChar10否课程号Course_nameVarchar20否课程名称Book_idChar15否书标识SC表字段名称数据类型长度允许空说明Course_idChar10否课程号Student_idVarchar8否学生号gradeTinyint否成绩creditTinyint否学分Teacher表字段名称数据类型长度允许空说明Teacher_idChar9否教师编号Teacher_namenvarchar8否教师姓名Department_idChar6否所在系号ProfessionNvarchar16职称SexBit否性别phoneNvarchar15是电话adressNchar40是住址SalaryNumeric7.2是工资BirthSmalldatetime否出生日期PostalcodeNumeric6,0是邮编Book表字段名称数据类型长度允许空说明Book_idChar13否书号Book_nameVarchar30否书名Publish_companyVarchar50否出版社Authornvarchar8是作者PriceNumeric5.2是价格Class表字段名称数据类型长度允许空说明Class_idChar6否班级号MonitorVarchar8是班主任姓名Class_course表字段名称数据类型长度允许空说明Class_idChar6否班级号Course_idchar10否课程号Department表字段名称数据类型长度允许空说明Department_idChar6否系编号Department_namenvarchar20否系名称

1 / 15
下载文档,编辑使用

©2015-2020 m.111doc.com 三一刀客.

备案号:赣ICP备18015867号-1 客服联系 QQ:2149211541

×
保存成功