I have three tables that are associated. A category contains options that user enter in values for, simplified in the following…
group
-----
id
name
active
option
------
id
group_id
name
active
user_option
-----------
id
opton_id
value
active
My question is what is the best way to handle situations where a group is deactivated. What I’ve done in the past is looked up option id’s where the group_id = the group id that is being deactivated. Then I take the list of returned id’s and deactivate the user_option like…
set active=0 where option_id in (list of ids)
Then I update option, then I update group.
I’m interested if there is a way to do this kind of thing with a trigger or cascade. I’ve never used either in MySQL and thought I’d see what the best approach to this problem is.
Adding the
ON UPDATE CASCADEto the foreign key on theUSER_OPTION.activecolumn should be all that’s necessary to implement your desired functionality, per the documentation. Assuming a foreign key constraint is already in place, use: