 |
|
ORA-01779 on SQL update tips
Oracle Tips by Burleson Consulting
Updated August 9, 2011 |
Question: I cannot get
this update to run and I get the ORA-01779 error:
ORA-01779: cannot modify a
column which maps to a non key-preserved table.
update
(select
src.object_name AS src_name,
src.object_type AS src_type,
tgt.object_name AS tgt_name,
tgt.object_type AS
tgt_type,
from
source_table src,
target_table tgt
where
src.object_id = tgt.object_id)
set
tgt_name = src_name,
tgt_type = src_type;
Answer: Using the
oerr
utility, the Oracle docs note this on the
ORA-01779 error:
ORA-01779: cannot modify a column which maps to a non
key-preserved table
Cause: An attempt was made to
insert or update columns of a join view which map to a non-key-preserved
table.
Action: Modify the underlying base tables
directly.
In your case, the ORA-01779 results because the update statement is malformed (by using an in-line view on
two tables) and you want to issue the SQL update directly against
one individual table, not the two tables that are specified in your DML
syntax.
Views can be updated under the right conditions, in which case, the
ORA-01779 will not be an issue.
Some views support inserts while at the same time rejecting updates.
Whether a view can be updated depends on the keys from the underlying table
or tables.
The answer to the question of whether or not a particular view is updatable
is directly related to the difference between simple and complex
views.
Generally, DML can be performed against the view if the base table is
key-preserved.
DML is not allowed in situations where duplicate data exists but is
suppressed, like with the DISTINCT operator. Attempting an update
using DISTINCT is not allowed because Oracle has no way of knowing which
record should be updated.
Thus, with complex views, some columns may be updated and some may not.
This is decided by the table from which the column comes and if there is a
primary key or key-preserved value that can be determined.
The ORA-01779 will result if you have inaccurately assessed your table for
primary key or key-preserved value integrity.
Follow the jump for more
good information
on views.