I have table "A" in MySQL. It has some references with cascade deleting to some other tables ("B", "C", "D" ...). I need to use a trigger when something deletes from "A". This trigger works when I delete records from "A" directly. But it doesn't work with cascade deleting. Does any version of MySQL exist where my trigger will work with cascade deleting? Or, maybe, there is another way to call
-
1This is an argument for doing the logic in application code, not a complex setup of Triggers and Cascading. – Rick James Apr 30 '16 at 18:56
4 Answers
From http://dev.mysql.com/doc/refman/5.6/en/innodb-restrictions.html
Cascaded foreign key actions do not activate triggers
In other words, you cannot use the trigger with cascaded deleting.
Also see related bugs:

- 4,539
- 4
- 39
- 47

- 7,806
- 9
- 63
- 100
-
63
-
4
-
-
-
@ahsteele I made that comment 4 years ago, and don't use MySQL anymore...so, I don't know – Neil McGuigan Sep 12 '16 at 21:19
-
3Sorry I meant to reply @yonessafari. To answer the question its still a bug: https://bugs.mysql.com/bug.php?id=11472 – ahsteele Sep 12 '16 at 21:37
To summarize the answers from @Niel de Wet and @Browny Lin:
- Sadly cascaded deletes do not activate triggers in MySQL.
- One solution is to not use cascading deletes but instead implement the automatic delete via another trigger.

- 5,238
- 38
- 43
-
1using another trigger for deleting is not a proper approach. because if I have a trigger on tableA and delete a row in tableB, and also If I have a trigger aon tableB for updating tableA, then a collision will occur. – Shafizadeh Sep 18 '15 at 21:05
Let me share how I've been "fixing" this issue from the first day I discovered it existed. I copy the trigger from the cascaded table into the first table that's deleted directly. It just works.
A lot of times it's a matter of copy/paste, and occasionally it requires extensive re-writing of the code.
The best part is, when Oracle finally fixes this Bug you only have remove the trigger code from the said table. Voila!

- 101
- 1
- 4
Yeah unfortunately this is a bug. But we do can do something to achieve what we need. Instead of defining foreign key for CASCADE update or delete you can achieve that by making a trigger.
For example if you have 2 tables categories
and subcategories
and you have a foreign key assigned to subcategories so on delete a category its subcategories should get deleted automatically. You can achieve the same functionality without foreign key and using triggers like shown below:
CREATE TRIGGER categories_delete AFTER DELETE ON categories
FOR EACH ROW BEGIN
DELETE FROM subcategories WHERE category_id = OLD.id;
END;
This will now let you do other triggers work just fine.
I hope that's helpful.

- 898
- 7
- 12