MySQL - MyISAM multi-table delete (similar to ON CASCADE DELETE)

784 Views Asked by At

I'm looking to delete all entries that are referenced by a record, and all the children in different tables as well. If possible I'd like to use a multi-table delete statement as opposed to triggers.

For example

Table: forms
id     var
1      foo
2      bar

Table 2: form_options
id     form_id     var
1      1           blah
2      2           hello
3      2           world

Table 3: form_options_info
id     form_options_id     var
1      3                   world info

So given the above type of table struct, if I delete row 2 from forms that would delete row 2,3 from form_options as well as row 1 from form_options_info.

1

There are 1 best solutions below

0
On

Maybee not the best solution, but it works:

DELETE FROM form_options_info, form_options, forms
  USING forms INNER JOIN form_options INNER JOIN form_options_info
  WHERE (form_options_info.form_options_id = form_options.id 
    AND form_options.form_id = forms.id
    OR form_options.form_id = forms.id)
    AND forms.id = 2;

...or just change the tables to InnoDB ;-)...