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#
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#
| Name | Meaning | Default |
|---|---|---|
BEFORE / AFTER | When the trigger runs relative to the row write. | none |
INSERT / UPDATE / DELETE | The event. One event per trigger. | none |
FOLLOWS, PRECEDES | Order among triggers of the same table, event and timing. | creation order |
NEW.col, OLD.col | Values 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 EXISTS | Replace / skip when the name exists. | off |
Remarks#
- Storage. A definition is a document of the hidden table
__rdb_triggersof 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; theAFTERtriggers when the statement has written all its rows),UPDATEandDELETE(including multi-tableUPDATE),REPLACE(the row in the way is a delete, then the insert) andINSERT ... ON DUPLICATE KEY UPDATE(theAFTER UPDATEtriggers). SET NEW.col = ...in aBEFOREtrigger 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.TRIGGERSlists them;SHOW TRIGGERSlists 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 existsand MySQL's isTrigger already exists. - Privilege: the
TRIGGERprivilege is known toGRANT; creating a trigger is not checked against it in this version.
Not supported yet#
- The
BEFOREtriggers of the update thatON DUPLICATE KEY UPDATEmakes. - Triggers for a multi-table
DELETE. - The order of
AFTERtriggers relative to later rows of the same statement: they all run after the last row. - Events (
CREATE EVENT).
Examples#
Example (client DELIMITER //):
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;