数据库1数据查询_第1页
数据库1数据查询_第2页
数据库1数据查询_第3页
数据库1数据查询_第4页
数据库1数据查询_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

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

文档简介

数据库1数据查询实验一 数据库查询课程名 数据库原理实验类 验证型称:实验型:实验名数据库查询学时称

4学时实验目的:使学生掌握SQLServerQueryAnalyzer 的使用方法,加深对 SQL和T-SQL语言的查询语句的理解。熟练掌握表的基本查询,连接查询和嵌套查询,以及掌握数据排序和数据分组的操作方法。实验原理:SELECT[ALL|DISTINCT] <目标列表达式>[,<目标列表达式>]⋯FROM<表名或视图名>[,<表名或视图名>]⋯[WHERE<条件表达式>][GROUPBY<列名1>[HAVING<条件表达式>]][orderby<列名2>[ASC|DESC]];实验方法:将查询需求用 T-SQLSQLServerQueryAnalyzer

语言表示;在的输入区中输入T-SQL查询语句;设置QueryAnalyzer的结果区为StandardExecute(标准执行)或ExecutetoGrid(网格执行)方式;发布执行命令,并在结果区中查看查询结果;如果结果不正确,要进行修改,直到正确为止。实验内容:分别用带DISTINCT和不带DISTINCT关键字的SELELCT在student中进行查询.带distinct:Select class_idfrom student不带distinct:select distinct class_idfrom student将teacher表中各教师的姓名、教工号及工资按95%发放的信息,并将工资按95%发放后的列名改为‘预发工资’select teacher_id ,teacher_name ,salary *0.95as 预发工资from teacher查询course表中所有学分大于2并且成绩不及格的学生的信息 .select distinct student .*from student ,course ,scwhere student .student_id =sc.andsc .course_id =courseandcourse .credit >2andsc .grade <60

student_id.course_id查询学分在4~8之间的学生信息.(用between..and和复合条件分别实现)selectfromwhere

distinct student .*student ,course ,scstudent .student_id =sc.andsc.course_id =courseandcourse .creditbetween 4 and8

student_id.course_id从student_course表中查询出学生为“g9940201”,“g9940202”的课程号、学生号以及学分,并按学分降序排列(用in实现)select *from sc ,coursewhere student_id in( 'g9940201' ,'g9940202'andcourse .course_id =sc.course_idorder bycourse .credit desc

)从teacher表中分别检索出姓王的教师的资料,或者姓名的第2个字是远或辉的教师的资料select *from teacherwhere teacher_namelike '王%'orteacher_namelike '_远%'orteacher_namelike '_辉%'查询每个学生及其选修课情况select student_name ,course_namefrom student ,sc ,coursewhere sc .student_id =student .student_idandsc.course_id =course .course_id以student表为主体列出每个学生的基本情况及其选课情况,如果学生没有选课,只输出其基本情况selectstudent.*,course.*fromstudentjoinsconstudent.student_id=sc.student_idleftjoincourseoncourse.course_id=sc.course_id查询选修dep04_s001号课程且成绩在80分以上的学生信息。(分别用连接,in和exists实现)连接:select

student .*from sc ,studentwhere sc .student_id =student .student_idandsc .course_id ='dep04_s001'andsc .grade >80In:select *from studentwhere student_idin(select student_idfrom scwhere course_idandgrade

='dep04_s001'>80)exists:select *from studentwhere exists(select

student_idfromwhere

scstudent .student_id =sc.student_idand course_id ='dep04_s001'andgrade >80)查询所有上计算机基础课程的学生的学号、选修课程号以及分数(分别用连接,in和exists实现)连接:select sc .*from sc ,coursewhere course

.course_name

='

计算机基础

'andcourse

.course_id

=sc.course_idIn:select student_id ,course_id ,gradefrom scwhere course_idin(select course_idfrom coursewhere course_name ='计算机基础 ')exists:select

student_id

,course_id

,gradefrom scwhere

exists(select *from coursewhere course_id =sc.course_idandcourse_name ='计算机基础

')查询选修了课程名为“数据库基础”的学生学号和姓名(分别用连接,in和exists实现)连接:select

student .student_id ,student .student_namefrom sc ,student ,coursewhere course .course_name ='数据库开发技术andsc .student_id =student .student_idandcourse .course_id =sc.course_id

'In:select student_id ,student_namefrom studentwhere student_idin(select student_idfrom scwhere course_id

=(select course_idfrom coursewhere course_name

='

数据库开发技术

'

) )exists:select student_idfrom studentwhere exists(

,student_nameselect *from scwhere sc.student_id

=student .student_idandsc .course_id

=(select course_idwhere course_name

='数据库开发技术'

))查询所有计算机系学生的学号、选修课程号以及分数(分别用连接,in和exists实现)连接:select

sc .*from sc ,department ,studentwhere department .department_nameanddepartment .department_idandsc .student_id =student

='计算机科学 '=student .department_id.student_idIn:select *from scwhere student_idin(select student_idfrom studentwhere department_id

=(select department_idfrom departmentwhere department_name

='

计算机科学

'))exists:select

*from scwhere

exists

(select

* from

studentwhere

student

.student_id

=sc.student_idandstudent

.department_id

=(select

department_idfrom

departmentwhere

department_name

='

计算机科'))13.查询每个dep_04系学生的总成绩、平均成绩,仅显示平均成绩及格的学生的记录。select

student

.student_name

,sum(grade

)

as

总成绩

,avg(grade

)

as平均成绩fromwhere

sc ,scand

student.student_idstudent

,department=student.department_id

.student_id=department

.Department_idand department .department_id ='dep_04'group bystudent .student_id ,student_namehaving avg(grade )>6014.查询“数据库开发技术 ”的平均成绩select avg(grade ) as 数据库开发平均成绩from scwhere course_id in(select course_idfrom coursewhere course_name ='数据库开发技术 ')15.按职称查询教师的平均工资,并按总工资降序排列select profession ,avg(salary ) as 平均工资,sum(salary ) as 总工资from teachergroup byprofessionorder by sum(salary ) desc附录1:教务管理数据库 jwgl结构student表结构列名称数据类长度允许空说明型值Student_Char8否学生id学号Student_Varchar8否学生name姓名SexBit1否性别AgeInt4否年龄Class_idChar6否班级号DepartmeChar6否系编nt_id号Addressvarchar50否家庭住址Course表字段名称数据类长度允许空说明型Course_iChar10否课程号dCourse_nVarchar20否课程名ame称Book_idChar15否书标识SC表字段名称数据类长度允许空说明型Course_iChar10否课程号dStudent_Varchar8否学生号idgradeTinyint否成绩creditTinyint否学分Teacher表字段名称数据类长度允许空说明型Teacher_Char9否教师编id号Teacher_nvarcha8否教师姓namer名DepartmeChar6否所在系nt_id号ProfessiNvarcha16职称onrSexBit否性别phoneNvarcha15是电话radressNchar40是住址SalaryNumeric7.2是工资BirthSmallda否出生日tetime期Postalco Numeric 6,0 是 邮编deBook表字段名数据类长度允许说明称型空Book_idChar13否书号Book_naVarchar30否书名mePu

温馨提示

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

评论

0/150

提交评论