Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How often do Update triggers fire on a multi-record update?

I have created an on update trigger.

If I am updating 5 records in the table in a single statement, how many times will the trigger execute? Does it change if I have multiple statements updating the records in a single transaction?

Does it execute 5 times, or only once after all the transactions are complete?

like image 262
Brijesh Patel Avatar asked Jul 03 '12 15:07

Brijesh Patel


People also ask

How many times a before update row-level trigger will execute if the update statement is affecting 10 rows?

In most cases if you update 10 rows trigger will be fired 10 times.

How many times a statement trigger is executed?

Statement-level triggers execute once for each transaction. For example, if a single transaction inserted 500 rows into the Customer table, then a statement-level trigger on that table would only be executed once.

How does after update trigger work?

An AFTER UPDATE Trigger means that Oracle will fire this trigger after the UPDATE operation is executed.

What happens if after update trigger fails?

Failure of a trigger causes the statement to fail, so trigger failure also causes rollback. For nontransactional tables, such rollback cannot be done, so although the statement fails, any changes performed prior to the point of the error remain in effect.


2 Answers

It all depends on the type of trigger you are using.

  • a row level trigger will fire for each and every row that is affected by the DML statement (note this is also true for INSERT statements that are based on a SELECT or are using a multi-row syntax to insert more than one row at a time)
  • a statement level trigger will fire once for the whole statement.

Oracle, PostgreSQL and DB2 support both, row level and statement level triggers. Microsoft SQL Server only supports statement level triggers and MySQL only supports row level triggers.

like image 124
a_horse_with_no_name Avatar answered Sep 22 '22 10:09

a_horse_with_no_name


With SQL 2008: If you are doing 1 update that updates 5 rows, the trigger should be executed only once.

That's why you have to use the tables "INSERTED" and "DELETED" to be able to detect all the modified rows.

If you are doing 5 updates that update 1 row, the trigger will be executed 5 times.

like image 38
Danielle Paquette-Harvey Avatar answered Sep 19 '22 10:09

Danielle Paquette-Harvey