Trigger on MySQL table when multiple rows deleted

北城以北 提交于 2020-01-06 08:47:46

问题


I wrote a trigger that runs before a row is deleted that updates a table summarizing the data from this table. The trigger works well when I delete a single row at a time. However, if I were to delete multiple rows at once with a statement like

DELETE FROM myTable WHERE id BETWEEN 1 and 100;

Will the trigger completely run on the first row before the next row is deleted or will the triggers run all at the same time?


回答1:


The trigger will completely run for every single row, see Trigger FAQ

A.5.3: Does MySQL 5.6 have statement-level or row-level triggers?

In MySQL 5.6, all triggers are FOR EACH ROW—that is, the trigger is activated for each row that is inserted, updated, or deleted. MySQL 5.6 does not support triggers using FOR EACH STATEMENT.

That is valid for the currently newest version, MySQL 5.7 too.




回答2:


from http://dev.mysql.com/doc/refman/5.5/en/trigger-syntax.html

The statement following FOR EACH ROW defines the trigger body; that is, the statement to execute each time the trigger activates, which occurs once for each row affected by the triggering event. In the example, the trigger body is a simple SET that accumulates into a user variable the values inserted into the amount column. The statement refers to the column as NEW.amount which means “the value of the amount column to be inserted into the new row.”

The delete transaction will only occur once, but then for every row affected by query the trigger will occur



来源:https://stackoverflow.com/questions/25318560/trigger-on-mysql-table-when-multiple-rows-deleted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!