--1.创建查询学生的学号,姓名,所在系,课程号,课程名,课程学分的视图
create view v1
as
select s.sno,sname,sdept,c.cno,cname,credit
from student s join sc on s.sno= sc.sno
join course c on c.cno = sc.cno
--2.创建查询每个学生的平均成绩的视图,要求列出学生学号及平均成绩
create view v2
as
select sno,avg(grade) avg_g
from sc
group by sno
--3.创建查询每个学生的总学分的视图,要求列出学号和总学分
create view v3
as
select sno,sum(credit) sum_c
from sc join course c on c.cno = sc.cno
where grade >= 60 and grade is not null
group by sno
--4.创建查询年龄大于等于20的男生的视图,列出学号,姓名,所在系和年龄,
--并要求限制通过此视图将年龄大于等于20的男生修改为年龄小于20
create view v4
as
select sno,sname,sdept,sage from student
where sage>=20 and ssex = '男'
with check option
--(1)创建查询每个学生的修课总学分的存储过程,要求列出学号及总学分
create proc p1
as
select sno,sum(credit)
from sc join course c on c.cno = sc.cno
where grade is not null
and grade >=60
group by sno
--(2)创建查询指定系学生学号,姓名,课程号,课程名,课程学分的存储过程,将学生所在系作为输入参数.
create proc p2
@d varchar(20)
as
select s.sno,sname,c.cno,cname,credit
from course c join sc on c.cno = sc.cno
join student s on sc.sno = s.sno
where sdept = @d
--(3)计算1+2+3+...的存储过程
create proc p3
@x int,@sum int output
as
declare @i int
set @i=1
set @sum = 0
while @i<=@x
begin
set @sum = @sum + @i
set @i = @i +1
end
--(4)创建删除指定学生的修课记录的存储过程,学号为输入参数
create proc p4
@sno char(8)
as
delete from sc where sno = @sno
--(5)创建修改指定课程的学分的存储过程.
create proc p5
@cno char(8), @cr int=3
as
update course set credit = @cr
where cno= @cno