Consider a table Employees with columns id and salary. A DELETE trigger is defined to insert the deleted row's id and salary into a log table DeletedEmployees. What will be the content of DeletedEmployees after deleting the employee with id = 3?
CREATE TABLE Employees (id INT PRIMARY KEY, salary INT); CREATE TABLE DeletedEmployees (id INT, salary INT); CREATE TRIGGER trg_after_delete AFTER DELETE ON Employees FOR EACH ROW INSERT INTO DeletedEmployees (id, salary) VALUES (OLD.id, OLD.salary); INSERT INTO Employees VALUES (1, 5000), (2, 6000), (3, 7000); DELETE FROM Employees WHERE id = 3;
Think about what data the trigger captures when a row is deleted.
The DELETE trigger runs after a row is deleted. It uses OLD to access the deleted row's data. So the deleted employee's id and salary are inserted into DeletedEmployees. Only the deleted row (id=3) is logged.
In a DELETE trigger, which of the following statements about OLD and NEW references is correct?
Think about what data exists after a row is deleted.
In DELETE triggers, OLD holds the data of the row being deleted. NEW is only available in INSERT or UPDATE triggers.
Which option contains a syntax error in the DELETE trigger definition?
CREATE TRIGGER trg_del AFTER DELETE ON Orders FOR EACH ROW BEGIN INSERT INTO DeletedOrders (order_id) VALUES (OLD.order_id); END;
Check the use of OLD and NEW in DELETE triggers.
Option D uses NEW.order_id in a DELETE trigger, which is invalid because NEW is not available in DELETE triggers.
You have a DELETE trigger that logs deleted rows one by one. Which approach improves performance when deleting many rows at once?
Think about how triggers handle multiple rows.
Statement-level triggers run once per DELETE statement and can handle all deleted rows together, improving performance over row-level triggers that run once per row.
Given the trigger below, deleting a row from Products causes an infinite loop. Why?
CREATE TRIGGER trg_del_product AFTER DELETE ON Products FOR EACH ROW BEGIN DELETE FROM Products WHERE id = OLD.id; END;
Consider what happens when the trigger deletes rows inside itself.
The trigger deletes the same row that was just deleted, causing the trigger to fire again repeatedly, creating an infinite loop.