How to create a Foreign Key with "ON UPDATE CASCADE" on Oracle?
Asked Answered
I

3

8

In MS SQL Server it is possible to create a foreign key with ON UPDATE CASCADE option, so whenever you update one of the columns in the primary key, the foreign keys in other tables will also be update by the DBMS.

So, how to do it in Oracle?

Incombustible answered 17/8, 2009 at 19:20 Comment(0)
D
4

Oracle does not allow a Foreign Key constraint with “ON UPDATE CASCADE”.

Here are a couple of options you have.

Create the Foreign Key, and create an “On Update” trigger. Make use of the package below (needs to be installed in the db).

http://tkyte.blogspot.com/2009/10/httpasktomoraclecomtkyteupdatecascade.html

Let me know if you have additional questions or need more information.

Downing answered 17/8, 2009 at 20:32 Comment(3)
The Post is old to comment but I see a dead URL. Being inquisitive here; If anyone happens to know the updated URL, pls. let us know.Yoga
@Yoga The post at asktom.oracle.com/pls/asktom/… may contain the same information.Burkholder
As usual, link is dead and no code given in answer, just hints...Sarcoma
D
2

Would a database trigger do the job for you ?

Here is the Oracle doc on the subject of Data Integrity for 11g (just incase you were interested).

Declinate answered 17/8, 2009 at 20:21 Comment(0)
A
0

You can't use on update cascade, but you can create a trigger that will resolve the issue:

create table tab1(
pk int PRIMARY KEY,
aa int);

create table tab2(
pk int PRIMARY KEY,
tab1_pk int,
FOREIGN KEY(tab1_pk) REFERENCES tab1(pk));

------------------------------------------

create or replace trigger tab1_pkUpdate
    after update of pk on tab1
    for each row
 begin
    update tab2 s
    set s.tab1_pk = :new.pk
    where s.tab1_pk = :old.pk;
end;
/
Arrive answered 10/10, 2022 at 14:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.