数据库实验1-6参考答案 下载本文

内容发布更新时间 : 2024/12/23 0:22:46星期一 下面是文章的全部内容请认真阅读。

选课表:SC Sno 9512101 9512103 9512101 9512103 9521101 9521102 9521103 9531101 9531102 9512101 9531102 9512101 9512101 Cno C03 C03 C05 C05 C05 C05 C05 C05 C05 C01 C01 C02 C04 Grade 95 51 80 60 72 80 45 81 94 NULL NULL 87 76

2、完成下列查询

(1)查询全体学生的信息。

select * from student

(2)查询“信息系”学生的学号,姓名和出生年份。

select Sno,Sname,2009-Sage as Birthyear from student where Sdept='信息系'

(3)查询考试不及格的学生的学号。

select Distinct Sno from SC where Grade<60

(4)查询无考试成绩的学生的学号和相应的课程号。

select Sno,Cno from SC where Grade is null

(5)将学生按年龄升序排序。

select * from student order by Sage asc

(6)查询选修了课程的学生的学号和姓名。

11

Select Sno,Sname from Student Where Sno in (Select Sno From Sc) 或:

select distinct student.Sno,Sname from student,SC where student.Sno=SC.Sno

(7)查询年龄在20-23岁之间的学生的姓名,系,年龄。

select Sname,Sage,Sdept from student where Sage between 20 and 23

(8)查询同时选修了“计算机导论”,“高等数学”课程的学生的学号,姓名。

select student.Sno,Sname from student where NOT EXISTS( select* from course where

Cname in('高等数学','计算机导论') AND NOT EXISTS(select * from SC where Sno=student.Sno and Cno=course.Cno)) 或:

select student.Sno,Sname from student,sc,Course where student.sno=sc.sno and sc.cno=course.cno and cname='高等数学' and student.sno in (select sno from sc where cno=(select cno from course where cname='计算机导论')) 或:

select student.Sno,Sname from student where sno in (select sno from sc where cno=(select cno from course where cname='高等数学')) and sno in (select sno from sc where cno=(select cno from course where cname='计算机导论'))

(9)查询姓“张”的学生的基本信息。

select * from student where Sname like '张%'

(10)查询“95211”班学生的选课情况,要求输出学号,姓名,课程名,成绩,按照学号升序排序。

select student.Sno,Sname,Cname,Grade from student,SC,course where student.Sno=SC.Sno and course.Cno=SC.Cno

and student.Sno like '95211%' order by student.Sno 或:

select student.Sno,Sname,Cname,Grade from student,SC,course where student.Sno=SC.Sno and course.Cno=SC.Cno

and left(student.sno,5)= '95211' order by 1

(11)查询选修了课程的学生的总人数。

12

select count(distinct sno) from SC

(12)查询选修了“C05”课程的的学生成绩单,要求输出学号,姓名,成绩,结果按班级升序,成绩降序排列。

select student.Sno,Sname,Grade from student,SC where student.Sno=SC.Sno and Cno='C05' order by left(student.Sno,5) asc,Grade desc

(13)统计各门课程的成绩,要求输出课程代号,课程名,平均成绩,选修人数。(成绩为NULL值的不统计)

select course.Cno,Cname,avg(Grade),count(Sno) from course,Sc where course.Cno=SC.Cno and Grade is not null group by Course.Cno,Cname

(14)统计各门课程的不及格人数,要求输出课程代号,课程名,不及格人数。

select Course.Cno,Cname,count(Sno) from SC,Course where SC.Cno=course.Cno and Grade<60 group by Course.Cno,Cname

(15)查询选修平均成绩在75分以上的学生的学号,姓名,所在系。

select sc.sno,sname,sdept from student,sc where student.sno=sc.sno group by sc.sno,sname,sdept having avg(grade)>75 或:

select sno,sname,sdept from student where Sno in (Select Sno From SC Group By Sno Having Avg(Grade)>75)

(16)查询与“王大力”同一个系的学生的基本信息

select * from student where sdept in(select sdept from student where sname='王大力')

(17)查询选修平均分高于所有学生平均分的学生的学号,并按学号升序排列。

select student.Sno from student,SC where student.Sno=SC.Sno group by student.Sno having avg(Grade)>(select Avg(Grade) from SC) order by student.Sno asc

(18)查询未选修“VB”或“数据库基础”两门课的学生的学号,姓名,系名。(要

13

求用嵌套查询)

select sno,sname,sdept from student where sno not in(select sno from sc where cno in(select cno from course where cname in('VB','数据库基础'))) 或:

select sno,sname,sdept from Student where exists(

select * from Course where cname='VB' and not exists(

select * from SC where sno=Student.sno and cno=Course.cno and Course.cname!='数据库基础'))

(19)查询选修了全部课程的学生的学号,姓名,系名。

select sno,sname,sdept from student

where not exists(select * from course where not exists

(select * from sc where sno=student.sno and cno=course.cno ) ) 或:

select sno,sname,sdept from student

where sno in (select sno from sc group by sno having count(cno)=(select coount(*) from course))

(20)输出“高等数学”课程成绩前三名的学生的学号,姓名,系名

select top 3 student.sno,sname,sdept from student,sc,course where student.sno=sc.sno and sc.cno=course.cno and cname='高等数学' order by grade desc

四、实验小结

五、评阅成绩

实验预习20% 实验过程20% 实验结果30% 实验报告30% 总成绩 14

实验四 数据更新

一、实验目的

1、掌握SQL语言的数据更新操作

2、掌握SQL Server 2000企业管理器的数据导入和导出功能

二、实验预习

1、数据插入语句格式:

2、数据修改语句格式:

3、数据删除语句格式: 4、SQL Server中可进行批量数据导入和导出,可支持哪些格式的数据导入导出?(举常见格式类型)

三、实验内容和要求

(执行操作后,将语句填写在下面的空白处) 1、插入数据

(1)在学生表Student中插入数据:

Sno:9512102 Sname:刘晨 Ssex:男 Sage:20 Sdept:计算机系 insert into Student

values ('9512102','刘晨','男',20,'计算机系')

(2)在课程表Course中插入数据:

Cno:C06 Cname:数据结构 Ccredit:5 Semster:4 insert into Course(cno,cname,ccredit,semster) values ('C06','数据结构',5,4) 或:

insert into Course values ('C06','数据结构',5,4,null)

15