1. 程式人生 > >什麼是Oracle Key-Preserved Table和什麼樣的檢視可以進行DML操作

什麼是Oracle Key-Preserved Table和什麼樣的檢視可以進行DML操作

在通過DML操作Oracle的檢視的時候,有一個很重要的條件是操作的base表必須是Key-Preserved Table.
那麼,什麼是Key-Preserved Table呢.Oracle給出的定義是:
A table is key preserved if every key of  the table can also be a key of the result of the join.
It is not necessary that the key or keys of a table be selected for it to be key preserved.

It is sufficient that if the key or keys were selected, then they would also be key(s) of the result of the join.

如果某一個表的主鍵可以作為這個join結果(view通常是幾個表的join結果)的主鍵,那麼這個表就是key preserved table.

這個表的主鍵並非一定要出現在select出來的結果集中(select list裡面),但是如果其出現在結果集中,那麼它必須可以滿足作為這個結果集的主鍵的要求.

來看一個例子,有這樣兩個表,dept和employee,以及基於這兩個表的testv檢視
create table dept(deptid int primary key,deptname varchar2(20))
create table employee(empid int primary key,empname varchar2(20),deptid int)

insert into dept values(1,'dept1');
insert into dept values(2,'dept2');
insert into dept values(3,'dept3');

insert into employee values(1,'emp1',1);
insert into employee values(2,'emp2',1);
insert into employee values(3,'emp3',2);

create view testv 
as select d.deptid deptid,deptname,empid,empname,e.deptid edeptid
from dept d join employee e
on d.deptid=e.deptid
查詢這個檢視:select * from testv


在testv這個檢視中,employee就是一個key preserved table,而dept不是.
那麼這個檢視可以進行的DML為
delete from testv where empid=1(操作的結果是將employee表中的empid=1的記錄delete了,dept表不會有改變)
delete from testv where deptid=1(操作的結果是將employee表中的empid=1和2的記錄都delete了,dept表不會有改變)
update testv set empname='empx' where edeptid=1
update testv set empname='empx' where empid=1
update testv set empname='empx' where deptid=1
insert into testv(empid,empname,edeptid) values(4,'emp4',2)
這個檢視不可以進行的DML為
update testv set deptname='deptx' where deptid=1
update testv set deptname='deptx' where empid=1
insert into testv(deptid,deptname) values(4,'dept4')
ORA-01779: cannot modify a column which maps to a non key-preserved table
一個View中可以有多個key preserved tables
create table test1(t1id int primary key,t1v varchar2(10))
create table test2(t2id int primary key,t2v varchar2(10))

insert into test1 values(1,'t1');
insert into test1 values(2,'t2');
insert into test1 values(3,'t3');

insert into test2 values(1,'t21');
insert into test2 values(2,'t22');
insert into test2 values(3,'t23');

create view test1v 
as select t1id ,t1v,t2id,t2v
from test1 join test2
on test1.t1id=test2.t2id
在這個檢視中,test1,test2都是key preserved table.但是對view的DML操作一次只能作用於一個表,不能同時對多個base 表操作.
select * from test1v


如下面的DML是可以的
update test1v set t1v='t11'
update test1v set t2v='t22'
insert into test1v(t1id,t1v) values(4,'t4')
insert into test1v(t2id,t2v) values(4,'t24')
delete from test1v where t1id=4
delete from test1v where t2id=4(這兩條delete語句都是將test1表中的資料delete了??)
但是下面的DML操作是不可以的:
update test1v set  t1v='t11',t2v='t22'
insert into test1v values(5,'t5',5,'t25')
ORA-01776: cannot modify more than one base table through a join view
對於View是否可以進行DML操作還有一些其他限制:
不能有start with和connect by.
不能有group by和having.
不能有集合操作union,intersect和minus.
不能有聚合函式,如AVG,COUNT,MAX等等.
不能有rownum偽列.
當view有check option的時候:
create view test1v 
as select t1id ,t1v,t2id,t2v
from test1 join test2
on test1.t1id=test2.t2id
with check option
insert into test1v(t1id,t1v) values(4,'t4')
update test1v set t1id=4 where t1id=1
ORA-01733: virtual column not allowed here

如何檢視那些欄位可以插入更新

select * from USER_UPDATABLE_COLUMNS where table_name='TESTV'