PLSQL_基础系列05_视图控制WITH CHECK OPTION(案例)

2014-12-09 Created By BaoXinjian

一、摘要


通过有with check option选项的视图操作基表(只是面对单表,对连接多表的视图正在寻找答案),有以下结论:

首先视图只操作它可以查询出来的数据,对于它查询不出的数据,即使基表有,也不可以通过视图来操作。

1. 对于update, 有with check option,要保证update后,数据要被视图查询出来

2. 对于delete, 有无with check option都一样

3. 对于insert, 有with check option,要保证insert后,数据要被视图查询出来

对于没有where 子句的视图,使用with check option是多余的。

 

二、案例


Step1. 创建表

create table emp(
    id number(5,0),
    name varchar2(12),
    address varchar2(12)
);

insert into emp values (5,'張三','廣西');
insert into emp values (6,'李四','北京');
insert into emp values (7,'王五','山東');

 

Step2. 创建带with check option的视图

create view emp_view
as
select * from emp where id=5
with check option;

 

Step3. 创建没有with check option的视图

create view emp_view2
as
select * from emp where id='5'

 

Step4. update 操作

update  emp_view set  name='陈六' where id=6;-,虽然基表有id=6的记录,但emp_view无法查看到,所以这个修改不会影响基表内容

update  emp_view set  id=6 where id=5; --出现 view WITH CHECK OPTION where-clause violation错误

update  emp_view2 set id=6 where id=5; --成功执/plain行

结论:对于update,有无with选项都是只更改视图出现的記錄,对有whih选项的update,要保证更改后仍可以出现在视图中

 

Step5. 操作

--update操作
update  emp set id=5 where name='張三';

--delete操作
delete emp_view where id='5'

--结论:
--对于delete,有无with选项是一样的。
--insert操作 insert into emp_view values (8,'','江蘇');--出现 view WITH CHECK OPTION where-clause violation错误 insert into emp_view2 values (8,'','江蘇');--执行成功

 

Step6. 结论:

对于insert,有with选项,插入的数据要最终要可以显示在视图中,对于无with选项的视图可以插入任何不违反约束的记录

 

Thanks and Regards

参考: http://blog.csdn.net/fredrickhu/article/details/4743204

原文地址:https://www.cnblogs.com/eastsea/p/4134346.html