MySQL trigger syntax
Update one table based on another table. This can be achieved when changes are made by using triggers.
In order to create a new trigger, you use the CREATE TRIGGER statement. The following illustrates the syntax of the CREATE TRIGGER statement:
CREATE TRIGGER trigger_name trigger_time trigger_event ON table_name FOR EACH ROW BEGIN ... END;
Let’s examine the syntax above in more detail.
- 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 examplebefore_employees_update
. - Trigger activation time can be
BEFORE
orAFTER
. You must specify the activation time when you define a trigger. You use theBEFORE
keyword if you want to process action prior to the change is made on the table andAFTER
if you need to process action after the change is made. - The trigger event can be
INSERT
,UPDATE or
DELETE
. This event causes the 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. - You place the SQL statements between
BEGIN
andEND
block. This is where you define the logic for the trigger.
MySQL trigger example
Let’s start creating a trigger in MySQL to log the changes of the employees table.
First, create a new table named employees_audit
to keep the changes of the employee
table. The following statement creates the employee_audit
table.
CREATE TABLE employees_audit ( id INT AUTO_INCREMENT PRIMARY KEY, employeeNumber INT NOT NULL, lastname VARCHAR(50) NOT NULL, changedat DATETIME DEFAULT NULL, action VARCHAR(50) DEFAULT NULL );
Next, create a BEFORE UPDATE
trigger that is invoked before a change is made to the employees
table.
DELIMITER $$ CREATE TRIGGER before_employee_update BEFORE UPDATE ON employees FOR EACH ROW BEGIN INSERT INTO employees_audit SET action = 'update', employeeNumber = OLD.employeeNumber, lastname = OLD.lastname, changedat = NOW(); END$$ DELIMITER ;
Inside the body of the trigger, we used the OLD
keyword to access employeeNumber
and lastname
column of the row affected by the trigger.
Notice that in a trigger defined for INSERT, you can use NEW
keyword only. You cannot use the OLD
keyword. However, in the trigger defined for DELETE, there is no new row so you can use the OLD
keyword only. In the UPDATE trigger, OLD
refers to the row before it is updated and NEW
refers to the row after it is updated.
Then, to view all triggers in the current database, you use SHOW TRIGGERS
statement as follows:
SHOW TRIGGERS;
In addition, if you look at the schema using MySQL Workbench under the employees > triggers, you will see the before_employee_update
trigger
After that, update the employees
table to check whether the trigger is invoked
UPDATE employees SET lastName = 'Phan' WHERE employeeNumber = 1056;
Finally, to check if the trigger was invoked by the UPDATE
statement, you can query the employees_audit
table using the following query
SELECT * FROM employees_audit;
You will see the trigger was invoked as it has inserted a new row into the employees_audit
table.
Let’s say you only wish to insert a new row into employees_audit
when a particular column has changed value. In the example below a new row will only be inserted if the employeeNumber
has changed.
DELIMITER $$ CREATE TRIGGER before_employee_update BEFORE UPDATE ON employees FOR EACH ROW BEGIN IF NEW.employeeNumber <> OLD.employeeNumber THEN INSERT INTO employees_audit SET action = 'update', employeeNumber = OLD.employeeNumber, lastname = OLD.lastname, changedat = NOW(); END IF; END$$
In this tutorial, you have learned how to create a trigger in MySQL. We also showed you how to develop a trigger to audit the changes of the employees
table.
A Full Example
DELIMITER $$ CREATE TRIGGER table1_bi BEFORE INSERT ON table1 FOR EACH ROW BEGIN INSERT INTO table2 (ID, ColABc, Col2, Col3) VALUES (NEW.ID, NEW.ColABc, NEW.Col2, NEW.Col3); END $$ CREATE TRIGGER table1_bu BEFORE UPDATE ON table1 FOR EACH ROW BEGIN UPDATE table2 SET ID = NEW.ID, ColABc = NEW.ColABc, Col2 = NEW.Col2, Col3 = NEW.Col3 WHERE ID = OLD.ID; END $$ CREATE TRIGGER table1_bd BEFORE DELETE ON table1 FOR EACH ROW BEGIN DELETE FROM table2 WHERE ID= OLD.ID; END $$ DELIMITER ;
Comments (0)