资源描述
实验五 视图的创建与使用
一、实验目的
(1)理解视图的概念。
(2)掌握创建视图、测试、加密视图的方法。
(3)掌握更改视图的方法。
(4)掌握用视图管理数据的方法。
(5)了解分区视图的实现方法。
二、实验内容
1.创建视图
(1)创建一个名为stuview2的水平视图,从数据库Student_infoXSGL的studentStudent表中查询出性别为“男”的所有学生的资料。并在创建视图时使用with check option。(注:该子句用于强制视图上执行的所有修改语句必须符合由Select语句where中的条件。)
create view stuview2
as
select Sno,Sname,Sex,Birth,Classno,Entrance_date,Homeaddr,Sdept,Postcode
from Student_20103322
where Sex='男'
with check option
(2)创建一个名为stuview3的投影视图,从数据库Student_infoXSGL的courseCourse表中查询学分大于3的所有课程的课程号、课程名、总学时。,并在创建时对该视图加密。(提示:用with ENCRYPTION关键子句,加在as与视图名称之间,且sp_helptext无法看到该视图的定义脚本。)
create view stuview3
with ENCRYPTION
as
select Cno,Cname,Total_perior
from Course_20103322
where Credit>3
with check option
(3)创建一个名为stuview4的视图,能检索出“052051”班所有女生的学号、课程号及相应的成绩。
create view stuview4
as
select Student_20103322.Sno,Cno,Grade
from Student_20103322,SC_20103322
where Student_20103322.Sno=SC_20103322.Sno
and Classno='051'
and Sex='女'
(4)创建一个名为stuview5的视图,能检索出每位选课学生的学号、姓名、总成绩。
create view stuview5(Sno,Sname,SumGrade)
as
select SC_20103322.Sno,Sname,SUM(Grade)
from SC_20103322,Student_20103322
where SC_20103322.Sno=Student_20103322.Sno
group by SC_20103322.Sno,Sname
2.查询视图的创建信息及视图中的数据
(1)查看视图stuview2的创建信息。
方法1:(提示:a.通过系统存储过程sp_help查看)
exec sp_help stuview2
方法2:(提示:b.通过select语句,查询表sysobjects)
select *
from sysobjects
where name='stuview2'
(2) 通过查看视图的定义脚本。
a.通过系统存储过程sp_helptext
exec sp_helptext stuview2
b.通过查询表sysobjects和表syscomments
(提示:视图的名称保存在表sysobjects的name列,定义脚本保存在表syscomments的text列)
(方法:(提示:sp_helptext)
select text
from sysobjects,syscomments
where name='stuview2'
and sysobjects.id=syscomments.id
3)查看加密视图stuview3的定义脚本。
exec sp_helptext stuview3
3.修改视图的定义
(1)修改视图stuview3使其从数据库Student_infoXSGL的studentStudent表中查询总学时大于60的所有课程的课程号、课程名、学分。(提示:若视图原具有加密保护,修改视图时若未加with encryption子句,则修改后的视图不再加密。修改视图请查阅帮助alter view关键字。)
alter view stuview3
with ENCRYPTION
as
select Cno,Cname,Credit
from Course_20103322
where Total_perior>60
4.视图的更名与删除
1)用系统存储过程sp_rename将视图stuview4更名为stuv4。
方法:(提示:sp_rename)
sp_rename stuview4,stuv4
2)将视图stuv4删除。
drop view stuv4
5.管理视图中的数据
1)从视图stuview2查询出班级为“051”、姓名为“张虹”的资料。
select *
from stuview2
where Classno='051' and Sname='张虹'
2)向视图stuview2中插入一行数据,内容为:
学号 姓名 班级 性别 家庭住址 入学时间 出生年月
20110005 赵小林 054 男 南京 2011/09/01 1993/01/09
insert
into stuview2(Sno,Sname,Classno,Sex,Homeaddr,Entrance_date,Birth,Sdept,Postcode)
values('20110005','赵小林','054','男','南京','2011-09-01','1993-01-09','计算机系','211506')
3)查询student,查看表中的内容有何变化。
select *
from Student_20103322
4)向视图stuview2中插入一行数据,内容为:
学号 姓名 班级 性别 家庭住址 入学时间 出生年月
20110006 赵静 054 女 南京 2011/09/01 1993/11/09
能成功插入吗?原因何在?
insert
into stuview2(Sno,Sname,Classno,Sex,Homeaddr,Entrance_date,Birth,Sdept,Postcode)
values('20110006','赵静','054','女','南京','2011-09-01','1993-11-09','计算机系','211506')
不能插入。因为stuview2的水平视图,是Student_20103322_info数据库的Student_20103322表中查询出的所有性别为“男”的学生资料,插入的新数据性别为“女”,所以不能执行。
5)修改视图stuview2中的数据。
a.将stuview2中054班、姓名为“赵小林”同学的家庭地址改为“扬州市”。
update stuview2
set Homeaddr='扬州市'
where Classno='054' and Sname='赵小林'
b. 查询student,查看表中的内容有何变化
select *
from Student_20103322
6) 从视图stuview2中将班级为054、姓名为“赵小林”同学删除。
delete
from stuview2
where Sname='赵小林'
14
展开阅读全文