Introduction To MySQL Triggers
Introduction To MySQL Triggers
When you use a statement that makes change to the table but does not use INSERT,
DELETE or UPDATE statement, the trigger is not invoked. For example, the TRUNCATE
statement removes the whole data of a table but does not invoke the trigger associated
with that table.
There are some statements that use the INSERT statement behind the scenes such as
REPLACE statement and LOAD DATA statement. If you use these statements, the
corresponding triggers associated with the tables if available will be invoked.
Triggers defined for a table must have a unique name. You can have the same trigger
name that defines for different tables but it is not recommended. In practice, the names
of triggers follow the following naming convention:
You can back up the MySQL triggers by copying the trigger files to the backup folder.
You can also backup the triggers using the mysqldump tool.
Use SHOW, LOAD DATA, LOAD TABLE, BACKUP DATABASE, RESTORE, FLUSH
and RETURN statements.
Use statements that commit or rollback implicitly or explicitly such as
COMMIT, ROLLBACK, START TRANSACTION, LOCK/UNLOCK TABLES, ALTER,
CREATE, DROP, RENAME, etc.
Use prepared statements such as PREPARE, EXECUTE, etc.
Use dynamic SQL statements.
Call a stored procedure or stored function.
You put the trigger name after the CREATE TRIGGER statement. The trigger
name should follow the naming convention [trigger time]_[table
name]_[trigger event], for example before_employees_update.
Trigger activation time can be BEFORE or AFTER. You must specify the
activation time when you define a trigger. You use BEFORE keyword if you
want to process action prior to the change is made on the table and AFTER
if you need to process action after the change is made.
Trigger event can be INSERT, UPDATE or DELETE. This event causes trigger
to be invoked. A trigger only can be invoked by one event. To define a
trigger that is invoked by multiple events, you have to define multiple
triggers, one for each event.
A trigger must be associated with a specific table. Without a table trigger
would not exist therefore you have to specify the table name after the ON
keyword.
The SQL statements are placed between BEGIN and END block.
The OLD and NEW keywords are very handy. The OLD keyword refers to the
existing record before you change the data and the NEW keyword refers to
the new row after you change the data.
Second, we create a new table named employees_audit to keep the changes of the
employee records. The following script creates the employee_audit table.
DELIMITER $$
CREATE TRIGGER before_emplo
BEFORE UPDATE ON employe
FOR EACH ROW BEGIN
1 DELIMITER $$
2 CREATE TRIGGER before_employee_update
3 BEFORE UPDATE ON employees
4 FOR EACH ROW BEGIN
5
6 INSERT INTO employees_audit
7 SET action = 'update',
8 employeeNumber = OLD.employeeNumber,
9 lastname = OLD.lastname,
1 changedon = NOW();
0 END$$
1 DELIMITER ;
1
1
2
If you take a look at the schema, you will see before_employee_update trigger under
the employees table as follows:
Now it’s time to update an employee record to test if the trigger is really invoked.
UPDATE employees
SET lastName = 'Phan'
WHERE employeeNumber = 105
1 UPDATE employees
2 SET lastName = 'Phan'
3 WHERE employeeNumber = 1056
To check if the trigger was invoked by the UPDATE statement, we can query the
employees_audit table by using the following query:
SELECT *
FROM employees_audit
1 SELECT *
2 FROM
employees_audit
As you see, our trigger was really invoked so that we have a new record in the
employees_audit table.
In this tutorial, you have learned how to create a trigger in MySQL. We also shown you
how to develop a trigger to audit the changes of the employees table.