Trigger on MySQL table when multiple rows deleted

2019-08-31 04:34发布

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?

2条回答
等我变得足够好
2楼-- · 2019-08-31 05:15

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

查看更多
欢心
3楼-- · 2019-08-31 05:29

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.

查看更多
登录 后发表回答