I have been trying to learn from reading tutorials online and stuff but I just can’t put my finger on it.
I have 2 tables at the moment (i’ll have a lot more later on as I build my application) so I want to knock out this issue before expanding and coding.
I have my tables set to use InnoDB and I have each table related to each other by using user_id as foreign keys.
If i issue a DELETE query on the main users table, how can i get all records from other tables that are linked to the user_id field get deleted as well?
I know its simple, but I think I just need to ask the question myself so I can understand the answer rather than reading the answer… heh
thank a lot for any help.
Since they are InnoDB tables with proper FK relationships, you can simply use
ON DELETE CASCADEin the foreign key definition. For example in one of the related tables:However, this requires altering your existing schema to modify the foreign keys.
See the MySQL
FOREIGN KEYdocs for complete information.