I have 4 tables that suppose: t1.id
is PK
and t2.id
, t3.id
, t4.id
are FK
, now,
how can I delete a specific row (id
) of all tables?
Of course, I searched but I couldn't find my answer and am confused!!!
Please help me.
I have 4 tables that suppose: t1.id
is PK
and t2.id
, t3.id
, t4.id
are FK
, now,
how can I delete a specific row (id
) of all tables?
Of course, I searched but I couldn't find my answer and am confused!!!
Please help me.
You can change the foreign on t2, t3 and t4 to have CASCADE DELETE.
So when a row is removed in t1, it automatically removes child rows from t2, t3 and t4
Sometimes, you can't have or don't want cascading, so you need to do it manually, like this question on cascade delete on a table with two FK to the same table
delete from t2 where t2.Id = <id>
delete from t3 where t3.Id = <id>
delete from t4 where t4.Id = <id>
delete from t1 where t1.Id = <id>
Not sure if I mis-understood your question. But deleting a specific row from a table only needs the id of the row. Also, when deleting entries from parent-child relationship relational schema, you have to ensure that the entry from parent table is deleted only after all the child entries are deleted.