Question: Can you give me some tips on when to
implement the "on delete cascade" vs. the "on delete
restrict" feature of a foreign key constraint?
Answer: The choice between on delete restrict or
on delete cascade depends on the design of your
application. You have three choices for managing deletes on
Oracle foreign key constraints:
alter
table sample1
add foreign key (col1)
references
sample (col2)
on delete no action;
alter table sample1
add foreign key (col1)
references
sample (col2)
on delete restrict;
alter table sample1
add foreign key (col1)
references sample (col2)
on delete cascade;
When you create a foreign key constraint, Oracle default to
"on delete restrict" to ensure that a parent rows cannot be deleted
while a child row still exists.
However, you can also implement
on delete cascade to delete all child rows when a parent row is
deleted.
Using "on delete cascade" and "on delete restrict" is used
when a strict one-to-many relationship exists such that any "orphan"
row violates the integrity of the data.
Also, see these important notes on
foreign key indexing,
especially important if you delete or update parent rows.
Many systems use "on delete cascade" when they have ad-hoc
updates so that the end-user does not have to navigate the child
table and delete dozens or hundreds of child entries. Of
course, using "on delete cascade" is dangerous because of possible
mistakes and because issuing a single delete on a parent row might
invoke thousands of deletes from the child table.
Obviously, if you are using "on delete cascade" and you
do not create an index on the child parent key the deletion of a
parent row would require a full-table scan of the child table, to
find and delete the child rows.
|
|
|
|
Guarantee your Success!
Oracle is the
world's most complex, robust and flexible database, considered
impossible to master without a mentor.
That's why all BC
Oracle trainers are working professionals, experts in Oracle who
share their tips and secrets. |
|
| |
|
Burleson is the American Team

Note:
This Oracle
documentation was created as a support and Oracle training reference for use by our
DBA performance tuning consulting professionals.
Feel free to ask questions on our
Oracle forum.
Verify
experience!
Anyone
considering using the services of an Oracle support expert should
independently investigate their credentials and experience, and not rely on
advertisements and self-proclaimed expertise. All legitimate Oracle experts
publish
their Oracle
qualifications.
Errata?
Oracle technology is changing and we
strive to update our BC Oracle support information. If you find an error
or have a suggestion for improving our content, we would appreciate your
feedback. Just
e-mail:
and include the URL for the page.
Copyright ? 1996 - 2012
All rights reserved.
Oracle ?
is the registered trademark of Oracle Corporation.
|
|