Solved

Oracle delete from tables with referential integrity

Posted on 2016-07-25
5
56 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 31

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 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 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 31

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 142

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 31

Expert Comment

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

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now