Trigger calls in cascade deleting

2019-01-23 12:56发布

问题:

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

回答1:

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.



回答2:

To summarize the answers from @Niel de Wet and @Browny Lin:

  1. Sadly cascaded deletes do not activate triggers in MySQL.
  2. One solution is to not use cascading deletes but instead implement the automatic delete via another trigger.


回答3:

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!