MOCKSTACKS
EN
Questions And Answers

More Tutorials









Oracle Update with Joins

Examples: what works and what doesn't


CREATE TABLE tgt ( id, val ) AS
 SELECT 1, 'a' FROM dual UNION ALL
 SELECT 2, 'b' FROM dual
;
TABLE TGT created.
CREATE TABLE src ( id, val ) AS
 SELECT 1, 'x' FROM dual UNION ALL
 SELECT 2, 'y' FROM dual
;
TABLE SRC created.
UPDATE
 ( SELECT t.val AS t_val, s.val AS s_val
 FROM tgt t inner JOIN src s ON t.id = s.id
 )
SET t_val = s_val
;
SQL Error: ORA-01779: cannot modify a column which maps to a non key-preserved table
01779. 00000 - "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.

Imagine what would happen if we had the value 1 in the column src.id more than once, with different values for src.val. Obviously, the update would make no sense (in ANY database - that's a logical issue). Now, we know that there are no duplicates in src.id, but the Oracle engine doesn't know that - so it's complaining. Perhaps this is why so many practitioners believe Oracle "doesn't have UPDATE with joins"?

What Oracle expects is that src.id should be unique, and that it, Oracle, would know that beforehand. Easily fixed! Note that the same works with composite keys (on more than one column), if the matching for the update needs to use more than one column. In practice, src.id may be PK and tgt.id may be FK pointing to this PK, but that is not relevant for updates with join; what is relevant is the unique constraint.

ALTER TABLE src ADD constraint src_uc UNIQUE (id);
TABLE SRC altered.
UPDATE
 ( SELECT t.val AS t_val, s.val AS s_val
 FROM tgt t inner JOIN src s ON t.id = s.id
 )
SET t_val = s_val
;
2 rows updated.
SELECT * FROM tgt;
ID VAL
-- ---
1 x
2 y

The same result could be achieved with a MERGE statement (which deserves its own Documentation article), and I personally prefer MERGE in these cases, but the reason is not that "Oracle doesn't do updates with joins." As this example shows, Oracle does do updates with joins.

Conclusion

In this page (written and validated by ) you learned about Oracle Update with Joins . What's Next? If you are interested in completing Oracle tutorial, your next topic will be learning about: Oracle Functions.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.