I am learning how to construct Triggers in MYSQL, I have constructed a Trigger that will recalculate the Department Salary if one of the Department Employees is updated (either their salary is updated or they are added to/removed from the department). If I update a user's salary, it is not updating the departments salary as well.
Employee Table:
SSN
Salary
Name
Dno
Supervisor_ssn
Department Table:
DName
Dno
Total_sal
Manager_SSN
Trigger looks as follows:
DELIMITER $$
CREATE TRIGGER `Employee_AUPD` AFTER UPDATE ON `Employee` FOR EACH ROW
BEGIN
IF old.salary != new.salary THEN
UPDATE department SET total_sal = total_sal - old.salary
WHERE new.dno = department.dno;
UPDATE department SET total_sal = total_sal + new.salary
WHERE new.dno = department.dno;
END IF;
IF old.dno != new.dno THEN
UPDATE department SET total_sal = total_sal - old.salary
WHERE old.dno = department.dno;
UPDATE department SET total_sal = total_sal + new.salary
WHERE new.dno = department.dno;
END IF;
END;
DELIMITER;