--创建一个简单的样例表
HR@prod> create table emp as select employee_id,last_name,salary,department_id from employees;
Table created.
--创建一个简单视图
HR@prod> create or replace view empvu
2 as
3 select * from emp
4 where department_id=20;
View created.
--通过视图插入一行数据
HR@prod> insert into empvu values(500,'mary',5000,10);
1 row created.
--插入的数据最终会保存到基表
HR@prod> select * from emp where employee_id=500;
EMPLOYEE_ID LAST_NAME SALARY DEPARTMENT_ID
----------- ------------------------- ---------- -------------
500 mary 5000 10
--但是视图里面查询不到,因为视图带有查询条件where department_id=20;
HR@prod> select * from empvu;
EMPLOYEE_ID LAST_NAME SALARY DEPARTMENT_ID
----------- ------------------------- ---------- -------------
201 Hartstein 13000 20
202 Fay 6000 20
--更新这个插入的数据,无法更新,因为视图里没有这个记录
HR@prod> update empvu set salary=6000 where employee_id=500;
0 rows updated.
--删除这条记录,也无法删除,同样因为视图里面不包含这个记录
HR@prod> delete from empvu where employee_id=500;
0 rows deleted.
--更新视图里包含的数据,可以正常更新
HR@prod> update empvu set salary=salary+1 where employee_id=201;
1 row updated.
--同样,更新的结果反映到基表
HR@prod> select * from emp where employee_id=201;
EMPLOYEE_ID LAST_NAME SALARY DEPARTMENT_ID
----------- ------------------------- ---------- -------------
201 Hartstein 13001 20
--删除视图里面包含的数据,同样可以实现
HR@prod> delete from empvu where employee_id=201;
1 row deleted.
--删除的效果,同样反映到视图
HR@prod> select * from empvu;
EMPLOYEE_ID LAST_NAME SALARY DEPARTMENT_ID
----------- ------------------------- ---------- -------------
202 Fay 6000 20
--基表当中,这个数据被删除
HR@prod> select * from emp where employee_id=201;
no rows selected
--创建一个带有withc check option的视图
HR@prod> create or replace view empvu20
2 as
3 select * from emp
4 where department_id=20
5 with check option constraint check_20;
View created.
--通过这个视图,想要插入一行记录
HR@prod> insert into empvu20 values(501,'mike',5000,20);
1 row created.
==插入的信息,是20号部门,所以可以操作;
--插入一行记录,不是20号部门
HR@prod> insert into empvu20 values(502,'rose',6000,10);
insert into empvu20 values(502,'rose',6000,10)
*
ERROR at line 1:
ORA-01402: view WITH CHECK OPTION where-clause violation