Skip to main content

What does a row-level trigger do?

A row-level trigger is a trigger that fires for every row affected by an INSERT, UPDATE, or DELETE operation.

So if a query changes 5 rows, the trigger runs 5 times, once per row.

Inside such a trigger, special pseudo-records are available:

  • NEW, the new data (after the change or insert),
  • OLD, the old data (before the change or delete).

Example

sql
CREATE TRIGGER check_age BEFORE INSERT ON users FOR EACH ROW BEGIN IF NEW.age < 18 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Age must be 18+'; END IF; END;

Here the trigger checks every row being inserted and blocks adding a user under 18.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.