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 can triggers affect database performance?
Triggers run automatically during data changes, so if they do a lot of work or run many times, they can slow down the database operations.
Click to reveal answer
intermediate
Name one way to reduce the performance impact of triggers.
Keep the trigger code simple and fast, doing only what is necessary to avoid slowing down the main database operation.
Click to reveal answer
intermediate
What is a potential problem of having multiple triggers on the same table?
Multiple triggers can cause extra processing time because each trigger runs in sequence, increasing the total time for the database operation.
Click to reveal answer
intermediate
How can you monitor the performance impact of triggers?
You can use database tools or logs to measure how long triggers take to run and check if they slow down data operations.
Click to reveal answer
What happens if a trigger contains complex queries that take a long time to run?
AThe trigger will be skipped to keep performance fast.
BThe trigger will run after the operation without affecting speed.
CThe database operation will be slower because the trigger runs during the operation.
DThe database will automatically optimize the trigger to run faster.
✗ Incorrect
Triggers run during the database operation, so complex or slow queries inside them will slow down the operation.
Which of these is a good practice to improve trigger performance?
AKeep trigger logic simple and fast.
BAvoid using triggers and do all work manually outside the database.
CInclude heavy calculations inside the trigger.
DCreate many triggers for the same event.
✗ Incorrect
Keeping trigger logic simple and fast helps reduce the performance impact on database operations.
What is a risk of having multiple triggers on the same table for the same event?
ATriggers will cancel each other out.
BTriggers will run in sequence, increasing operation time.
COnly the first trigger will run.
DTriggers will run in parallel without delay.
✗ Incorrect
Multiple triggers run one after another, which can increase the total time for the database operation.
How can you check if a trigger is causing slow database operations?
ABy using database monitoring tools to measure trigger execution time.
BBy disabling all triggers permanently.
CBy guessing based on the number of triggers.
DBy rewriting the entire database.
✗ Incorrect
Database monitoring tools can help measure how long triggers take and identify performance issues.
When does a trigger execute in relation to the database operation?
AOnly after the database operation finishes.
BOnly before the database operation starts.
CRandomly at any time.
DBefore or after the data change event, depending on trigger type.
✗ Incorrect
Triggers can be set to run before or after data changes, affecting when they impact performance.
Explain how triggers can impact database performance and what you can do to minimize this impact.
Think about when triggers run and how their complexity affects speed.
You got /5 concepts.
Describe best practices for writing efficient triggers to avoid slowing down your database.
Focus on simplicity and monitoring.
You got /5 concepts.
Practice
(1/5)
1. What is the main reason to keep SQL triggers simple and fast?
easy
A. To avoid slowing down database operations
B. To make triggers run only once a day
C. To allow triggers to modify unrelated tables
D. To enable triggers to run without any conditions
Solution
Step 1: Understand trigger execution timing
Triggers run automatically during data changes, so their speed affects overall database performance.
Step 2: Consider impact of complex triggers
Complex or slow triggers can delay data operations, causing performance issues.
Final Answer:
To avoid slowing down database operations -> Option A
Quick Check:
Trigger speed affects database speed = A [OK]
Hint: Fast triggers keep database operations smooth [OK]
Common Mistakes:
Thinking triggers run only once a day
Assuming triggers can modify any table without limits
Believing triggers run without conditions
2. Which of the following is the correct syntax to create a simple AFTER INSERT trigger in SQL?
easy
A. CREATE TRIGGER trg AFTER INSERT employees BEGIN UPDATE stats SET count = count + 1; END;
B. CREATE TRIGGER trg AFTER INSERT ON employees FOR EACH ROW BEGIN UPDATE stats SET count = count + 1; END;
C. CREATE TRIGGER trg BEFORE UPDATE ON employees FOR EACH ROW UPDATE stats SET count = count + 1;
D. CREATE TRIGGER trg ON employees AFTER INSERT BEGIN UPDATE stats SET count = count + 1; END;
Solution
Step 1: Check trigger timing and event
The trigger is AFTER INSERT on the employees table, so syntax must include 'AFTER INSERT ON employees'.
Step 2: Verify trigger body structure
The trigger body must be enclosed in BEGIN ... END; and include FOR EACH ROW for row-level triggers.
Final Answer:
CREATE TRIGGER trg AFTER INSERT ON employees FOR EACH ROW BEGIN UPDATE stats SET count = count + 1; END; -> Option B
Quick Check:
Correct syntax includes timing, event, table, and body [OK]
Hint: Look for 'AFTER INSERT ON table FOR EACH ROW' syntax [OK]
Common Mistakes:
Omitting 'ON' keyword before table name
Missing 'FOR EACH ROW' for row-level triggers
Incorrect order of keywords
3. Given this trigger code:
CREATE TRIGGER trg_update AFTER UPDATE ON orders FOR EACH ROW BEGIN UPDATE inventory SET stock = stock - NEW.quantity WHERE product_id = NEW.product_id; END;
What happens if an update changes 10 units of product_id 5 in orders?
medium
A. The inventory stock for product_id 5 increases by 10
B. No change happens to inventory stock
C. The inventory stock for product_id 5 decreases by 10
D. The trigger causes a syntax error
Solution
Step 1: Understand trigger action on update
The trigger runs AFTER UPDATE on orders and adjusts inventory stock by subtracting NEW.quantity.
Step 2: Apply the update values
For product_id 5, stock is reduced by 10 units as NEW.quantity is 10.
Final Answer:
The inventory stock for product_id 5 decreases by 10 -> Option C
Quick Check:
Trigger subtracts NEW.quantity from stock = B [OK]
Hint: NEW.quantity subtracts from stock after update [OK]
Common Mistakes:
Confusing NEW with OLD values
Assuming stock increases instead of decreases
Thinking trigger causes syntax error
4. You wrote this trigger but it causes a runtime error:
CREATE TRIGGER trg_log AFTER INSERT ON sales FOR EACH ROW BEGIN INSERT INTO sales_log VALUES (NEW.id, NEW.amount); UPDATE sales SET amount = amount * 1.1 WHERE id = NEW.id; END;
What is the likely cause of the error?
medium
A. Trigger tries to update the same table it is triggered on, causing recursion
B. Missing semicolon after INSERT statement
C. NEW keyword cannot be used in AFTER INSERT triggers
D. sales_log table does not exist
Solution
Step 1: Identify trigger actions
The trigger inserts into sales_log and then updates sales table, which is the same table that fired the trigger.
Step 2: Understand recursion risk
Updating sales inside an AFTER INSERT trigger on sales causes the trigger to fire again, leading to infinite recursion and runtime error.
Final Answer:
Trigger tries to update the same table it is triggered on, causing recursion -> Option A