1、5实验五 视图的创建与使用实验五 视图的创立与使用一、实验目的(1理解视图的概念。(2掌握创立视图、测试、加密视图的方法。(3掌握更改视图的方法。(4掌握用视图管理数据的方法。二、实验内容(1创立一个名为stuview2的水平视图,从数据库Student_info的Student表中查询出性别为“男的所有学生的资料。并在创立视图时使用with check option。注:该子句用于强制视图上执行的所有修改语句必须符合由Select语句where中的条件。create view stuview2asselect * from Studentwhere Sex=男with check optio
2、n查看视图:select * from stuview2(2创立一个名为stuview3的投影视图,从数据库Student_info的Course表中查询学分大于3的所有课程的课程号、课程名、总学时。并在创立时对该视图加密。提示:用with ENCRYPTION关键子句create view stuview3with ENCRYPTIONasselect Cno,Cname,Total_perior from Coursewhere Credit3查看视图:select * from stuview3(3创立一个名为stuview4的视图,能检索出“051班所有女生的学号、课程号及相应的成绩。
3、create view stuview4asselect * from SCwhere Sno=( select Sno from Student where Classno=051 and Sex=女)查看视图:select * from stuview4(4创立一个名为stuview5的视图,能检索出每位选课学生的学号、姓名、总成绩。create view stuview5asselect Student.Sno 学号,Sname 姓名,Grade 成绩from Student,SCwhere Student.Sno=SC.Sno查看视图:select * from stuview5假设出
4、现如上图所示情况,单击“查询IntelliSense刷新本地缓存然后就解决了。(1查看视图stuview2的创立信息。sp_help查看查询表sysobjectsa、sp_help stuview2b、select so.name,sc.name,sc.colid,st.namefrom sysobjects so,syscolumns sc,systypes st where SO.id = SC.id and SO.xtype = V and SO.status = 0 and SC.xtype = ST.xusertype and SO.name = stuview2 order by
5、SO.name, SC.colorder(2) 通过查看视图的定义脚本。sp_helptextsp_helptext stuview2和表syscomments提示:视图的名称保存在表sysobjects的name列,定义脚本保存在表syscomments的text列select SO.name,SC.textfrom sysobjects SO,syscomments SC where SO.id = SC.id and SO.xtype = V and SO.status = 0 and SO.name = stuview23)查看加密视图stuview3的定义脚本。sp_helptext
6、 stuview3(1修改视图stuview3使其从数据库Student_info的Student表中查询总学时大于60的所有课程的课程号、课程名、学分。提示:假设视图原具有加密保护,修改视图时假设未加with encryption子句,那么修改后的视图不再加密。alter view stuview3with encryptionasselect Cno,Cname,Credit from Coursewhere Total_perior60查看视图:select * from stuview31用系统存储过程sp_rename将视图stuview4更名为stuv4。sp_rename stu
7、view4,stuv42)将视图stuv4删除。drop view stuv41从视图stuview2查询出班级为“051、姓名为“张虹的资料。select * from stuview2where Classno=051 and Sname=张虹2向视图stuview2中插入一行数据,内容为:学号 姓名 班级 性别 家庭住址 入学时间 出生年月20210005 赵小林 054 男 南京 2011/09/01 1993/01/09insert into stuview2values(20210005,赵小林,男,1993/01/09,054,2021/09/01, 南京,CH,202111)查
8、看视图:select * from stuview23查询student,查看表中的内容有何变化。Student 表中已有“赵小林的信息select * from Student4向视图stuview2中插入一行数据,内容为:学号 姓名 班级 性别 家庭住址 入学时间 出生年月20210006 赵静 054 女 南京 2021/09/01 1993/11/09 能成功插入吗?原因何在?不能插入,原因是目标视图或者目标视图所跨越的某一视图指定了WITH CHECK OPTION,而该操作的一个或多个结果行又不符合CHECK OPTION 约束。insert into stuview2values
9、(20210006,赵静,女,1993/01/09,054,2021/09/01, 南京,CH,202111)5修改视图stuview2中的数据。stuview2中054班、姓名为“赵小林同学的家庭地址改为“扬州市。update stuview2set Home_addr=扬州市where Home_addr=南京 and Sname=赵小林 and Classno=054查看视图:select * from stuview2b. 查询student,查看表中的内容有何变化student 表中的赵小林的家庭住址已发生了改变select * from Student6 从视图stuview1中将班级为054、姓名为“赵小林同学删除。delete from stuview2where Sname=赵小林查看视图:【本文档内容可以自由复制内容或自由编辑修改内容期待你的好评和关注,我们将会做得更好】
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2