Trigger Overview
A comprehensive guide to triggers, explaining their execution timing (BEFORE/AFTER), supported events, and how they interact with storage engines.
A trigger is a set of statements that run when, or are triggered by, an event that occurs on a table.
Events
The event can be an INSERT, an UPDATE, or a DELETE. The trigger can be executed BEFORE or AFTER the event. A table can have multiple triggers defined for the same event and timing combination.
The LOAD DATA INFILE and LOAD XML statements invoke INSERT triggers for each row that is being inserted.
The REPLACE statement is executed with the following workflow:
BEFORE INSERT;BEFORE DELETE(only if a row is being deleted);AFTER DELETE(only if a row is being deleted);AFTER INSERT.
The INSERT ... ON DUPLICATE KEY UPDATE statement, when a row already exists, follows the following workflow:
BEFORE INSERT;BEFORE UPDATE;AFTER UPDATE.
Otherwise, it works like a normal INSERT statement.
Note that TRUNCATE TABLE does not activate any triggers.
Triggers and Errors
With non-transactional storage engines, if a BEFORE statement produces an error, the statement isn't executed. Statements that affect multiple rows fail before inserting the current row.
With transactional engines, triggers are executed in the same transaction as the statement that invoked them.
If a warning is issued with the SIGNAL or RESIGNAL statement (that is, an error with an SQLSTATE starting with '01'), it is treated like an error.
Creating a Trigger
Here's a simple example to demonstrate a trigger in action. Using these two tables as an example:
We want to increment a counter each time a new animal is added. Here's what the trigger looks like:
The trigger has:
a name (in this case,
increment_animal),a trigger time (in this case, after the specified trigger event),
a trigger event (an
INSERT),a table with which it is associated (
animals),a set of statements to run (here, just the one
UPDATEstatement).
AFTER INSERT specifies that the trigger runs after an INSERT. The trigger could also be set to run before, and the statement causing the trigger could be a DELETE or an UPDATE as well. You can also have multiple triggers for an action. In this case, you can use FOLLOWS | PRECEDES`` other_trigger_name to specify the order of the triggers.
The set of statements to run are the statements on the table of the trigger; therefore, columns/values that change are always just a column name or an expression like NEW.column_name. Table references of other tables must come from explicit table references.
Now, if we insert a record into the animals table, the trigger runs, incrementing the animal_count table.
For more details on the syntax, see CREATE TRIGGER.
Dropping Triggers
To drop a trigger, use the DROP TRIGGER statement. Triggers are also dropped if the table with which they are associated is also dropped.
Triggers Metadata
The Information Schema TRIGGERS Table stores information about triggers.
The SHOW TRIGGERS statement returns similar information.
The SHOW CREATE TRIGGER statement returns a CREATE TRIGGER statement that creates the given trigger.
More Complex Triggers
Triggers can consist of multiple statements enclosed by a BEGIN and END. If you're entering multiple statements on the command line, temporarily set a new delimiter so that you can use a semicolon to delimit the statements inside your trigger. See Delimiters in the mariadb client for more.
Trigger Errors
If a trigger contains an error and the engine is transactional, or it is a BEFORE trigger, the trigger will not run, and will prevent the original statement from running as well. If the engine is non-transactional, and it is an AFTER trigger, the trigger will not run, but the original statement will.
Here, we'll drop the above examples and then recreate the trigger with an error, a field that doesn't exist, first using the default InnoDB, a transactional engine, and then again using MyISAM, a non-transactional engine.
And now the identical procedure, but with a MyISAM table.
The following example shows how to use a trigger to validate data. The SIGNAL statement is used to intentionally produce an error if the email field is not a valid email. As the example shows, in that case, the new row is not inserted (because it is a BEFORE trigger).
See Also
This page is licensed: CC BY-SA / Gnu FDL
Last updated
Was this helpful?

