Challenge - 5 Problems
Trigger Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
When to avoid using triggers in MySQL?
Which of the following is a recommended reason to avoid using triggers in MySQL?
Attempts:
2 left
💡 Hint
Think about how triggers can affect code readability and debugging.
✗ Incorrect
Triggers can cause hidden side effects that make debugging and maintenance harder. Keeping business logic explicit in application code is often better.
❓ query_result
intermediate2:00remaining
Output of a BEFORE INSERT trigger modifying data
Given this table and trigger, what will be the value of the 'name' column after inserting ('john')?
Table: users(id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50))
Trigger:
CREATE TRIGGER before_insert_users
BEFORE INSERT ON users
FOR EACH ROW
SET NEW.name = UPPER(NEW.name);
MySQL
INSERT INTO users (name) VALUES ('john'); SELECT name FROM users WHERE id = LAST_INSERT_ID();
Attempts:
2 left
💡 Hint
BEFORE INSERT triggers can modify the data before it is saved.
✗ Incorrect
The trigger converts the 'name' to uppercase before inserting, so the stored value is 'JOHN'.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this trigger definition
Which option contains a syntax error in the MySQL trigger definition?
MySQL
CREATE TRIGGER trg_after_update AFTER UPDATE ON employees FOR EACH ROW BEGIN IF NEW.salary < OLD.salary THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Salary cannot decrease'; END IF; END;
Attempts:
2 left
💡 Hint
MySQL requires changing the delimiter when defining triggers with BEGIN...END blocks.
✗ Incorrect
MySQL needs the delimiter changed before creating triggers with multiple statements to avoid premature end of the statement.
❓ optimization
advanced2:00remaining
Best practice to avoid performance issues with triggers
Which practice helps avoid performance problems caused by triggers in MySQL?
Attempts:
2 left
💡 Hint
Think about how triggers run synchronously during data changes.
✗ Incorrect
Complex or heavy queries inside triggers slow down data modification operations. Keeping triggers simple improves performance.
🔧 Debug
expert3:00remaining
Why does this trigger cause an infinite loop?
Consider this trigger:
CREATE TRIGGER trg_after_update
AFTER UPDATE ON orders
FOR EACH ROW
BEGIN
UPDATE orders SET status = 'processed' WHERE id = NEW.id;
END;
Why does this trigger cause an infinite loop?
Attempts:
2 left
💡 Hint
Think about what happens when a trigger updates the same table it listens to.
✗ Incorrect
The UPDATE inside the trigger causes the trigger to fire again, creating an infinite loop of updates.