0
0
MySQLquery~20 mins

AFTER INSERT triggers in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
AFTER INSERT Trigger Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of AFTER INSERT trigger updating another table
Consider two tables: 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');
A[]
B[{"order_id": 1, "log_time": "<current_timestamp>"}]
C[{"order_id": null, "log_time": "<current_timestamp>"}]
DSyntax error in trigger
Attempts:
2 left
💡 Hint
AFTER INSERT triggers run after the row is inserted, so NEW.id is available.
📝 Syntax
intermediate
1: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;
AMissing DELIMITER change before trigger creation
BTrigger body must be a single statement without BEGIN...END
CNEW keyword cannot be used in AFTER INSERT triggers
DFOR EACH ROW should be FOR EACH STATEMENT
Attempts:
2 left
💡 Hint
MySQL requires changing the delimiter when defining triggers with multiple statements.
optimization
advanced
2:30remaining
Optimizing AFTER INSERT trigger to avoid performance issues
An AFTER INSERT trigger on a large 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?
AUse BEFORE INSERT trigger to update totals
BUpdate totals by summing all sales for the product inside the trigger
CRemove the trigger and update totals manually once a day
DIncrement totals by NEW.sale_amount instead of summing all sales
Attempts:
2 left
💡 Hint
Avoid recalculating sums over large tables inside triggers.
🔧 Debug
advanced
2:00remaining
Why does this AFTER INSERT trigger cause a deadlock?
Given two tables 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;
ANEW keyword is invalid in AFTER INSERT triggers
BAFTER INSERT triggers cannot update other tables
CTrigger updates accounts causing lock conflicts with concurrent inserts
DTrigger must use BEFORE INSERT to avoid deadlocks
Attempts:
2 left
💡 Hint
Concurrent transactions updating the same rows can cause deadlocks.
🧠 Conceptual
expert
1:30remaining
Understanding limitations of AFTER INSERT triggers
Which statement about AFTER INSERT triggers in MySQL is TRUE?
AAFTER INSERT triggers execute after the row is inserted and can affect other tables but not the inserted row
BAFTER INSERT triggers execute before the row is inserted into the table
CAFTER INSERT triggers cannot be used to insert rows into other tables
DAFTER INSERT triggers can modify the row being inserted by changing NEW values
Attempts:
2 left
💡 Hint
Think about when AFTER INSERT triggers run and what NEW values represent.