Complete the code to create a trigger that activates after an INSERT on the employees table.
CREATE TRIGGER trg_after_insert
AFTER INSERT ON employees
FOR EACH ROW
BEGIN
INSERT INTO audit_log(action) VALUES([1]);
END;The trigger inserts the string 'INSERT' into the audit_log table to record the action.
Complete the code to reference the new employee's name in the trigger.
CREATE TRIGGER trg_after_insert AFTER INSERT ON employees FOR EACH ROW BEGIN INSERT INTO audit_log(description) VALUES(CONCAT('New employee: ', [1])); END;
NEW.name refers to the inserted row's name in an INSERT trigger.
Fix the error in the trigger by completing the missing keyword.
CREATE TRIGGER trg_before_insert
[1] INSERT ON employees
FOR EACH ROW
BEGIN
SET NEW.created_at = NOW();
END;The trigger must be defined as BEFORE INSERT to modify NEW values before insertion.
Fill both blanks to create a trigger that logs the new employee's ID and name after insert.
CREATE TRIGGER trg_log_insert AFTER INSERT ON employees FOR EACH ROW BEGIN INSERT INTO audit_log(emp_id, emp_name) VALUES([1], [2]); END;
NEW.id and NEW.name refer to the inserted row's ID and name.
Fill all three blanks to create a trigger that prevents inserting employees with salary less than 3000.
CREATE TRIGGER trg_check_salary BEFORE INSERT ON employees FOR EACH ROW BEGIN IF NEW.salary [1] [2] THEN SIGNAL SQLSTATE [3] SET MESSAGE_TEXT = 'Salary too low'; END IF; END;
The trigger checks if NEW.salary is less than 3000 and raises an error with SQLSTATE '45000'.