0
0
MySQLquery~20 mins

DELETE triggers in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DELETE Trigger Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of DELETE trigger with BEFORE DELETE
Consider a table employees with columns id and salary. A BEFORE DELETE trigger reduces the salary by 10% before the row is deleted. What will be the salary value logged if the row with id=3 and salary=1000 is deleted?
MySQL
CREATE TRIGGER before_delete_employee
BEFORE DELETE ON employees
FOR EACH ROW
BEGIN
  SET @old_salary = OLD.salary;
  SET NEW.salary = OLD.salary * 0.9;
  INSERT INTO salary_log(employee_id, old_salary, new_salary) VALUES (OLD.id, @old_salary, NEW.salary);
END;

DELETE FROM employees WHERE id = 3;
ANULL
B900
CError: Cannot assign NEW in BEFORE DELETE trigger
D1000
Attempts:
2 left
💡 Hint
In MySQL, the NEW keyword cannot be used in DELETE triggers.
query_result
intermediate
2:00remaining
Effect of AFTER DELETE trigger on audit table
Given a table orders and an audit table orders_audit, an AFTER DELETE trigger inserts the deleted row's id and deletion time into orders_audit. What will be the content of orders_audit after deleting the order with id=5?
MySQL
CREATE TRIGGER after_delete_order
AFTER DELETE ON orders
FOR EACH ROW
BEGIN
  INSERT INTO orders_audit(order_id, deleted_at) VALUES (OLD.id, NOW());
END;

DELETE FROM orders WHERE id = 5;
AError: OLD keyword not allowed in AFTER DELETE
BNo rows inserted
CA new row with order_id=NULL and current timestamp
DA new row with order_id=5 and current timestamp
Attempts:
2 left
💡 Hint
AFTER DELETE triggers can access OLD values to log deleted data.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in DELETE trigger
Which option contains a syntax error in the DELETE trigger definition?
MySQL
CREATE TRIGGER trg_delete_customer
BEFORE DELETE ON customers
FOR EACH ROW
BEGIN
  INSERT INTO deleted_customers(id, deleted_on) VALUES (OLD.id, NOW());
END;
AMissing delimiter change before trigger creation
BFOR EACH ROW should be FOR EACH STATEMENT
COLD keyword cannot be used in DELETE triggers
DTrigger name cannot contain underscores
Attempts:
2 left
💡 Hint
MySQL requires changing the delimiter when defining triggers with multiple statements.
optimization
advanced
2:00remaining
Optimizing DELETE trigger to avoid performance issues
A DELETE trigger on a large products table logs each deleted product to products_log. Which option best improves performance?
AInsert deleted rows in batches outside the trigger
BDisable the trigger during bulk deletes
CAdd indexes on products_log for faster inserts
DUse AFTER DELETE trigger instead of BEFORE DELETE
Attempts:
2 left
💡 Hint
Triggers run per row and can slow down bulk operations.
🧠 Conceptual
expert
2:00remaining
Understanding cascading DELETE triggers and recursion
In a MySQL database, table A has a DELETE trigger that deletes related rows in table B. Table B has a DELETE trigger that deletes related rows in table C. What happens if a DELETE on A causes cascading deletes in B and C?
ATriggers cause infinite recursion and crash the server
BAll triggers execute in order without recursion issues
COnly the first trigger executes; others are ignored
DMySQL prevents cascading triggers automatically
Attempts:
2 left
💡 Hint
MySQL supports cascading triggers but limits recursion depth.