Complete the code to create a trigger that runs before an insert.
CREATE TRIGGER before_insert_trigger BEFORE INSERT ON employees FOR EACH ROW BEGIN [1]; END;The trigger sets the created_at field to the current time before inserting a new row.
Complete the code to create a trigger that logs deletions.
CREATE TRIGGER log_delete AFTER DELETE ON orders FOR EACH ROW BEGIN INSERT INTO audit_log (order_id, action) VALUES ([1], 'deleted'); END;
In a DELETE trigger, OLD holds the row being deleted, so we use OLD.order_id to log it.
Fix the error in the trigger code to update salary after insert.
CREATE TRIGGER update_salary AFTER INSERT ON employees FOR EACH ROW BEGIN UPDATE employees SET salary = salary * 1.1 WHERE id = [1]; END;
After inserting a new employee, use NEW.id to update that employee's salary.
Fill both blanks to create a trigger that prevents negative stock.
CREATE TRIGGER prevent_negative_stock BEFORE UPDATE ON products FOR EACH ROW BEGIN IF NEW.stock [1] 0 THEN SIGNAL SQLSTATE [2] SET MESSAGE_TEXT = 'Stock cannot be negative'; END IF; END;
The trigger checks if the new stock is less than zero and raises an error with SQLSTATE '45000'.
Fill all three blanks to create a trigger that logs salary changes.
CREATE TRIGGER log_salary_change AFTER UPDATE ON employees FOR EACH ROW BEGIN IF NEW.salary [1] OLD.salary THEN INSERT INTO salary_log (employee_id, old_salary, new_salary) VALUES ([2], [3], NEW.salary); END IF; END;
The trigger checks if the new salary is greater than the old salary and logs the change with the employee's ID and old salary.