Challenge - 5 Problems
AFTER INSERT Trigger Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of AFTER INSERT trigger updating another table
Consider two tables:
Given the following trigger and insert statement, what will be the content of
orders and order_log. An AFTER INSERT trigger on orders inserts a log entry into order_log with the new order's ID and timestamp.Given the following trigger and insert statement, what will be the content of
order_log after the insert?MySQL
CREATE TABLE orders (id INT PRIMARY KEY AUTO_INCREMENT, product VARCHAR(50)); CREATE TABLE order_log (order_id INT, log_time DATETIME); DELIMITER $$ CREATE TRIGGER after_order_insert AFTER INSERT ON orders FOR EACH ROW BEGIN INSERT INTO order_log(order_id, log_time) VALUES (NEW.id, NOW()); END$$ DELIMITER ; INSERT INTO orders (product) VALUES ('Book');
Attempts:
2 left
💡 Hint
AFTER INSERT triggers run after the row is inserted, so NEW.id is available.
✗ Incorrect
The AFTER INSERT trigger runs after the new row is inserted into orders. The NEW.id contains the auto-incremented ID (1 in this case). The trigger inserts a row into order_log with this ID and the current timestamp.
📝 Syntax
intermediate1:30remaining
Identify the syntax error in AFTER INSERT trigger
Which option contains a syntax error in this AFTER INSERT trigger definition?
MySQL
CREATE TRIGGER trg_after_insert AFTER INSERT ON employees FOR EACH ROW BEGIN UPDATE departments SET count = count + 1 WHERE id = NEW.department_id; END;
Attempts:
2 left
💡 Hint
MySQL requires changing the delimiter when defining triggers with multiple statements.
✗ Incorrect
In MySQL, when defining triggers with BEGIN...END blocks, you must change the delimiter temporarily to avoid syntax errors. Without changing the delimiter, the CREATE TRIGGER statement will fail.
❓ optimization
advanced2:30remaining
Optimizing AFTER INSERT trigger to avoid performance issues
An AFTER INSERT trigger on a large
Which option optimizes the trigger to improve performance?
sales table updates a totals table by summing all sales for the inserted product. This causes slow inserts.Which option optimizes the trigger to improve performance?
Attempts:
2 left
💡 Hint
Avoid recalculating sums over large tables inside triggers.
✗ Incorrect
Incrementing totals by the inserted sale amount avoids scanning the entire sales table, improving performance significantly.
🔧 Debug
advanced2:00remaining
Why does this AFTER INSERT trigger cause a deadlock?
Given two tables
What is the most likely cause?
accounts and transactions, an AFTER INSERT trigger on transactions updates the balance in accounts. Sometimes, inserting into transactions causes a deadlock.What is the most likely cause?
MySQL
CREATE TRIGGER after_transaction_insert AFTER INSERT ON transactions FOR EACH ROW BEGIN UPDATE accounts SET balance = balance + NEW.amount WHERE id = NEW.account_id; END;
Attempts:
2 left
💡 Hint
Concurrent transactions updating the same rows can cause deadlocks.
✗ Incorrect
The trigger updates the accounts table row for each inserted transaction. If multiple transactions insert concurrently for the same account, they try to update the same row, causing deadlocks.
🧠 Conceptual
expert1:30remaining
Understanding limitations of AFTER INSERT triggers
Which statement about AFTER INSERT triggers in MySQL is TRUE?
Attempts:
2 left
💡 Hint
Think about when AFTER INSERT triggers run and what NEW values represent.
✗ Incorrect
AFTER INSERT triggers run after the row is inserted, so they cannot change the inserted row's NEW values. They can perform actions on other tables.