"Data may be inserted into complex cviews when the statement affects only one of the tables in the join."
Tom, can you show us how in a view , the statement can effect only one of the tables in the join.
Does it mean that the insert can be done only into one table at a time in the view.
2."In complex views, for update statements all columns changed are extracted from a key -preserved table . In addition , if the view is created wiht the check option clause, join columns and columns taken from the tables that are referenced more than once in the view are not part of the update."
-What is a key preserved table.
-why are join columns not part of the update
3."In complex views, for delete statements, there is only one key preserved table in the join.this table may be present more than once in the join , unless the view has been created witht the check option."
can you explain the above...
4.why should all the columns where values are inserted must come from a key preserved table in case of of a complex view.
searching for
key preserved
on this site, i find:
</code>
http://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:548422757486 <code>
which explains the concept (and once you understand the concept, the rest is pretty easy)....
1) so, here is the example of the insert:
ops$tkyte@ORA817DEV.US.ORACLE.COM> create table t1 ( t1_x int primary key, t1_y date );
Table created.
ops$tkyte@ORA817DEV.US.ORACLE.COM> create table t2 ( t2_x int primary key, t2_y date );
Table created.
ops$tkyte@ORA817DEV.US.ORACLE.COM>
ops$tkyte@ORA817DEV.US.ORACLE.COM> create or replace view v
2 as
3 select * from t1, t2 where t1.t1_x = t2.t2_x;
View created.
ops$tkyte@ORA817DEV.US.ORACLE.COM>
ops$tkyte@ORA817DEV.US.ORACLE.COM> insert into v values ( 1, sysdate, 1, sysdate );
insert into v values ( 1, sysdate, 1, sysdate )
*
ERROR at line 1:
ORA-01776: cannot modify more than one base table through a join view
ops$tkyte@ORA817DEV.US.ORACLE.COM> insert into v (t1_x, t1_y ) values ( 1, sysdate );
1 row created.
ops$tkyte@ORA817DEV.US.ORACLE.COM> insert into v (t2_x, t2_y ) values ( 1, sysdate );
1 row created.
ops$tkyte@ORA817DEV.US.ORACLE.COM>
you see -- we cannot insert into both t1 and t2 using this view WITHOUT HELP (we can write an instead of trigger and provide the logic to insert into both underlying tables). We can directly insert into either of t1 or t2 as demonstrated however.
2) See the above link for "what is a key preserved..."
As for the part about the join columns -- well, only one table at a time can be modified via the view, and if you update the join columns -- that row would not appear in that view anymore (you changed the join column values -- that row doesn't exist anymore, the check condition fails). Hence in a view with check option the join columns cannot be modified. You can see this via:
ops$tkyte@ORA817DEV.US.ORACLE.COM> select column_name, updatable, insertable, deletable
2 from user_updatable_columns where table_name = 'V'
3 /
COLUMN_NAME UPD INS DEL
------------------------------ --- --- ---
T1_X YES YES YES
T1_Y YES YES YES
T2_X YES YES YES
T2_Y YES YES YES
ops$tkyte@ORA817DEV.US.ORACLE.COM> create or replace view v
2 as
3 select * from t1, t2 where t1.t1_x = t2.t2_x
4 WITH CHECK OPTION;
View created.
ops$tkyte@ORA817DEV.US.ORACLE.COM>
ops$tkyte@ORA817DEV.US.ORACLE.COM> select column_name, updatable, insertable, deletable
2 from user_updatable_columns where table_name = 'V'
3 /
COLUMN_NAME UPD INS DEL
------------------------------ --- --- ---
T1_X NO NO NO
T1_Y YES YES YES
T2_X NO NO NO
T2_Y YES YES YES
All we did was recreate the view with check option here....
3) if there are two key preserved tables in a view (our example has 2 key preserved tables), we cannot delete from it cause Oracle doesn't know WHICH table to delete from and it won't do both.
ops$tkyte@ORA817DEV.US.ORACLE.COM> delete from v;
delete from v
*
ERROR at line 1:
ORA-01752: cannot delete from view without exactly one key-preserved table
Now, if we drop the primary key constraint:
ops$tkyte@ORA817DEV.US.ORACLE.COM> alter table t1 drop constraint SYS_C004852 ;
Table altered.
ops$tkyte@ORA817DEV.US.ORACLE.COM> delete from v;
1 row deleted.
ops$tkyte@ORA817DEV.US.ORACLE.COM> select * from t2;
T2_X T2_Y
---------- ---------
1 13-JAN-02
ops$tkyte@ORA817DEV.US.ORACLE.COM> select * from t1;
no rows selected
T1 remains key preserved but T2 is no longer key preserved. Hence we can delete from the view and the act of deleting from the view will delete from T1.
4) hopefully, once you read about key preserved -- this will be obvious.... We need to make sure that the row will appear at most "once" in the view itself -- otherwise the very act if modifying the row is ambigous....