I have a table ParentTable in which I have parentId as primary key.
Now I have three child tables ChildTable1, ChildTable2 and ChildTable3.
-
in
ChildTable1, primary key isch1Idand foreign key isfkparentId(pk fromParentTable) -
in
ChildTable2, primary key isch2Idand foreign keys arefkparentIdandfkch1Id(primary key fromChildTable1) -
in
ChildTable3, primary key isch3Idand foreign keys arefkparentId, fkch1Idandfkch2Id(primary key fromChildTable2).
And this goes on..
I want to write a query to delete these tables hierarchially in the following manner.
First delete ChildTable3 > then delete ChildTable2 > then delete ChildTable1 and finally delete ParentTable.
I have a vague idea of doing this like
delete from ChildTable3
where fkch2Id = ch2.ch2Id
and fkch1Id = ch1.ch1Id
and fkparentId = p.parentId
then proceeding to ChildTable2 for deletion and so on. Hope I made it clear.
You can do same thing without any query by setting
cascade on deleteon foreign key definitin.if you don’t set foreign key definiton on db you can delete records on
fkparentIdlike this.