数据库1数据查询

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验一数据库查询

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

实验目的:

使学生掌握SQL Server Query Analyzer的使用方法,加深对SQL和T-SQL语言的查询语句的理解。熟练掌握表的基本查询,连接查询和嵌套查询,以及掌握数据排序和数据分组的操作方法。实验原理:

SELECT [ALL|DISTINCT] <目标列表达式>[,<目标列表达式>]…

FROM <表名或视图名>[,<表名或视图名>]…

[WHERE <条件表达式>]

[GROUP BY <列名1> [HA VING <条件表达式>]]

[order by <列名2> [ASC|DESC]];

实验方法:

将查询需求用T-SQL语言表示;在SQL Server Query Analyzer的输入区中输入T-SQL查询语句;设置Query Analyzer的结果区为Standard Execute(标准执行)或Execute to Grid(网格执行)方式;发布执行命令,并在结果区中查看查询结果;如果结果不正确,要进行修改,直到正确为止。实验内容:

1.分别用带DISTINCT和不带DISTINCT关键字的SELELCT在student中进行查询.

带distinct:

Select class_id

student

from

不带distinct:

select distinct class_id

from student

2.将teacher表中各教师的姓名、教工号及工资按95%发放的信息,并将工资按95%发放后的列名改为‘预发工资'

select teacher_id,teacher_name,salary*0.95

as预发工资

teacher

from

3.查询course表中所有学分大于2并且成绩不及格的学生的信息.

select distinct student.*

from student,course,sc

where student.student_id=sc.student_id

and sc.course_id=course.course_id

and course.credit>2

and sc.grade<60

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

from student,course,sc

where student.student_id=sc.student_id

and sc.course_id=course.course_id

and course.credit

8

and 4 between

5.从student_course表中查询出学生为“”,“”的课程号、g9940202g9940201学生号以及学分,并按学分降序排列(用in实现)

select*

from sc,course

where student_id in('g9940201','g9940202')

and course.course_id=sc.course_id

order by course.credit desc

6.从teacher表中分别检索出姓王的教师的资料,或者姓名的第2个字是远或辉的教师的资料select*

from teacher

where teacher_name

like'王%'

or teacher_name

like'_远%'

or teacher_name

like'_辉%'

7.查询每个学生及其选修课情况

course_name

, student_name select

from student, sc,course

where sc.student_id=student.student_id

and sc.course_id=course.course_id

8.以student表为主体列出每个学生的基本情况及其选课情况,如果学生没有选课,只输出其基本情况

select student.*,course.*

from student join sc

on student.student_id=sc.student_id

left join course

on course.course_id=sc.course_id

9.查询选修dep04_s001号课程且成绩在80分以上的学生信息。(分别用连接,in和exists实现)连接:

select student.*

from sc,student

where sc.student_id=student.student_id

and sc.course_id='dep04_s001'

and sc.grade>80

In:

select*

from student

where student_id

in(

select student_id

from sc

where course_id='dep04_s001'

and grade>80)

exists:

select*

from student

where exists(

select student_id

from sc

where student.student_id=sc.student_id

and course_id='dep04_s001'

)

80> grade and

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

连接:

select sc.*

from sc,course

where course.course_name='计算机基础'

and course.course_id=sc.course_id

In:

select student_id,course_id,grade

相关文档
最新文档