Support for triggers is included beginning with MySQL 5.0.2. 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 an
INSERT
,
DELETE
, or
UPDATE
statement executes for the
associated table. A trigger can be set to activate either before or
after the triggering statement. 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 are activated by SQL statements
only. They are not activated by changes in
tables made by APIs that do not transmit SQL statements to the
MySQL Server; in particular, they are not activated by updates
made using the NDB
API.
To use triggers if you have upgraded to MySQL 5.0 from an older release that did not support triggers, you should upgrade your grant tables so that they contain the trigger-related privileges. See Section 4.4.9, “mysql_upgrade — Check Tables for MySQL Upgrade”.
The following discussion describes the syntax for creating and dropping triggers, and shows some examples of how to use them.
Additional Resources
You may find the Triggers User Forum of use when working with views.
For answers to some commonly asked questions regarding triggers in MySQL, see Section A.5, “MySQL 5.0 FAQ — Triggers”.
There are some restrictions on the use of triggers; see Section D.1, “Restrictions on Stored Routines and Triggers”.
Binary logging for triggers takes place as described in Section 18.5, “Binary Logging of Stored Programs”.
User Comments
A DROP DATABASE on a database with triggers fails, so you must drop triggers before droping database.
A DROP TRIGGER fails if the table doesn't exists, so drop tiggers before.
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
begin
set @id=new.entity;
set @attribute=new.attribute;
set @value=new.value;
update pivot
set
Author=(select if(@attribute='Author',@value,Author)),
Title=(select if(@attribute='Title',@value,Title)),
Publisher=(select if(@attribute='Publisher',@value,Publisher))
where
id=@id;
end
[root@shamun ~]# mysql
2 rows in set (0.00 sec)Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 23
Server version: 5.1.41 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use shamun
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
mysql> delimiter ;;
mysql> create trigger foo
-> after insert on forum
-> for each row
-> begin
-> insert into contacts (email) values ('example@abc.com');
-> end
-> ;;
Query OK, 0 rows affected (0.31 sec)
mysql> delimiter ;
// daily query
mysql> insert into forum (status) values ('open');
Query OK, 1 row affected, 13 warnings (0.00 sec)
mysql> select *from forum;
1 row in set (0.00 sec)
mysql> select email from contacts;
1 row in set (0.00 sec)
mysql>
Add your own comment.