Challenge - 5 Problems
Trigger Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Purpose of Triggers in Databases
Why are triggers used to automate responses in a database?
Attempts:
2 left
💡 Hint
Think about what happens automatically when data changes.
✗ Incorrect
Triggers are special procedures that run automatically in response to certain events like insert, update, or delete. This automation helps maintain data integrity and enforce rules without manual intervention.
❓ query_result
intermediate2:00remaining
Output of a Trigger on Insert
Given a trigger that logs every new row inserted into a table, what will be the result after inserting a row?
MySQL
CREATE TRIGGER log_insert AFTER INSERT ON employees FOR EACH ROW INSERT INTO audit_log(action, emp_id) VALUES ('insert', NEW.id); INSERT INTO employees (id, name) VALUES (1, 'Alice'); SELECT * FROM audit_log WHERE emp_id = 1;
Attempts:
2 left
💡 Hint
Triggers run automatically after the insert event.
✗ Incorrect
The trigger inserts a log entry into audit_log for each new employee inserted. After inserting employee with id 1, audit_log will have one matching row.
📝 Syntax
advanced2:00remaining
Identify the Syntax Error in Trigger Creation
Which option contains a syntax error when creating a trigger to update a timestamp column on row update?
MySQL
CREATE TRIGGER update_timestamp BEFORE UPDATE ON orders FOR EACH ROW SET NEW.updated_at = NOW();
Attempts:
2 left
💡 Hint
Check if the trigger body syntax is correct for MySQL.
✗ Incorrect
In MySQL, inside a BEGIN...END compound statement in a trigger, assignments to NEW columns must use the SET keyword (SET NEW.updated_at = NOW();). Option A incorrectly uses direct assignment (NEW.updated_at = NOW()), which is a syntax error. Options B and C are valid syntax.
❓ optimization
advanced2:00remaining
Optimizing Trigger Performance
Which practice helps optimize triggers to avoid slowing down database operations?
Attempts:
2 left
💡 Hint
Think about what makes database operations slower.
✗ Incorrect
Complex or heavy logic inside triggers can slow down data changes. Keeping triggers simple ensures they run quickly and do not block operations.
🔧 Debug
expert3:00remaining
Debugging a Trigger Causing Infinite Loop
A trigger on table sales updates a column in the same table, causing an infinite loop. Which option correctly fixes this issue?
Attempts:
2 left
💡 Hint
Prevent the trigger from updating when no change is needed.
✗ Incorrect
Triggers that update the same table can cause recursive calls. Adding a condition to update only when needed stops infinite loops.