Thanks Mr. Tom.
Francisco Mtz., October 28, 2001 - 3:55 pm UTC
Thanks Mr. Tom for the quick response.
Your answer was very useful, but what about if the user wants to know the name of the dependant tables?
I appreciate your help.
Best regards.
October 28, 2001 - 4:07 pm UTC
then simply do this:
select 'CHILD_TABLE_1'
from dual
where exists ( select null from child_table_1 where fk = pk )
union all
select 'CHILD_TABLE_2'
from dual
where exists ( select null from child_table_2 where fk = pk )
.....
that'll return a list of child tables with rows.
Other "cascades" features...
Andre Nasser, October 29, 2001 - 9:24 am UTC
Tom,
I know Oracle implements ON CASCADE DELETES up to release 8i.
Other forms have to be implemented through code (i.e. trigers).
Do you know if in the next versions (9i,...) there will be other cascade options like:
- ON UPDATE CASCADES;
- ON DELETE NULLIFIES;
- etc...
that already exist in other rdms's a long time ?
Thanks again !
October 29, 2001 - 10:16 am UTC
Well, with regards to ON UPDATE CASCADE, I hope we never implement it. You should never need it (if you do, you've done your design utterly wrong).
See
http://asktom.oracle.com/~tkyte/update_cascade/index.html
for a way to do it in 7.x and up however (in 90% of the cases). Should give you an appreciation of how nasty this is.
Additionally, you could create your constraints DEFERRABLE, set them to be deferred and easily accomplish the update cascade via a procedure (but again, if you are finding a constant, recurring need for this -- YOU'VE DONE YOUR JOB WRONG).
For the ON DELETE SET NULL
ops$tkyte@ORA815.US.ORACLE.COM> create table p ( x int primary key );
Table created.
ops$tkyte@ORA815.US.ORACLE.COM> create table c ( x references p on delete set null );
Table created.
works in Oracle8i release 1 (815) and up.
As for the "etc" -- I'm not aware of any "etc's" to be handled beyond these two.
Thanks
Andre Whittick Nasser, October 29, 2001 - 11:21 am UTC
I was now aware of the ON DELETE SET NULL feature, starting in 8.1.5.
ON UPDATE CASCADE nasty ?
Andre Whittick Nasser, October 29, 2001 - 3:20 pm UTC
Tom,
Sorry, but I did not quite get why this is so bad.
Just a note... If I am not mistaken, DB2 has this feature, which set the standard for SQL. By the way I studied DBMSs using Date's book based on DB2's SQL.
I imagine a very simple case: I want to update the DEPTNO primary key on DEPT table, and I want all chldren in EMP to have the DEPTNO foreign key updated.
What could be so bad ?
Locking ? Let us set up the right indexes.
October 29, 2001 - 3:53 pm UTC
It depends on what flavor of DB2 you have -- some got it, some don't (like some have hash partitioning, some have range, some don't have hash, some don't have range). You'd have to specify which one of the umpteen DB2's you were talking about.
The first rule of the relational model is that primary keys are immutable. If you need to update the primary key of the dept table -- its not a primary key, it might be a surrogate key, it might be a column that needs to be unique, but it is not the primary key.
Its just a design issue, if the key might change, its not the key. Something else must be the key (even if that something else is a system generated key).
Sagi, December 04, 2002 - 9:42 am UTC
Hi Tom,
As usual learning alot from your site.
I never new that you can do "SET NULL" in ON DELETE CASCADE constraint.
Which Data Dictionary will tell me whether it was
ON DELETE CASCADE or
ON DELETE SET NULL
I looked into USER_CONSTRAINTS and DBA_CONSTRAINTS.
It did not show any information there. The Column DELETE_RULE in both shows "NO ACTION".
Is there any method or only method is insert a row in parent & child and delete it and see what happens?
Thanx in Advance.
Regards,
Sagi
December 04, 2002 - 12:29 pm UTC
It is an oversight -- they forgot to add it to the decode in the *_constraints views.
It is fixed in 902 and up.
If you read out the view text for *_CONSTRAINTS, you'll see a decode on c.refact. It should be:
decode(c.type#, 4,
decode(c.refact, 1, 'CASCADE', 2, 'SET NULL', 'NO ACTION'),
In prior releases the decode on 2 was missing. Now -- DO NOT MODIFY dba_constraints, user_constraints, all_constraints -- but you can create "my_dba_constraints" using this modified decode.
On delete cascade
Laxman S Kondal, November 03, 2004 - 4:37 pm UTC
Hi Tom
After reading R92 document I found
--
ON DELETE Clause
The ON DELETE clause lets you determine how Oracle automatically maintains referential integrity if you remove a referenced primary or unique key value. If you omit this clause, then Oracle does not allow you to delete referenced key values in the parent table that have dependent rows in the child table.
Specify CASCADE if you want Oracle to remove dependent foreign key values.
Specify SET NULL if you want Oracle to convert dependent foreign key values to NULL.
--
Here it says dependent foreign key values. And surprisingly I found table created by some one had this:
alter table MODEL_RUNS
add constraint MDR_MDR_FK foreign key (PARENT_MULTI_MDR_ID)
references MODEL_RUNS (MDR_ID) on delete cascade;
To know the effect of this I altered EMP table like this
ALTER TABLE Emp ADD CONSTRAINT fk2_deptno
FOREIGN KEY (deptno)
REFERENCES scott.Dept (deptno) ON DELETE CASCADE;
and then:
scott@ORA9I> delete from emp where deptno=20;
5 rows deleted.
Elapsed: 00:00:00.00
scott@ORA9I> select * from dept where deptno=20;
DEPTNO DNAME LOC
---------- -------------- -------------
20 RESEARCH DALLAS
Elapsed: 00:00:00.00
scott@ORA9I>
only EMP records were deleted.
Could you please explain what's the purpose and why should it be done and what could be the effect.
This also never gave any error while adding constraint or deleting.
Thnaks.
November 05, 2004 - 11:10 am UTC
it cascades DOWN, not UP.
a delete on the REFERENCED table (DEPT in this case) will "cascade down" to the EMP table.
delete from dept where deptno = 20;
will delete from EMP and DEPT.
Laxman S Kondal, November 05, 2004 - 2:00 pm UTC
Thanks Tom and you made it simple - cascade down
ALTER the ON DELETE clause of a FK constraint?
Brad, July 29, 2005 - 12:44 pm UTC
It appears that the only way to remove the ON DELETE clause is to drop and re-create the constraints. This seems like something that one should be able to ALTER -- am I just missing it?
Oracle v9.2.0.6
July 29, 2005 - 2:55 pm UTC
you cannot alter a constraint, no. (well, beyond enable/disable type stuff, you cannot modify that constraint)
cascade delete performance
Vad, May 23, 2008 - 12:14 pm UTC
Hi Tom,
we have some performance issue with cascade deletes.
we have a bunch of tables, around 40, which are children of 3 main tables. many of the tables has more than 10 million records. i have to delete around 100K old records. we have indexes on all foreign keys.
our DBAs suggested not to use cascade to delete data. They were telling that context has to switch for every child to delete data from it.
we implemented that and it turned out to be a disaster as main tables are taking 5hrs each to delete even though we deleted the children's data before them.
we do you suggest?
go by cascade?
is there anyway that i can hint saying that don't check for children while deleting?
or is there any better way to solve the problem?
May 23, 2008 - 6:15 pm UTC
... They were telling that context has to switch for every child to delete data from it....
no, it doesn't. you are in sql, you stay in sql - it does sql to do the sql, but it just is all in sql.
you have to delete 100k records - but how many child records go with that. What are your expectations and what are you observing
modify constraint error
Reader, August 13, 2008 - 1:33 pm UTC
Tom,
Could you please help me with this error. What did I do wrong because I followed syntax diagram.
Thank you for your time
SQL> alter table emp modify constraint fk_deptno references dept (deptno) on delete cascade;
alter table EMP modify constraint fk_deptno references DEPT (deptno) on delete cascade
*
ERROR at line 1:
ORA-00933: SQL command not properly ended
modify constraint error
Reader, August 13, 2008 - 1:56 pm UTC
Tom,
I forgot to mention in my above post that I was trying to modify constraint adding 'ON DELETE CASCADE'
Thank you.
August 18, 2008 - 9:37 am UTC
see above
on cascade delete
Ankit, November 03, 2008 - 9:09 am UTC
Hi tom
I know you dont accept new questions but please answer this one, its really really important,answer it just this time only.
I have child and parent tables , now i havent defined ON CASCADE DELETE clause in create table defination, now if i want to delete the rows from both parent and child tables i cant.. coz i dont have ON CASCADE DELETE clause .
Now i know the way you said before also that by droping the constaint and add it again using alter table....on cascade delete.
The reason i dont want to use this mehtod is because i have lots of tables and it would be a long process besides if someone accidently deletes rows from parent table then rows from child table would alos be deleted.
I hope you understand my problem, please suggest any other approach for this problem.
November 10, 2008 - 3:10 pm UTC
there is no other approach, you cannot alter the existing constraint.
... if someone accidently deletes rows from
parent table then rows from child table would alos be deleted. ...
that made no sense, your goal is to have the child records deleted.
Thankyou
Ankit, November 12, 2008 - 7:19 am UTC
Thanks for the response
having question
sathya, January 30, 2011 - 7:59 pm UTC
i created one parent table and one child table.
now i want to delete only the parent record.i must have the child records.is there any possible way
February 01, 2011 - 4:23 pm UTC
update the foreign key value to NULL.
You can also create the foreign key with "on delete set null"