What is an oracle key-preserved table and what kind of views can be used for DML operations?

Source: Internet
Author: User

When operating the Oracle view through DML, a very important condition is that the base table to be operated must be a key-preserved table.
So what is the key-preserved table? Oracle defines it:
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 wowould also be key (s) of the result of the join.

If the primary key of a table can be used as the primary key of the join result (view is usually the join result of several tables), the table is the key preserved table.

The primary key of this table does not necessarily appear in the select result set (in the select list), but if it appears in the result set, it must meet the requirements of the primary key used as the result set.

Let's look at an example. There are two tables, DEPT and employee, and the testv view based on the two tables.

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 edeptidfrom dept d join employee eon d.deptid=e.deptid

Query this view: Select * From testv

In the testv view, employee is a key preserved table, and DEPT is not.
The DML that this view can perform is
Delete from testv where empid = 1 (the operation result is to delete the record empid = 1 in the employee table, and the dept table will not change)
Delete from testv where deptid = 1 (the operation result is to delete the records of empid = 1 and 2 in the employee table, and the dept table will not change)
Update testv set empname = 'emp' where edeptid = 1
Update testv set empname = 'emp' where empid = 1
Update testv set empname = 'emp' where deptid = 1
Insert into testv (empid, empname, edeptid) values (4, 'emp4 ', 2)
DML that cannot be performed in this view is
Update testv set deptname = 'depptx' where deptid = 1
Update testv set deptname = 'ptx' 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
A view can have multiple keys 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,t2vfrom test1 join test2on test1.t1id=test2.t2id

In this view, both test1 and Test2 are key preserved tables. However, the DML operation on the view can only act on one table at a time and cannot operate on multiple base tables at a time.
Select * From test1v

The following DML is acceptable.
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 (the two Delete statements Delete the data in Table test1 ??)
However, the following DML operations are not allowed:
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
There are other restrictions on whether a view can perform DML operations:
Start with and connect.
Group by and having.
Union, intersect, and minus operations are not allowed.
There cannot be Aggregate functions, such as AVG, count, and Max.
Rownum pseudo columns are not allowed.
When view has check option:

create view test1v as select t1id ,t1v,t2id,t2vfrom test1 join test2on test1.t1id=test2.t2idwith 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

How to view which fields can be inserted and updated?

Select * From user_updatable_columns where table_name = 'testv'

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.