Skip to content
Advertisement

How to cascade on delete in a foreign key of a mysql table correctly?

I have three tables with foreign keys which should cascade on delete nevertheless when I run this

only the mapping in Tagmap and nothing happens in Excerpt nor Tag. What am I doing wrong?

Advertisement

Answer

This is not how foreign keys work. You delete from the parent table (here, tag, or excerpt), and then children record are automatically dropped. If you think about it, the other way around wouldn’t be safe, since there might be multiple children referencing the same parent.

So, you would typically run this query:

And the on delete cascade option of the foreign key constraint will automatically delete all records in tagmap with the corresponding excerptID.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement