Delete child records before I can delete the parent
Delete child records before I can delete the parent
(OP)
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'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
RE: Delete child records before I can delete the parent
r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
RE: Delete child records before I can delete the parent
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