Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger calls in cascade deleting

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

like image 687
user758690 Avatar asked May 18 '11 07:05

user758690


People also ask

Is on delete cascade a trigger?

Sadly cascaded deletes do not activate triggers in MySQL.

What happens if the on delete cascade clause is set?

Use the ON DELETE CASCADE option to specify whether you want rows deleted in a child table when corresponding rows are deleted in the parent table. If you do not specify cascading deletes, the default behavior of the database server prevents you from deleting data in a table if other tables reference it.

Is it good to use on delete cascade?

Yes, the use of ON DELETE CASCADE is fine, but only when the dependent rows are really a logical extension of the row being deleted. For example, it's OK for DELETE ORDERS to delete the associated ORDER_LINES because clearly, you want to delete this order, which consists of a header and some lines.

How does Cascade on delete work?

A foreign key with cascade delete means that if a record in the parent table is deleted, then the corresponding records in the child table will automatically be deleted. This is called a cascade delete in SQL Server.


2 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:

  • MySQL: https://bugs.mysql.com/bug.php?id=11472
  • MariaDB: https://jira.mariadb.org/browse/MDEV-19402
like image 115
Niel de Wet Avatar answered Oct 05 '22 19:10

Niel de Wet


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.
like image 27
jcoffland Avatar answered Oct 05 '22 18:10

jcoffland