A trigger is a special kind of stored procedure that automatically runs when certain events happen in the database, like inserting, updating, or deleting data.
Click to reveal answer
beginner
Why do we need triggers in databases?
Triggers help automate tasks, keep data consistent, enforce rules, and react to changes without needing manual intervention.
Click to reveal answer
beginner
Give an example of a situation where a trigger is useful.
For example, when a new order is added, a trigger can automatically update the stock quantity so the data stays accurate.
Click to reveal answer
intermediate
How do triggers help maintain data integrity?
Triggers can check or change data automatically when it is added or changed, making sure the data follows the rules set by the database.
Click to reveal answer
intermediate
Can triggers replace application code for some tasks? Why or why not?
Yes, triggers can handle some tasks inside the database itself, reducing the need for extra code in applications and ensuring rules are always followed.
Click to reveal answer
What event can cause a trigger to run?
AInserting data into a table
BOpening a database connection
CRunning a SELECT query
DClosing the database
✗ Incorrect
Triggers run automatically when data changes, like inserting, updating, or deleting rows.
Which of these is a main reason to use triggers?
ATo automate repetitive tasks in the database
BTo speed up SELECT queries
CTo create user interfaces
DTo store large files
✗ Incorrect
Triggers automate tasks like updating related data or enforcing rules when data changes.
Triggers help keep data consistent by:
AEncrypting data on disk
BBacking up the database every hour
CAutomatically checking or updating data when changes happen
DAllowing multiple users to edit the same data simultaneously
✗ Incorrect
Triggers run code to check or fix data automatically when it is inserted, updated, or deleted.
When is a trigger NOT useful?
AFor logging changes to data
BFor automatically updating related tables
CFor enforcing business rules inside the database
DFor creating reports from data
✗ Incorrect
Triggers do not create reports; they react to data changes.
Triggers run:
AManually by the user
BAutomatically when specific data events occur
COnly during database backups
DOnly when the database starts
✗ Incorrect
Triggers run automatically when data is inserted, updated, or deleted.
Explain in your own words why triggers are important in databases.
Think about how triggers help keep data correct without extra effort.
You got /4 concepts.
Describe a real-life example where a trigger could help manage data automatically.
Consider situations like updating stock after a sale or logging changes.
You got /4 concepts.
Practice
(1/5)
1. Why are triggers needed in a database? Triggers are special procedures that run automatically. What is their main purpose?
easy
A. To write complex queries for reports
B. To manually update data in tables
C. To create new tables in the database
D. To automatically perform actions when data changes happen
Solution
Step 1: Understand what triggers do
Triggers run automatically when data changes happen in a table, like insert, update, or delete.
Step 2: Identify the main purpose
The main purpose is to automate actions without manual intervention, such as enforcing rules or logging changes.
Final Answer:
To automatically perform actions when data changes happen -> Option D
Quick Check:
Triggers run automatically = D [OK]
Hint: Triggers act automatically on data changes [OK]
Common Mistakes:
Thinking triggers are for manual updates
Confusing triggers with creating tables
Assuming triggers write reports
2. Which of the following is the correct syntax to create a trigger that runs after an INSERT on a table named Employees?
easy
A. CREATE TRIGGER trg AFTER INSERT ON Employees BEGIN ... END;
B. CREATE TRIGGER trg BEFORE UPDATE ON Employees BEGIN ... END;
C. CREATE TRIGGER trg ON Employees AFTER DELETE BEGIN ... END;
D. CREATE TRIGGER trg INSERT AFTER Employees BEGIN ... END;
Solution
Step 1: Identify correct trigger timing and event
The question asks for a trigger after INSERT on Employees, so syntax must say AFTER INSERT ON Employees.
Step 2: Check syntax structure
CREATE TRIGGER trg AFTER INSERT ON Employees BEGIN ... END; matches the correct order: CREATE TRIGGER name AFTER INSERT ON table BEGIN ... END;
Final Answer:
CREATE TRIGGER trg AFTER INSERT ON Employees BEGIN ... END; -> Option A
Quick Check:
Correct trigger syntax = C [OK]
Hint: Syntax: CREATE TRIGGER name AFTER|BEFORE event ON table [OK]
Common Mistakes:
Mixing BEFORE and AFTER keywords
Placing table name in wrong position
Using ON before event instead of after
3. Consider this trigger on table Orders:
CREATE TRIGGER trg_log AFTER UPDATE ON Orders BEGIN INSERT INTO AuditLog VALUES('Order updated'); END;
What happens when an order is updated?
medium
A. Nothing happens automatically
B. A new row with 'Order updated' is added to AuditLog
C. The Orders table is deleted
D. The update on Orders is canceled
Solution
Step 1: Understand trigger action on UPDATE
The trigger runs AFTER UPDATE on Orders and inserts a row into AuditLog with text 'Order updated'.
Step 2: Identify the effect of the trigger
Each time an order is updated, a new row is added to AuditLog recording the event.
Final Answer:
A new row with 'Order updated' is added to AuditLog -> Option B
Quick Check:
Trigger inserts log row = B [OK]
Hint: Trigger inserts log row after update [OK]
Common Mistakes:
Thinking trigger cancels update
Assuming no action happens automatically
Confusing trigger with delete operation
4. You wrote this trigger:
CREATE TRIGGER trg_check BEFORE INSERT ON Users BEGIN IF NEW.age < 18 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Age must be 18 or older'; END IF; END;
But it does not prevent inserting users younger than 18. What is the likely error?
medium
A. The trigger does not have a proper delimiter for multiple statements
B. The SIGNAL statement syntax is incorrect
C. The trigger should be AFTER INSERT, not BEFORE
D. Triggers cannot check conditions on NEW data
Solution
Step 1: Analyze trigger structure with multiple statements
The trigger has IF and SIGNAL inside BEGIN...END, which requires proper statement delimiters (like semicolons) to separate commands.
Step 2: Identify missing delimiter issue
Without delimiters, the database cannot parse the trigger body correctly, so the condition check fails.
Final Answer:
The trigger does not have a proper delimiter for multiple statements -> Option A
Quick Check:
Missing delimiters cause trigger failure = A [OK]
Hint: Use delimiters for multiple statements in triggers [OK]
Common Mistakes:
Changing BEFORE to AFTER incorrectly
Assuming SIGNAL syntax is wrong without checking
Believing triggers can't access NEW data
5. A company wants to ensure that whenever a product's price is updated, the old price is saved in a PriceHistory table automatically. Which trigger design best achieves this?
hard
A. A BEFORE DELETE trigger on Products that inserts old price into PriceHistory
B. An AFTER INSERT trigger on Products that inserts new price into PriceHistory
C. An AFTER UPDATE trigger on Products that inserts old price into PriceHistory
D. A BEFORE UPDATE trigger on Products that inserts old price into PriceHistory
Solution
Step 1: Understand when old price is available
The old price is available after the update operation starts but before the new data replaces it, so a trigger on UPDATE is needed.
Step 2: Choose trigger timing to save old price
Using AFTER UPDATE ensures the update succeeded, and the trigger can insert the old price (accessible via OLD) into PriceHistory.
Final Answer:
An AFTER UPDATE trigger on Products that inserts old price into PriceHistory -> Option C
Quick Check:
Save old price after update = A [OK]
Hint: Use AFTER UPDATE trigger to save old data changes [OK]
Common Mistakes:
Using BEFORE DELETE instead of UPDATE
Using AFTER INSERT which doesn't track price changes
Choosing BEFORE UPDATE which may block or cause errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.