Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete child records before I can delete the parent

Status
Not open for further replies.

nkamp

Programmer
Jan 19, 2004
35
NL
Hello,

I'm a newbie to SQL and if it is not to difficult I can succeed to write querie's. I don't much know about PL/SQL. We are working with Oracle.

The parent ERD is this.

Table 1 --> linking Table --> Table 2 --> Table 3(detail)
1 | 1 1 1 1 1 n
|--> Table 1 detail
1

Now I want to delete for every record of table 1 who doesn't have record in the table 1 detail table, delete these records.
But before I can do this I have to delete the one or more records in table 3, then in table 2 and then the records in the linking table and at last the records from Table 1.

Can anybody help or put me in a direction how to solve this?
Cascade doesn't work I think.
Linking table is I mean a table with only three UID's(Table 1, Table 3 and his self)
I mean by the numbers the relation between the tables.

Thanks in advance,

Nico
 
i think you would have a much better chance of getting a satisfactory reply if you posted this in one of the oracle forums

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
nkamp,

cascade delete on your foreign keys would certainly do the job, but I don't use cascade, as it means that something happens unexpectedly, behind the scenes.

The way to do it is to select from 3 inner join 2 inner join 1 using the FK fields, and then delete from that selection. You have to start with the grandchildren, then delete the children, to finally make it possible to delete the parent record.

Regards

T
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top