📖 Scenario: You manage a small library database. When a book record is deleted, you want to keep a log of the deleted book's title and deletion time for future reference.
🎯 Goal: Build a DELETE trigger that automatically saves the title of the deleted book and the time it was deleted into a separate log table.
📋 What You'll Learn
Create a table called books with columns id (integer primary key) and title (text).
Create a table called deleted_books_log with columns book_title (text) and deleted_at (timestamp).
Create a DELETE trigger on the books table.
The trigger should insert the deleted book's title and current timestamp into deleted_books_log.
💡 Why This Matters
🌍 Real World
Logging deleted records helps keep track of important data changes and supports auditing in real-world databases.
💼 Career
Database administrators and developers often use triggers to automate tasks like logging, enforcing rules, and maintaining data integrity.
Progress0 / 4 steps
1
Create the books table
Create a table called books with columns id as an integer primary key and title as text.
SQL
Hint
Use CREATE TABLE books (id INTEGER PRIMARY KEY, title TEXT);
2
Create the deleted_books_log table
Create a table called deleted_books_log with columns book_title as text and deleted_at as timestamp.
SQL
Hint
Use CREATE TABLE deleted_books_log (book_title TEXT, deleted_at TIMESTAMP);
3
Create the DELETE trigger function
Create a trigger function called log_deleted_book that inserts the deleted book's title and current timestamp into deleted_books_log. Use OLD.title to access the deleted row's title.
SQL
Hint
Use a trigger function with OLD.title and CURRENT_TIMESTAMP to insert into the log table.
4
Create the DELETE trigger on books
Create a DELETE trigger called after_delete_book on the books table that calls the log_deleted_book function after a row is deleted.
SQL
Hint
Use CREATE TRIGGER after_delete_book AFTER DELETE ON books FOR EACH ROW EXECUTE FUNCTION log_deleted_book();
Practice
(1/5)
1. What is the main purpose of a DELETE trigger in a database?
easy
A. To insert new rows into a table
B. To update rows before they are deleted
C. To automatically run code when rows are deleted from a table
D. To prevent any data from being deleted
Solution
Step 1: Understand what triggers do
Triggers run automatically in response to certain database events like insert, update, or delete.
Step 2: Focus on DELETE triggers
A DELETE trigger specifically runs when rows are deleted, allowing automatic actions during that event.
Final Answer:
To automatically run code when rows are deleted from a table -> Option C
Quick Check:
DELETE trigger = runs on row deletion [OK]
Hint: DELETE triggers run code automatically on row deletions [OK]
Common Mistakes:
Confusing DELETE triggers with INSERT triggers
Thinking DELETE triggers prevent deletion by default
Assuming DELETE triggers update rows instead
2. Which of the following is the correct syntax to create a trigger that runs before a row is deleted from a table named Employees?
easy
A. CREATE TRIGGER before_delete_employee DELETE ON Employees FOR EACH ROW BEGIN --code END;
B. CREATE TRIGGER before_delete_employee AFTER DELETE ON Employees FOR EACH ROW BEGIN --code END;
C. CREATE TRIGGER before_delete_employee ON Employees BEFORE DELETE FOR EACH ROW BEGIN --code END;
D. CREATE TRIGGER before_delete_employee BEFORE DELETE ON Employees FOR EACH ROW BEGIN --code END;
Solution
Step 1: Identify correct trigger timing syntax
The syntax for a BEFORE DELETE trigger includes 'BEFORE DELETE ON TableName'.
Step 2: Check full syntax correctness
CREATE TRIGGER before_delete_employee BEFORE DELETE ON Employees FOR EACH ROW BEGIN --code END; correctly uses 'CREATE TRIGGER name BEFORE DELETE ON Employees FOR EACH ROW BEGIN ... END;'. Others misuse order or keywords.
Final Answer:
CREATE TRIGGER before_delete_employee BEFORE DELETE ON Employees FOR EACH ROW BEGIN --code END; -> Option D
Quick Check:
BEFORE DELETE ON Table = correct syntax [OK]
Hint: Use BEFORE DELETE ON table for pre-delete triggers [OK]
Common Mistakes:
Placing BEFORE DELETE after ON table
Omitting FOR EACH ROW clause
Using DELETE ON instead of BEFORE DELETE ON
3. Given the following trigger and table data, what will be the output after deleting the row with id = 2 from Orders?
CREATE TRIGGER log_delete AFTER DELETE ON Orders
FOR EACH ROW
BEGIN
INSERT INTO DeletedOrdersLog(order_id, deleted_at) VALUES (OLD.id, CURRENT_TIMESTAMP);
END;
-- Initial Orders table:
-- id | product
-- 1 | Book
-- 2 | Pen
-- 3 | Notebook
-- DeletedOrdersLog is initially empty.
medium
A. A new row with order_id = 2 is inserted into DeletedOrdersLog
B. The row with id = 2 is restored in Orders
C. No changes happen to DeletedOrdersLog
D. An error occurs because OLD.id is invalid
Solution
Step 1: Understand AFTER DELETE trigger behavior
The trigger runs after a row is deleted from Orders, so OLD.id refers to the deleted row's id.
Step 2: Analyze trigger action
The trigger inserts a new row into DeletedOrdersLog with order_id = OLD.id (2) and current timestamp.
Final Answer:
A new row with order_id = 2 is inserted into DeletedOrdersLog -> Option A
Quick Check:
AFTER DELETE inserts log row with OLD.id [OK]
Hint: AFTER DELETE triggers can log deleted row info using OLD [OK]
Common Mistakes:
Thinking OLD.id is not accessible in DELETE triggers
Assuming trigger restores deleted rows
Believing no action happens after delete
4. Consider this trigger code snippet intended to prevent deletion of rows from Users table if the user is an admin:
CREATE TRIGGER prevent_admin_delete BEFORE DELETE ON Users
FOR EACH ROW
BEGIN
IF OLD.role = 'admin' THEN
SIGNAL SQLSTATE '45000' MESSAGE_TEXT = 'Cannot delete admin user';
END IF;
END;
What is the main issue if this trigger does not prevent admin deletions?
medium
A. The SIGNAL statement syntax is incorrect
B. The condition should check NEW.role instead of OLD.role
C. The trigger does not specify FOR EACH ROW
D. The trigger should be AFTER DELETE, not BEFORE DELETE
Solution
Step 1: Check trigger timing and row context
BEFORE DELETE and FOR EACH ROW are correctly used to check OLD.role before deletion.
Step 2: Verify SIGNAL syntax
In standard SQL, SIGNAL syntax requires correct SQLSTATE and message format. If syntax is wrong, error won't raise and deletion proceeds.
Final Answer:
The SIGNAL statement syntax is incorrect -> Option A
Quick Check:
SIGNAL syntax error prevents trigger from blocking delete [OK]
Hint: Check SIGNAL syntax carefully to block deletes [OK]
Common Mistakes:
Using AFTER DELETE instead of BEFORE DELETE
Checking NEW instead of OLD in DELETE triggers
Omitting FOR EACH ROW clause
5. You want to create a DELETE trigger on a Products table that archives deleted rows into an ArchivedProducts table, but only if the product price is above 100. Which trigger definition correctly implements this logic?
hard
A. CREATE TRIGGER archive_expensive_product BEFORE DELETE ON Products FOR EACH ROW BEGIN IF NEW.price > 100 THEN INSERT INTO ArchivedProducts (id, price) VALUES (NEW.id, NEW.price); END IF; END;
B. CREATE TRIGGER archive_expensive_product AFTER DELETE ON Products FOR EACH ROW BEGIN IF OLD.price > 100 THEN INSERT INTO ArchivedProducts (id, price) VALUES (OLD.id, OLD.price); END IF; END;
C. CREATE TRIGGER archive_expensive_product AFTER DELETE ON Products FOR EACH ROW BEGIN IF NEW.price > 100 THEN INSERT INTO ArchivedProducts (id, price) VALUES (OLD.id, OLD.price); END IF; END;
D. CREATE TRIGGER archive_expensive_product BEFORE DELETE ON Products FOR EACH ROW BEGIN IF NEW.price > 100 THEN INSERT INTO ArchivedProducts (id, price) VALUES (OLD.id, OLD.price); END IF; END;
Solution
Step 1: Choose correct timing and row reference
AFTER DELETE triggers run after deletion; OLD refers to deleted row data. BEFORE DELETE can also work but OLD is preferred after deletion.
Step 2: Check condition and insert logic
Condition must check OLD.price > 100. Insert must use OLD column values to archive deleted row data.
Step 3: Validate options
CREATE TRIGGER archive_expensive_product AFTER DELETE ON Products FOR EACH ROW BEGIN IF OLD.price > 100 THEN INSERT INTO ArchivedProducts (id, price) VALUES (OLD.id, OLD.price); END IF; END; uses AFTER DELETE, OLD.price, and inserts OLD.id, OLD.price correctly. Others use NEW or wrong timing.
Final Answer:
CREATE TRIGGER archive_expensive_product AFTER DELETE ON Products FOR EACH ROW BEGIN IF OLD.price > 100 THEN INSERT INTO ArchivedProducts (id, price) VALUES (OLD.id, OLD.price); END IF; END; -> Option B
Quick Check:
AFTER DELETE + OLD.price condition + insert OLD columns = correct [OK]
Hint: Use AFTER DELETE and OLD to archive deleted rows conditionally [OK]
Common Mistakes:
Using NEW instead of OLD in DELETE triggers
Placing condition on NEW.price which is invalid
Using BEFORE DELETE but referencing OLD incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.