I have Table_A and Table_B having Table_A a FK to Table_B. I want to delete some rows from Table B, so I have a query like
DELETE FROM TABLE_B
WHERE TABLE_B.id IN(
SELECT TABLE_A.B_id
FROM TABLE_A
WHERE condition
)
So if I try to execute this, I am getting an error of integrity constraint violated due to the FK from A.
The problem I am having is that, if I first delete the problematic rows from Table_A (those that match condition
), the inner SELECT don't return the expected result (because those rows have been already deleted), so the Delete doesn't work as expected.
I am completely noob in SQL so my question is, how can I do this? Is there a way to store the result of the inner select, then delete rows from Table_A and then delete from Table_B?
I don't want to create a new table or anything similar. Thanks.