我把源码都给你吧
下面是建立表的。
create table student
(
sno varchar(3)
primary key,
thename varchar(8),
sex bit default 0, --0为男,1为女
age int check (age>0),
zy varchar(10) --专业
)
go
create table class
(
cno varchar(3)
primary key,
classname varchar(10)
)
go
create table checkclass
(
sno varchar(3) not null,
cno varchar(3) not null,
primary key(sno,cno),
grade int check(grade>0)
)
go
下面是试验的数据。
insert into class values ('001','English')
insert into class values ('002','Chinese')
insert into class values ('003','Computer')
insert into class values ('004','Math')
insert into student values ('001','王一',0,23,'计算机')
insert into student values ('002','赵二',1,21,'外语')
insert into student values ('003','张三',0,24,'数学')
insert into student values ('004','李四',0,22,'计算机')
insert into student values ('005','刘五',0,26,'物理')
insert into student values ('006','孙六',1,20,'计算机')
insert into checkclass (sno,cno)
select sno,cno from student,class
where sno<>'004' and cno <>'001'
insert into checkclass (sno,cno)
select sno,cno from student,class
where sno='004'
declare ingrade cursor
for select * from checkclass
open ingrade
fetch next from ingrade
while @@FETCH_STATUS = 0
begin
update checkclass set grade=rand()*100
where current of ingrade
fetch next from ingrade
end
下面是我自己做的前三题的答案,如果有不对的地方别笑我呀。
create view noenglish
as
select thename from student where sno not in (select sno from checkclass,class
where checkclass.cno=class.cno and classname='english')
go
create view allcheck
as
select thename from student
where exists
(select sno ,count(cno) from checkclass
group by sno
having count(cno)=(select count(*) from class) and student.sno=checkclass.sno )
go
create view three
as
select cno,avg(grade) from checkclass
group by cno
having avg(grade)>50
go