CREATE [OR REPLACE] [FORCE] VIEW
view_name [(alias[, alias]...)]
AS select_statement
[WITH CHECK OPTION]
[WITH READ ONLY];
force:强制创建视图,不管有没有表都强制创建,当创建了表后,强制创建后便可使用。
在with check option的选项下,可以总结为:
with read only :不能进行任何更新、插入、删除等操作,只能查询。
-- 没有使用with check option选项
create table e_bak as select * from emp;
create view view3 as select * from e_bak where empno=‘7369‘;
update view3 set empno=‘9999‘ where empno=‘7369‘;
-- 查询记录
select * from e_bak; -- 查询发现empno为7369的记录已经被修改
-- 查询视图
select * from view3; -- 查询结果无数据
在上述这种情况下,视图view3也就相当于失效了,是一个没有任何意义的视图。那么在Oracle引入了with check option来进行更新限制
-- 使用with check option选项
create view view4 as select * from e_bak where empno=‘9999‘ with check option;
update view4 set empno=‘8888‘ where empno=‘9999‘;
-- 执行报错:ORA-01402: 视图 WITH CHECK OPTION where 子句违规
在视图上也可以使用修改数据的DML语句,如INSERT、UPDATE和DELETE
视图上的DML语句有如下限制:
案例说明
create table student_info(stu_no number primary key,stu_name varchar2(40),cls_no number);
create table class(cls_no number primary key,cls_name varchar2(40));
-- 插入数据
insert into student_info values(1,‘张三‘,10);
insert into student_info values(2,‘李四‘,30);
insert into student_info values(3,‘王五‘,30);
insert into student_info values(4,‘周六‘,20);
insert into class values(10,‘统计1101‘);
insert into class values(20,‘信计1101‘);
insert into class values(30,‘物理1101‘);
insert into class values(40,‘软件1101‘);
commit;
-- 创建视图
create view stu_cls as select
s.stu_no,
s.stu_name,
s.cls_no,
c.cls_no as class_no,
c.cls_name
from student_info s,class c where s.cls_no=c.cls_no;
-- 现在student是主键保留表,cls是非主键保留表
-- 更新student
update stu_cls set stu_no=‘11‘ where stu_no=1;
update stu_cls set stu_name=‘Jack‘ where stu_no=2;
update stu_cls set cls_no=‘40‘ where stu_no=3;
-- 更新class
update stu_cls set cls_name=‘交通运输1101‘ where cls_no=‘20‘
-- 报错:ORA-01779: 无法修改与非键值保存表对应的列
DROP VIEW view_name;
原文:https://www.cnblogs.com/OliverQin/p/12679858.html