0% found this document useful (0 votes)
78 views2 pages

MYSQL Triggers

MYSQL Triggers

Uploaded by

Eugen T. Morar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views2 pages

MYSQL Triggers

MYSQL Triggers

Uploaded by

Eugen T. Morar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

Create employees_audit table


we create a new table named employees_audit to keep the changes of the employee records.
USE CLASSICMODELS;
CREATE TABLE employees_audit(
id INT(11) NOT NULL AUTO_INCREMENT,
employeeNumber INT(11) NOT NULL,
lastname VARCHAR(50) NOT NULL,
changedon DATETIME DEFAULT NULL,
action VARCHAR(50) DEFAULT NULL,
PRIMARY KEY (id)
)

2.Create an UPDATE Trigger


We create a BEFORE UPDATE trigger to be 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,
changedon = NOW();
END //
DELIMITER ;

UPDATE employees SET lastName = Phan WHERE employeeNumber = 1056;


SELECT * FROM employees_audit;

3.Displaying Triggers
If you want to retrieve all triggers in a database, just executing the following SQL statement
SELECT * FROM Information_Schema.Triggers
WHERE Trigger_schema = classicmodels;

4.Dropping Triggers
You can not only to view the trigger but also remove an existing one
DROP TRIGGER employees_audit. before_employee_update

You might also like