0
0
MySQLquery~20 mins

Trigger best practices and limitations in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Trigger Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
When to avoid using triggers in MySQL?
Which of the following is a recommended reason to avoid using triggers in MySQL?
AWhen you want to keep business logic simple and maintainable by avoiding hidden side effects
BWhen you need to enforce data integrity at the database level
CWhen you want to automatically update audit columns like last_modified
DWhen you want to replicate data changes to another table automatically
Attempts:
2 left
💡 Hint
Think about how triggers can affect code readability and debugging.
query_result
intermediate
2: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();
A'john'
BSyntax error in trigger
CNULL
D'JOHN'
Attempts:
2 left
💡 Hint
BEFORE INSERT triggers can modify the data before it is saved.
📝 Syntax
advanced
2: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;
AMissing delimiter change before trigger creation
BFOR EACH ROW should be FOR EACH STATEMENT
CSIGNAL SQLSTATE is not allowed in triggers
DTrigger name cannot contain underscores
Attempts:
2 left
💡 Hint
MySQL requires changing the delimiter when defining triggers with BEGIN...END blocks.
optimization
advanced
2:00remaining
Best practice to avoid performance issues with triggers
Which practice helps avoid performance problems caused by triggers in MySQL?
AUse triggers to replace all application-level validations
BKeep trigger logic simple and avoid complex queries inside triggers
CCreate multiple triggers for the same event to split logic
DUse triggers to perform heavy data aggregation on large tables
Attempts:
2 left
💡 Hint
Think about how triggers run synchronously during data changes.
🔧 Debug
expert
3: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?
ABecause the trigger syntax is invalid and causes repeated errors
BBecause AFTER UPDATE triggers cannot modify the same table
CBecause the UPDATE inside the trigger fires the same trigger again repeatedly
DBecause the trigger does not have a WHERE clause
Attempts:
2 left
💡 Hint
Think about what happens when a trigger updates the same table it listens to.