@[toc]
1.定义所有女生信息的视图“F_Student”(5分)
CREATE VIEW F_Student
AS
SELECT *
FROM Student
WHERE Sex='女'
2.通过视图“F_Student”查询小于18岁的女生人数。(5分)
SELECT COUNT(*)
FROM F_Student
WHERE Sage<18
3.定义“计算机系”学生的视图”CS_Student”,并要求透过该视图进行的更新操作只涉及计算机系学生。(5分)
CREATE VIEW CS_Student
AS
SELECT Sno,Sname,Sex,Sage,Sdept
FROM Student
WHERE Sdept= '计算机系'
WITH CHECK OPTION
4.通过视图CS_Student插入一条学生记录,数据自拟,插入的数据在Sdept是否有限制?(5分)
INSERT INTO CS_Student
VALUES('19250101','王芳','女',20,'计算机系')
有限制:所插入的数据在Sdept列上必须是“计算机系”
5.通过视图CS_Student删除姓名为“徐庆”的学生记录,能否成功?为什么?(10分)
DELETE FROM CS_Student
WHERE Sname='徐庆'
go
--执行结果显示0行受影响,则说明未删除成功
SELECT *
FROM Student
WHERE Sname='徐庆'
go
--执行查询依然可看到该生信息
原因:徐庆并非“计算机系”学生,因此无法透过视图“CS_Student”来实现删除功能。
6.建立选修了“数据库原理”这门课的学生信息视图“DB_SC”,视图列包含:学号,姓名,所在系,课程名,分数。(5分)
CREATE VIEW DB_SC(Sno,Sname,Sdept,Cname,Score)
AS
SELECT Student.Sno,Sname,Sdept,Course.Cname,Score
FROM Student,SC,Course
WHERE Student.Sno=SC.Sno AND SC.Cno=Course.Cno AND Cname='数据库原理'
7.在视图“DB_SC”上定义新视图“DB_SC_60”,选修了“数据库原理”这门课且成绩及格的学生信息视图。(5分)
CREATE VIEW DB_SC_60
AS
SELECT *
FROM DB_SC
WHERE Score>=60
8.将每门课程及其平均分建立视图,视图名自定义(5分)
CREATE VIEW Cour_AVG(Cno,Cavg)
AS
SELECT Cno, AVG(Score)
FROM SC
GROUP BY Cno
go
Select *
from Cour_AVG
GO
9.将视图“F_Student”修改为记录所有男生信息的视图。(5分)
ALTER VIEW F_Student
AS
SELECT *
FROM Student
WHERE Sex='男'
10.删除视图“F_Student”(5分)
DROP VIEW F_Student
存储过程
11.创建一个存储过程“PROC_STU”,查询所有学生信息,执行该存储过程,查看结果。(10分)
CREATE PROCEDURE PROC_STU
AS
SELECT *
FROM Student
GO
EXEC PROC_STU
go
12.创建一个存储过程“PROC_SELECT_SEX_COUNT”,查询男生和女生的人数。执行该存储过程,查看执行结果。(10分)
CREATE PROCEDURE PROC_SELECT_SEX_COUNT
AS
SELECT Sex,COUNT(Sno)
FROM Student
GROUP BY Sex
GO
EXEC PROC_SELECT_SEX_COUNT
GO
13.创建一个带有参数的存储过程,根据系别查询学生人数,执行该存储过程,查看执行结果。(10分)
CREATE PROCEDURE PROC_SELECT_COUNT_BYDEPT
@Dept varchar(50)
AS
SELECT COUNT(Sno)
FROM Student
WHERE Sdept=@Dept
GROUP BY Sdept
go
EXEC PROC_SELECT_COUNT_BYDEPT '计算机系'
go
14.创建一个带有参数的存储过程,根据课程名,查询其先修课的课程名,要求使用输出参数返回查询结果,并执行该存储过程查看结果。(15分)
CREATE PROCEDURE PROC_COURSE_PRE
@cname varchar(50),@precname varchar(50) OUTPUT
AS
SELECT @precname=Cname
FROM Course
WHERE Cno IN(SELECT Cpno
FROM Course
WHERE Cname=@cname)
GO
DECLARE @name varchar(50),@pre varchar(50)
SET @name='数据库原理'
EXEC PROC_COURSE_PRE @name,@pre OUTPUT
PRINT @pre
go