Related Documentation Download this Manual
PDF (US Ltr) - 38.2Mb
PDF (A4) - 38.2Mb
PDF (RPM) - 37.1Mb
HTML Download (TGZ) - 10.2Mb
HTML Download (Zip) - 10.3Mb
HTML Download (RPM) - 9.0Mb
Man Pages (TGZ) - 206.2Kb
Man Pages (Zip) - 314.7Kb
Info (Gzip) - 3.5Mb
Info (Zip) - 3.5Mb
Excerpts from this Manual

23.3 Using Triggers

A trigger is a named database object that is associated with a table, and that activates when a particular event occurs for the table. Some uses for triggers are to perform checks of values to be inserted into a table or to perform calculations on values involved in an update.

A trigger is defined to activate when a statement inserts, updates, or deletes rows in the associated table. These row operations are trigger events. For example, rows can be inserted by INSERT or LOAD DATA statements, and an insert trigger activates for each inserted row. A trigger can be set to activate either before or after the trigger event. For example, you can have a trigger activate before each row that is inserted into a table or after each row that is updated.


MySQL triggers activate only for changes made to tables by SQL statements. This includes changes to base tables that underlie updatable views. Triggers do not activate for changes to tables made by APIs that do not transmit SQL statements to the MySQL Server. This means that triggers are not activated by updates made using the NDB API.

Triggers are not activated by changes in INFORMATION_SCHEMA or performance_schema tables. Those tables are actually views and triggers are not permitted on views.

The following sections describe the syntax for creating and dropping triggers, show some examples of how to use them, and indicate how to obtain trigger metadata.

Additional Resources

User Comments
  Posted by Gabe Holmes on June 20, 2009
A trigger can be used to do a real time pivot of an entity-attribute-value table. Suppose you have two tables, "eav" with columns entity, attribute, and value (entity+attribute are the primary key) and "pivot" with columns id,Author,Title,Publisher.
Here's the code:

create trigger ai_eav
after insert on eav
for each row
set @id=new.entity;
set @attribute=new.attribute;
set @value=new.value;
update pivot
Author=(select if(@attribute='Author',@value,Author)),
Title=(select if(@attribute='Title',@value,Title)),
Publisher=(select if(@attribute='Publisher',@value,Publisher))
  Posted by Andre Koethur on March 23, 2014
Be aware of that MySQL does foreign key checks BEFORE invoking any trigger. So it is not possible to implement a BEFORE INSERT trigger that enters up a missing column value with a foreign key constraint.
  Posted by Russell Bohlmann on July 15, 2014
Regarding a BEFORE INSERT/UPDATE TRIGGER: I have found that value TYPE checks (ie. checks for integer vs. string values) are done BEFORE the trigger is invoked but FOREIGN KEY checks are done AFTER.
  Posted by Jeff Orrok on November 28, 2016
To display a trigger use the SHOW TRIGGERS command
Sign Up Login You must be logged in to post a comment.