SQL reference (MySQL and MariaDB dialects)

CREATE TRIGGER and DROP TRIGGER

Row triggers BEFORE and AFTER INSERT, UPDATE and DELETE with NEW and OLD, where they run and what is not done yet.

A trigger runs a stored-program body for every row that an INSERT, UPDATE or DELETE changes in a table (D88). The body is the same language as a routine's (see CREATE PROCEDURE).

Syntax#

SQL
CREATE [OR REPLACE] [DEFINER = account]
    TRIGGER [IF NOT EXISTS] [db_name.]trigger_name
    { BEFORE | AFTER } { INSERT | UPDATE | DELETE }
    ON tbl_name FOR EACH ROW
    [{ FOLLOWS | PRECEDES } other_trigger_name]
    trigger_body

DROP TRIGGER [IF EXISTS] [db_name.]trigger_name

SHOW CREATE TRIGGER trigger_name
SHOW TRIGGERS [{ FROM | IN } db_name] [LIKE 'pattern' | WHERE expr]

Arguments#

NameMeaningDefault
BEFORE / AFTERWhen the trigger runs relative to the row write.none
INSERT / UPDATE / DELETEThe event. One event per trigger.none
FOLLOWS, PRECEDESOrder among triggers of the same table, event and timing.creation order
NEW.col, OLD.colValues of the row after and before the change, as variables of the body. OLD exists for UPDATE and DELETE, NEW for INSERT and UPDATE.none
OR REPLACE, IF NOT EXISTSReplace / skip when the name exists.off

Remarks#

  • Storage. A definition is a document of the hidden table __rdb_triggers of the database, read once and kept. The triggers of a table go with it when it is dropped.
  • Where they run. INSERT (before the row is written; the AFTER triggers when the statement has written all its rows), UPDATE and DELETE (including multi-table UPDATE), REPLACE (the row in the way is a delete, then the insert) and INSERT ... ON DUPLICATE KEY UPDATE (the AFTER UPDATE triggers).
  • SET NEW.col = ... in a BEFORE trigger changes the row; the value is coerced to the column's type again.
  • An error in a trigger (SIGNAL, or a failing statement) fails the statement, which is all or nothing as before.
  • A statement of a trigger may not change the table the trigger is for (error 1442).
  • A trigger on a view is refused, as MySQL does.
  • information_schema.TRIGGERS lists them; SHOW TRIGGERS lists them with MySQL's columns.
  • The output is identical to MySQL 9.7 and MariaDB 11.4 except the account host and the times. When a trigger name exists, MariaDB's error 1359 text is Trigger 'db.name' already exists and MySQL's is Trigger already exists.
  • Privilege: the TRIGGER privilege is known to GRANT; creating a trigger is not checked against it in this version.

Not supported yet#

  • The BEFORE triggers of the update that ON DUPLICATE KEY UPDATE makes.
  • Triggers for a multi-table DELETE.
  • The order of AFTER triggers relative to later rows of the same statement: they all run after the last row.
  • Events (CREATE EVENT).

Examples#

Example (client DELIMITER //):

SQL
CREATE TRIGGER acct_bi BEFORE INSERT ON acct FOR EACH ROW
BEGIN
  IF NEW.balance < 0 THEN
    SET NEW.balance = 0;
  END IF;
  SET NEW.name = UPPER(NEW.name);
END//

CREATE TRIGGER acct_bu BEFORE UPDATE ON acct FOR EACH ROW
BEGIN
  IF NEW.balance < OLD.balance - 100 THEN
    SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'too big a withdrawal';
  END IF;
  SET NEW.updated = OLD.updated + 1;
END//

CREATE TRIGGER acct_ai AFTER INSERT ON acct FOR EACH ROW
BEGIN
  INSERT INTO audit (what) VALUES (CONCAT('insert ', NEW.name, ' ', NEW.balance));
END//

SHOW TRIGGERS;
SHOW CREATE TRIGGER acct_bi;
DROP TRIGGER IF EXISTS acct_au;

See also#