Suppose that we have 3 related table in this manner in MySQL:
Table 1: Table 2: Table 3: ------------- ------------- ------------- UserID | ProfileID | ArticleID | ------------- ------------- ------------- Username | UserID | UserID | ------------- ------------- ------------- Password | UserAvatar | Article | ------------- ------------- -------------
I need to delete every related records upon delete action on Table1. in other words, when user performs delete action on Table 1, related records from other related tables must be deleted automatically and not write tons of queries to achieve this or we have orphan records without its master record.
what should I do?
what are the best practices?
Thanks.
What you want is to perform a cascade delete. Here is what you have to do:
foreign keysbetween the tables using UserID as the linkON DELETE CASCADEAnd you are done.
Update
Not gonna give you teh codez. All you need to read is here. Not gonna give you the fish, I’m teaching you how to catch your own fish.