Complete the code to create a trigger that fires before an UPDATE on the employees table.
CREATE TRIGGER before_employee_update BEFORE UPDATE ON employees FOR EACH ROW BEGIN SET NEW.salary = NEW.salary * [1]; END;The trigger multiplies the new salary by 1.1 before updating, giving a 10% raise.
Complete the code to log the old salary value into a salary_changes table inside the trigger.
CREATE TRIGGER log_salary_change AFTER UPDATE ON employees FOR EACH ROW BEGIN INSERT INTO salary_changes(employee_id, old_salary) VALUES (NEW.id, [1]); END;OLD.salary holds the salary value before the update, which we want to log.
Fix the error in the trigger code to correctly prevent salary from being set below 3000.
CREATE TRIGGER check_salary BEFORE UPDATE ON employees FOR EACH ROW BEGIN IF NEW.salary < [1] THEN SET NEW.salary = [1]; END IF; END;
The trigger checks if the new salary is less than 3000 and sets it to 3000 if so.
Fill both blanks to create a trigger that updates the last_modified column to current timestamp on update.
CREATE TRIGGER update_timestamp BEFORE UPDATE ON employees FOR EACH ROW BEGIN SET NEW.[1] = [2]; END;
The trigger sets the last_modified column to the current timestamp using NOW() before updating.
Fill all three blanks to create a trigger that prevents changing the employee's department to 'HR' if their salary is below 5000.
CREATE TRIGGER prevent_hr_update BEFORE UPDATE ON employees FOR EACH ROW BEGIN IF NEW.[1] = 'HR' AND OLD.[2] < [3] THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Salary too low for HR department'; END IF; END;
The trigger checks if the new department is 'HR' and the old salary is less than 5000, then raises an error.