MySQL Triggers
By Prof. B.A.Khivsara
Note: The material to prepare this presentation has been taken from internet and are generated only for students reference and not for commercial use.
What is Trigger?
A SQL trigger is a set of SQL statements stored in the database catalog.
A SQL trigger is executed or fired whenever an event associated with a table occurs e.g., insert, update or delete.
A SQL trigger is a special type of stored procedure.
It is special because it is not called directly like a stored procedure.
The main difference between a trigger and a stored procedure is that a trigger is called automatically when a data modification event is made against a table whereas a stored procedure must be called explicitly.
Advantages of triggers
SQL triggers provide an alternative way to check the integrity of data.
SQL triggers can catch errors in business logic in the database layer.
SQL triggers provide an alternative way to run scheduled tasks.
By using SQL triggers, you don’t have to wait to run the scheduled tasks because the triggers are invoked automatically before or after a change is made to the data in the tables.
SQL triggers are very useful to audit the changes of data in tables.
Disadvantages of triggers
SQL triggers only can provide an extended validation and they cannot replace all the validations.
SQL triggers are invoked and executed invisible from the client applications, therefore, it is difficult to figure out what happen in the database layer.
SQL triggers may increase the overhead of the database server.
MySQL Triggers types
BEFORE INSERT – activated before data is inserted into the table.
AFTER INSERT – activated after data is inserted into the table.
BEFORE UPDATE – activated before data in the table is updated.
AFTER UPDATE – activated after data in the table is updated.
BEFORE DELETE – activated before data is removed from the table.
AFTER DELETE – activated after data is removed from the table.
Trigger Syntax
TRIGGER trigger_name
{ BEFORE | AFTER }
{ INSERT | UPDATE | DELETE }
ON tbl_name
FOR EACH ROW
trigger_body
Trigger Example-1: Create two tables emp and emp_update as shown below
Create table emp(EmpNo INT, Lname VARCHAR(50), Salary int(5));
Create table emp_audit (EmpNo INT, Lname VARCHAR(50), changedat DATETIME, action VARCHAR(50));
Trigger Example-1: Insert values in only emp table as shown below
Insert into emp values
(101, ‘John’,9000),
(102, ‘Pawar’,9000),
(103, ‘Jagruti’,7000);
Trigger Example-1 write a trigger for before update on emp MySQL>DELIMITER //
MySQL>CREATE TRIGGER t1
AFTER UPDATE ON emp
FOR EACH ROW
BEGIN
INSERT INTO emp_audit
SET EmpNo = OLD. EmpNo,
Lname= OLD.Lname,
changedat = NOW()
action = 'update';
END //
MySQL>DELIMITER ;
MySQL>SHOW TRIGGERS; // To see information about triggers
Trigger Example-1 Trigger will automatically run in background after update statement on emp
UPDATE emp SET Lname = ‘Pratik’ WHERE EmpNo = 101;
SELECT * FROM emp;
SELECT * FROM emp_audit;
Assignment
Write a database trigger on Library table.
The System should keep track of the records that are being updated or deleted.
The old value of updated or deleted records should be added in Library_Audit table.
References
• https://dev.mysql.com/doc/refman/5.7/en/triggers.html
• http://www.mysqltutorial.org/mysql-triggers.aspx
• http://www.w3resource.com/mysql/mysql-triggers.php