熟悉SQL支持的有关视图的操作,能够熟练使用SQL语句来创建需要的视图,对视图进行查询和删除视图。
4.4实验结果
1.代码如下:
- create view xun_CS_View
- as
- select xn_Sno,xn_Tno,xn_Cno,xn_Scroce
- from xun_Reports
- where xn_Scroce>=60
- with check option
2.代码如下:
- create view xun_SCT_View
- as
- select xn_Sname,xn_Cname,xn_Tname
- from xun_Students,xun_Reports,xun_Courses,xun_Teachers
- where xun_Students.xn_Sno=xun_Reports.xn_Sno
- and xun_Reports.xn_Cno=xun_Courses.xn_Cno
- and xun_Reports.xn_Tno=xun_Teachers.xn_Tno
3.代码如下:
- create view xun_EXP_View(xn_Sname,xn_Cname,xn_Scroce)
- as
- select xn_Sname,xn_Cname,xn_Scroce+5
- from xun_Students,xun_Reports,xun_Courses
- where xun_Students.xn_Sno=xun_Reports.xn_Sno
- and xun_Reports.xn_Cno=xun_Courses.xn_Cno
视图如下:

原先的xun_Reports表视图如下,可知成绩xn_Scroce已经加5.

4.代码如下:
- create view xun_Group_View
- as
- select xn_Sno,avg(xn_Scroce) avgscore
- from xun_Reports
- group by xn_Sno
xun_Group_View视图如下:

5.
- create view xun_VV_View
- as
- select xn_Sno,xn_Cno,avg(xn_Scroce) avgscore
- from xun_CS_View
- group by xn_Sno,xn_Cno
- having avg(xn_Scroce)>=60
6.
- select xn_Sname
- from xun_Students
- where xn_Sno in(
- select xn_Sno
- from xun_Reports
- where xn_Cno in(
- select xn_Cno
- from xun_Courses
- where xn_Cname='数据库原理及其应用'
- )
- )
7.
- insert into xun_CS_View
- values('S52','T02','C02',59)
下会有错误信息弹出,如下:
错误信息:
消息 550,级别 16,状态 1,第 1 行
试图进行的插入或更新已失败,原因是目标视图或者目标视图所跨越的某一视图指定了 WITH CHECK OPTION,而该操作的一个或多个结果行又不符合 CHECK OPTION 约束。
语句已终止。

因为建立xun_CS_View视图时加了with check option,有限制条件xn_Scroce要大于等于60,但这条记录的成绩不满足要求,所以不能成功插入。
若在建立xun_CS_View视图时不加了with check option,则可以成功插入,但视图不显示。
8.
减5
- update xun_CS_View
- set xn_Scroce-=5
- where xn_Cno='C01'
下会有错误信息弹出,如下:
错误信息:
消息 550,级别 16,状态 1,第 1 行
试图进行的插入或更新已失败,原因是目标视图或者目标视图所跨越的某一视图指定了 WITH CHECK OPTION,而该操作的一个或多个结果行又不符合 CHECK OPTION 约束。
语句已终止。

因为减5后,有些记录的xn_Scroce值会有小于60,不满足约束要求,所以报错。
加5
- update xun_CS_View
- set xn_Scroce+=5
- where xn_Cno='C01'and xn_Scroce<95
代码正常执行

视图变为下图:

加5后,不影响约束条件,所以可以正常执行。
9.
- delete
- from xun_CS_View
- where xn_Sno='S03'
正常执行
10.
drop view xun_CS_View,xun_SCT_View
4.5实验体会:
1.with check option会限制视图的更新update,插入insert和删除delete,需要保证满足视图定义中的谓词条件。
2.不使用with check option语句,但是有谓词条件,不满足谓词条件的操作代码可以正常执行,但是这条数据就不会显示在视图中。
3.基于视图建立的视图和基于表建立视图的操作很相似,基本一样的。
4.使用DROP语句删除一个视图,由该视图导出的其他视图定义仍在数据字典中,但已不能使用,必须显式删除。
4.6附件:
- 1.
- create view xun_CS_View
- as
- select xn_Sno,xn_Tno,xn_Cno,xn_Scroce
- from xun_Reports
- where xn_Scroce>=60
- with check option
-
- 2.
- create view xun_SCT_View
- as
- select xn_Sname,xn_Cname,xn_Tname
- from xun_Students,xun_Reports,xun_Courses,xun_Teachers
- where xun_Students.xn_Sno=xun_Reports.xn_Sno
- and xun_Reports.xn_Cno=xun_Courses.xn_Cno
- and xun_Reports.xn_Tno=xun_Teachers.xn_Tno
-
- 3.
- create view xun_EXP_View(xn_Sname,xn_Cname,xn_Scroce)
- as
- select xn_Sname,xn_Cname,xn_Scroce+5
- from xun_Students,xun_Reports,xun_Courses
- where xun_Students.xn_Sno=xun_Reports.xn_Sno
- and xun_Reports.xn_Cno=xun_Courses.xn_Cno
-
- 4.
- create view xun_Group_View
- as
- select xn_Sno,avg(xn_Scroce) avgscore
- from xun_Reports
- group by xn_Sno
-
- 5.
- create view xun_VV_View
- as
- select xn_Sno,xn_Cno,avg(xn_Scroce) avgscore
- from xun_CS_View
- group by xn_Sno,xn_Cno
- having avg(xn_Scroce)>=60
-
- 6.
- select xn_Sname
- from xun_Students
- where xn_Sno in(
- select xn_Sno
- from xun_Reports
- where xn_Cno in(
- select xn_Cno
- from xun_Courses
- where xn_Cname='数据库原理及其应用'
- )
- )
-
-
- 7.
- insert into xun_CS_View
- values('S52','T02','C02',59)
-
- 8.
- 减5
- update xun_CS_View
- set xn_Scroce-=5
- where xn_Cno='C01'
- 加5
- update xun_CS_View
- set xn_Scroce+=5
- where xn_Cno='C01'and xn_Scroce<95
-
- 9.
- delete
- from xun_CS_View
- where xn_Sno='S03'
-
- 10.
- drop view xun_CS_View,xun_SCT_View