A database trigger is a special procedure that automatically runs when certain events happen in the database, like inserting, updating, or deleting data.
Click to reveal answer
beginner
Why should triggers be kept simple and fast?
Because triggers run automatically during data changes, slow or complex triggers can delay the main operation and affect database performance.
Click to reveal answer
intermediate
Name one limitation of MySQL triggers.
MySQL allows only one trigger per table for each combination of timing (BEFORE/AFTER) and event (INSERT/UPDATE/DELETE), which limits splitting complex logic across multiple triggers.
Click to reveal answer
intermediate
What is a common best practice when using triggers to avoid unexpected behavior?
Avoid having triggers that cause other triggers to fire in a loop, which can cause infinite recursion and crash the database.
Click to reveal answer
beginner
How can you test a trigger safely before applying it in production?
Test triggers in a development or staging environment with sample data to ensure they work correctly and do not cause errors or performance issues.
Click to reveal answer
Which event can a MySQL trigger NOT respond to?
AINSERT
BUPDATE
CDELETE
DSELECT
✗ Incorrect
Triggers in MySQL cannot be set to run on SELECT statements; they only respond to data changes like INSERT, UPDATE, or DELETE.
What happens if a trigger causes another trigger to fire repeatedly in MySQL?
AThe database optimizes and stops the loop automatically
BIt can cause infinite recursion and crash the database
CTriggers do not fire other triggers in MySQL
DThe second trigger runs only once
✗ Incorrect
If triggers call each other repeatedly without stopping, it causes infinite recursion which can crash the database.
Which is a good practice when writing triggers?
AKeep triggers simple and fast
BAvoid testing triggers
CUse triggers to replace all application logic
DMake triggers as complex as possible
✗ Incorrect
Keeping triggers simple and fast helps maintain good database performance and avoids delays.
Can MySQL triggers call stored procedures that modify data?
AYes, always
BNo, this is not allowed
COnly if the procedure is read-only
DOnly in MySQL 8.0 and above
✗ Incorrect
Yes, MySQL triggers can call stored procedures that modify data, but this must be done carefully to avoid recursion issues.
Where should you test triggers before using them in production?
ADirectly on the live database
BOnly on backups
COn a development or staging environment
DNo testing is needed
✗ Incorrect
Testing triggers in a development or staging environment helps catch errors and performance issues before affecting live data.
Explain the best practices to follow when creating triggers in MySQL.
Think about performance, safety, and testing.
You got /4 concepts.
Describe the main limitations of MySQL triggers.
Focus on what triggers cannot do and what risks they have.
You got /4 concepts.
Practice
(1/5)
1. Which of the following is a best practice when creating MySQL triggers?
easy
A. Keep trigger logic simple to avoid performance issues.
B. Use triggers to replace all application logic.
C. Allow triggers to call themselves recursively without limits.
D. Write triggers that perform complex calculations on large datasets.
Solution
Step 1: Understand trigger performance impact
Triggers run automatically on data changes, so complex logic can slow down database operations.
Step 2: Identify best practice for trigger design
Keeping trigger logic simple helps maintain good performance and avoids unexpected delays.
Final Answer:
Keep trigger logic simple to avoid performance issues. -> Option A
Quick Check:
Simple trigger logic = Better performance [OK]
Hint: Keep triggers simple to avoid slowdowns [OK]
Common Mistakes:
Trying to put all business logic inside triggers
Allowing infinite recursion in triggers
Using triggers for heavy data processing
2. Which syntax correctly defines a BEFORE INSERT trigger in MySQL?
easy
A. CREATE TRIGGER trg BEFORE INSERT ON table_name FOR EACH ROW BEGIN ... END;
B. CREATE TRIGGER trg AFTER INSERT ON table_name FOR EACH ROW BEGIN ... END;
C. CREATE TRIGGER trg BEFORE UPDATE ON table_name FOR EACH ROW BEGIN ... END;
D. CREATE TRIGGER trg BEFORE DELETE ON table_name FOR EACH ROW BEGIN ... END;
Solution
Step 1: Identify trigger timing and event
BEFORE INSERT triggers run before a new row is inserted.
Step 2: Match syntax to BEFORE INSERT event
CREATE TRIGGER trg BEFORE INSERT ON table_name FOR EACH ROW BEGIN ... END; uses BEFORE INSERT, correct timing and event for this trigger.
Final Answer:
CREATE TRIGGER trg BEFORE INSERT ON table_name FOR EACH ROW BEGIN ... END; -> Option A
Quick Check:
BEFORE INSERT syntax = CREATE TRIGGER trg BEFORE INSERT ON table_name FOR EACH ROW BEGIN ... END; [OK]
Hint: BEFORE triggers run before data changes [OK]
Common Mistakes:
Confusing BEFORE and AFTER keywords
Using wrong event like UPDATE or DELETE
Missing FOR EACH ROW clause
3. Given this trigger, what happens when a new row is inserted?
CREATE TRIGGER trg BEFORE INSERT ON employees FOR EACH ROW BEGIN SET NEW.salary = NEW.salary * 1.1; END;
medium
A. The trigger causes a syntax error.
B. The salary is increased by 10% before insertion.
C. The salary remains unchanged.
D. The insertion fails due to recursion.
Solution
Step 1: Analyze trigger action on BEFORE INSERT
The trigger modifies the NEW.salary value by multiplying it by 1.1 before the row is inserted.
Step 2: Understand effect on inserted data
The inserted row will have salary increased by 10% automatically.
Final Answer:
The salary is increased by 10% before insertion. -> Option B
Quick Check:
BEFORE INSERT modifies NEW.salary = increased salary [OK]
Hint: BEFORE triggers can modify NEW row data [OK]
Common Mistakes:
Thinking triggers run after insertion
Assuming salary stays the same
Confusing NEW and OLD keywords
4. What is wrong with this trigger definition?
CREATE TRIGGER trg AFTER UPDATE ON employees FOR EACH ROW BEGIN UPDATE employees SET salary = salary * 1.05 WHERE id = NEW.id; END;
medium
A. It will not update any rows because NEW.id is invalid.
B. Syntax error due to missing DELIMITER statement.
C. Triggers cannot use UPDATE statements inside their body.
D. It causes infinite recursion by updating the same table inside the trigger.
Solution
Step 1: Identify trigger action on AFTER UPDATE
The trigger updates the same table employees inside an AFTER UPDATE trigger.
Step 2: Understand recursion risk
Updating the same table inside its own UPDATE trigger causes the trigger to call itself repeatedly, causing infinite recursion.
Final Answer:
It causes infinite recursion by updating the same table inside the trigger. -> Option D
Quick Check:
Trigger updating same table = recursion risk [OK]
Hint: Avoid updating same table inside its trigger [OK]
Common Mistakes:
Ignoring recursion risk
Assuming syntax error without DELIMITER
Thinking triggers can't run UPDATE statements
5. You want to log every DELETE action on the orders table without slowing down the delete operation. Which trigger design is best?
hard
A. Use a BEFORE DELETE trigger that updates the orders table with a log message.
B. Use an AFTER DELETE trigger that updates the orders table itself.
C. Use an AFTER DELETE trigger that inserts a log record into a separate table.
D. Use a BEFORE DELETE trigger that performs complex calculations before deleting.
Solution
Step 1: Choose trigger timing for logging
AFTER DELETE triggers run after the row is deleted, so they don't block the delete operation.
Step 2: Select action that avoids slowing delete
Inserting a log record into a separate table is efficient and safe, avoiding recursion or delays.
Final Answer:
Use an AFTER DELETE trigger that inserts a log record into a separate table. -> Option C
Quick Check:
AFTER DELETE + separate log table = best practice [OK]
Hint: Log deletes AFTER action to avoid delays [OK]
Common Mistakes:
Updating same table inside trigger causing recursion
Using BEFORE DELETE for logging causing delays
Doing heavy calculations inside triggers
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.