EX :4
Create a row level trigger for the customers table that would fire for INSERT or UPDATE or DELETE operations performed on the CUSTOMERS table. This trigger will display the salary difference between the old & new Salary.
CUSTOMERS(ID,NAME,AGE,ADDRESS,SALARY)
STEP Table 1: Create Database , Use DB , Create Table(salary_log)
CREATE TABLE salary_log (
log_id INT AUTO_INCREMENT PRIMARY KEY,
customer_id INT,
old_salary DECIMAL(10,2),
new_salary DECIMAL(10,2),
salary_difference DECIMAL(10,2),
action_type VARCHAR(10),
log_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
INSERT Trigger
UPDATE Trigger
DELETE Trigger
DELIMITER $$
CREATE TRIGGER customers_after_delete
AFTER DELETE ON CUSTOMERS
FOR EACH ROW
BEGIN
INSERT INTO salary_log(customer_id, old_salary, new_salary, salary_difference, action_type)
VALUES(OLD.ID, OLD.SALARY, NULL, -OLD.SALARY, 'DELETE');
END$$
DELIMITER ;
No comments:
Post a Comment