Search code examples
mysqlmysql-workbenchtable-relationships

HOW TO: When record on table A gets deleted all records on table B, that are associated with table A, should ALSO be deleted?


By having into consideration the following scheme:

table relations

We need to make sure that, if an association gets deleted, all the dogs that belong to that association, should also be deleted.

However, it makes sense to, while doing this, keep the relation that actually exists between Association and Dog tables, because, each association can have several Dogs, however, one Dog belong to only one Association. So I believe the foreign key configuration is correct.

I believe I should apply Cascade somewhere, but I'm not seeing where. :(

Please advice


Solution

  • Selecting 'Dog' as your target table, then selecting the 'Foreign Keys' tab, you should have a foreign key there for the 'association_id' field selected. Once you find that just check the popup value showing in the 'Foreign Key Options' section for 'On Delete' it should show 'CASCADE' (similar to what your screenshot above is showing - if it isn't showing 'CASCADE' simply select it).