I am trying to delete from a table(MyTable
) that has a foreign key
reference linking it to 4 other tables.
I needed to delete all the data MyTable
that is referenced by Table1
and Table2
, but NOT Table3
and Table4
. I have already deleted the data in Table1
and Table2
I tried something like this:
delete from MyTable where ID NOT IN(SELECT MyTableID FROM Table1)
delete from MyTable where ID NOT IN(SELECT MyTableID FROM Table2)
But it obviously doesn't work because if it did it would inadvertently delete the data that Table2
references.
Is there a way to delete from a table where FKs
aren't being referenced by certain tables?