Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Oracle delete from tables with referential integrity

Posted on 2016-07-25
5
Medium Priority
?
131 Views
Last Modified: 2016-08-16
Experts,
I have 3 tables say a, b and c. table a has the primary key 'a_id' and table b has the primary key 'b_id' and also foreign key from table a as 'a_id'. Table c has the primary key 'c_id' and the foreign key from table b which is 'b_id'.
If I want to delete a record from table a which has child record in table b and the child record in table b has a child record in table c.

can it be done in a single delete statement?
0
Comment
Question by:smalig
  • 3
5 Comments
 
LVL 32

Expert Comment

by:awking00
ID: 41728285
If you disable the foreign keys for tables b and c and table a was created with on delete cascade. You should be able to delete the record from table a. However, you would need to delete the child records from b and c before you could re-enable the foreign keys.
0
 
LVL 78

Accepted Solution

by:
slightwv (䄆 Netminder) earned 2000 total points
ID: 41728290
If you set the constraints to cascade on delete, then a single delete will remove all the children as well.
0
 
LVL 32

Expert Comment

by:awking00
ID: 41728291
I meant to say if the delete rule for b and c is not cascade, you would need to remove the child records first before re-enabling.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41728292
in other terms: with the referential constraints ( foreign key ) enabled, you delete from table a only

ogherwise you delete from c first, then from b, finally from a
0
 
LVL 32

Expert Comment

by:awking00
ID: 41728295
slightwv is right. I should have stopped my initial response after the second sentence.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question