日期:2014-05-18  浏览次数:20582 次

冰天雪地祼跪求答!!!!!!
老师给的题目如下,请高手们指点。
/*************************************************
问题描述:
已知关系模式:
  学员表student (学号std_no,姓名std_name,性别sex,班号class_no)
1 张三 男 1001
2 李四 男 1001
3 王五 女 1002
4 陈六 男 1002
  课程表course (课程号course_no,课程名course_name,授课老师teacher)
1001 JAVA 李明
1002 SQL 杨芳
1003 C# 刘宏
  考试表score (学号std_no,课程号course_no,成绩score) 
1 1001 90
1 1002 100
1 1003 80
2 1001 80
2 1002 40
2 1003 56
3 1001 70
3 1002 50

**************************************************/
--创建数据库
if exists(select name from sys.databases where name='accp')
drop table accp
create database accp
on primary
(
name = 'accp_data.mdf',
filename = 'D:\temp\DataBase\ch3\accp_data.mdf',
size = 5MB
)
log on
(
name = 'accp_log.ldf',
filename = 'D:\temp\DataBase\ch3\accp_log.ldf',
size = 3MB
)
go
--创建学员表student并插入数据
if exists(select name from sys.objects where name = 'student')
drop table student
create table student
(
std_no int not null identity(1,1) primary key,
std_name varchar(10) not null,
sex bit not null,
class_no int not null
)
go
insert into student
select '张三',0,1001 union
select '李四',0,1001 union
select '王五',1,1002 union
select '陈六',0,1002
go
select * from student
--创建课程表course并插入数据
if exists(select name from sys.objects where name = 'course')
drop table course
create table course
(
course_no int not null primary key,
course_name varchar(20) not null,
teacher varchar(10) not null
)
go
insert into course
select 1001,'JAVA','李明' union
select 1002,'SQL','杨芳' union
select 1003,'C#','刘宏'
go
select * from course
--创建考试表score并插入数据
if exists(select name from sys.objects where name = 'score')
drop table score
create table score
(
std_no int foreign key references student(std_no),
course_no int foreign key references course(course_no),
mark float
)
go
insert into score
select 1,1001,90 union
select 1,1002,100 union
select 1,1003,80 union
select 2,1001,80 union
select 2,1002,40 union
select 2,1003,56 union
select 3,1001,70 union
select 3,1002,50
go
select * from score
--1.查询有参加课程名称为'JAVA'考试的学员学号和姓名

--2.查询没有参加课程编号为1003考试的学员姓名

--3.查询参加了全部课程考试的学员姓名

--4. 查询没有考试的学员人数

--5. 找出没有参加"李明"老师讲授课程考试的所有学生姓名

--6. 列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩

--7. 列出既学过1001号课程,又学过1002号课程的所有学生姓名

--8. 列出课程1001成绩比1002成绩高的所有学生的学号

--9. 列出课程1001成绩比1002成绩高的所有学生的学号及其1001和1002的成绩

------解决方案--------------------
--1.查询有参加课程名称为'JAVA'考试的学员学号和姓名 
select a.std_no,
a.std_name
from student a
inner join score b on a.std_no = b.std_no
inner join course c on c.course_no = b.course_no
where c.course_no = 1001

--2.查询没有参加课程编号为1003考试的学员姓名
select std_name
from student
where std_name not in 
(select a.std_name
from student a
inner join score b on a.std_no = b.std_no
inner join course c on c.course_no = b.course_no
where c.course_no = 1003)

--3.查询参加了全部课程考试的学员姓名
select std_name
from student
where std_no in 
(select std_no from(
select std_no,
sum(case when course_no = 1001 or course_no= 1002 or course_no = 1003 then 1 else 0 end) as 'count'
 from score
group by std_no
having count(*) = 3) c)

--4. 查询没有考试的学员人数???

--5. 找出没有参加"李明"老师讲授课程考试的所有学生姓名
select std_name
from student
where std_no not in 
(select a.std_no
from student a
inner join s