Skip to Main Content

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Ramamoorthi.

Asked: August 11, 2016 - 2:40 pm UTC

Last updated: August 12, 2016 - 2:35 pm UTC

Version: 10.1.2

Viewed 1000+ times

You Asked

Is oracle support on update cascade and on delete cascade in child table?


e.g
I want to delete the particular value from parent table column which primary key column and this column have relationship with child table column so if try to delete the record from parent table will get error,so we use on delete cascade to over come the issue.

Now my question is can we create on delete cascade and on update cascade on child table foreign key column....


and Chris said...

So you want to cascade a delete down multiple levels of children? So you can destroy your database with a single delete? ;)

If so, yes, yes you can.

Just keep adding the "on delete cascade" clause to the foreign keys.

Oracle doesn't have an "on update cascade" clause. Are you referring to "on delete set null"?

Anyway, here's the proof:

create table t1 (
  x int not null primary key
);

create table t2 (
  x int not null primary key references t1(x) on delete cascade
);

create table t3 (
  x int not null references t2(x) on delete cascade,
  y int
);

create table t4 (
  x int references t2(x) on delete set null,
  y int
);

insert into t1 values (1);
insert into t2 values (1);
insert into t3 values (1, 1);
insert into t4 values (1, 1);

commit;

delete t1;

select count(*) from t2;

COUNT(*)  
0  

select count(*) from t3;

COUNT(*)  
0  

select count(*) from t4;

COUNT(*)  
1 

select * from t4;

X  Y  
   1  


Use this power wisely! Like I said, this provides an easy way to completely wipe your database with a single command.

And in 12c, you can cascade truncates too! Not only is this like to be quicker, you can't rollback!

Is this answer out of date? If it is, please let us know via a Comment