A BEFORE trigger is a special procedure that runs automatically before an INSERT, UPDATE, or DELETE operation on a table. It lets you check or change data before it is saved.
Click to reveal answer
beginner
When does a BEFORE trigger execute?
A BEFORE trigger executes just before the data modification happens on the table. This means it runs before the new data is inserted, updated, or deleted.
Click to reveal answer
intermediate
Can a BEFORE trigger modify the data being inserted or updated?
Yes, a BEFORE trigger can change the data before it is saved to the table. For example, it can set default values or correct data formats.
Click to reveal answer
intermediate
Why use a BEFORE trigger instead of an AFTER trigger?
Use a BEFORE trigger to validate or modify data before it is saved. AFTER triggers run after the data change, so they can't stop or change the data before saving.
Click to reveal answer
intermediate
Example of a simple BEFORE INSERT trigger in SQL?
CREATE TRIGGER set_default_status BEFORE INSERT ON orders FOR EACH ROW BEGIN IF NEW.status IS NULL THEN SET NEW.status = 'pending'; END IF; END;
Click to reveal answer
When does a BEFORE trigger run in SQL?
ABefore the data change happens
BAfter the data change happens
COnly when data is deleted
DOnly when data is selected
✗ Incorrect
A BEFORE trigger runs before the data is inserted, updated, or deleted.
Which of these can a BEFORE trigger do?
ASend an email after saving
BModify data before saving
CCreate a new table
DDrop the database
✗ Incorrect
BEFORE triggers can modify data before it is saved to the table.
If you want to prevent bad data from being saved, which trigger should you use?
ABEFORE trigger
BAFTER trigger
CINSTEAD OF trigger
DNO trigger
✗ Incorrect
BEFORE triggers run before data is saved, so they can stop or fix bad data.
Which SQL statement is used to create a BEFORE trigger?
ACREATE VIEW ... BEFORE
BCREATE PROCEDURE ... BEFORE
CCREATE FUNCTION ... BEFORE
DCREATE TRIGGER ... BEFORE
✗ Incorrect
CREATE TRIGGER with BEFORE specifies a trigger that runs before data changes.
Can a BEFORE trigger run on UPDATE operations?
AOnly on DELETE
BNo
CYes
DOnly on INSERT
✗ Incorrect
BEFORE triggers can run on INSERT, UPDATE, or DELETE operations.
Explain what a BEFORE trigger is and when it runs.
Think about what happens before saving data.
You got /3 concepts.
Describe a practical use case for a BEFORE trigger in a database.
Imagine you want to fix or check data before it is stored.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a BEFORE trigger in SQL?
easy
A. To execute code after data is inserted or updated
B. To delete data automatically
C. To execute code before a data modification happens
D. To create a backup of the database
Solution
Step 1: Understand trigger timing
A BEFORE trigger runs before the data change happens, allowing checks or modifications.
Step 2: Compare options
To execute code before a data modification happens correctly states the trigger runs before data modification. Options A, C, and D describe other actions not related to BEFORE triggers.
Final Answer:
To execute code before a data modification happens -> Option C
Quick Check:
BEFORE trigger = runs before data change [OK]
Hint: BEFORE triggers run before data changes happen [OK]
Common Mistakes:
Confusing BEFORE with AFTER triggers
Thinking triggers delete data automatically
Assuming triggers create backups
2. Which of the following is the correct syntax to create a BEFORE INSERT trigger in SQL?
easy
A. CREATE TRIGGER trg BEFORE DELETE ON table_name FOR EACH ROW BEGIN ... END;
B. CREATE TRIGGER trg AFTER INSERT ON table_name BEGIN ... END;
C. CREATE TRIGGER trg BEFORE UPDATE ON table_name FOR EACH ROW BEGIN ... END;
D. CREATE TRIGGER trg BEFORE INSERT ON table_name FOR EACH ROW BEGIN ... END;
Solution
Step 1: Identify correct trigger timing and event
The question asks for a BEFORE INSERT trigger, so timing is BEFORE and event is INSERT.
Step 2: Match syntax
CREATE TRIGGER trg BEFORE INSERT ON table_name FOR EACH ROW BEGIN ... END; matches CREATE TRIGGER trg BEFORE INSERT ON table_name FOR EACH ROW BEGIN ... END;. Other options have wrong timing or event.
Final Answer:
CREATE TRIGGER trg BEFORE INSERT ON table_name FOR EACH ROW BEGIN ... END; -> Option D
Quick Check:
BEFORE INSERT syntax = CREATE TRIGGER trg BEFORE INSERT ON table_name FOR EACH ROW BEGIN ... END; [OK]
Hint: BEFORE triggers use 'BEFORE event ON table' syntax [OK]
Common Mistakes:
Using AFTER instead of BEFORE
Mixing INSERT with UPDATE or DELETE
Omitting FOR EACH ROW clause
3. Given this trigger code:
CREATE TRIGGER check_age BEFORE INSERT ON users FOR EACH ROW BEGIN IF NEW.age < 18 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Age must be 18 or older'; END IF; END;
What happens if you try to insert a user with age 16?
medium
A. The insert fails with an error message 'Age must be 18 or older'
B. The age is automatically set to 18
C. The insert is ignored silently
D. The insert succeeds and user is added
Solution
Step 1: Understand trigger logic
The trigger checks if NEW.age is less than 18 and raises an error if true.
Step 2: Apply to age 16
Since 16 < 18, the trigger raises an error with message 'Age must be 18 or older', stopping the insert.
Final Answer:
The insert fails with an error message 'Age must be 18 or older' -> Option A
Quick Check:
BEFORE trigger can stop invalid inserts [OK]
Hint: BEFORE triggers can block invalid data with SIGNAL [OK]
Common Mistakes:
Thinking insert succeeds despite error
Assuming silent ignore instead of error
Believing trigger changes data automatically
4. Consider this trigger code:
CREATE TRIGGER trg BEFORE UPDATE ON products FOR EACH ROW BEGIN SET NEW.price = NEW.price * 0.9; END;
When trying to create this trigger, you get an error. What is the likely cause?
medium
A. The syntax is missing a delimiter or semicolon inside the body
B. The trigger body must be enclosed in BEGIN and END
C. The trigger must specify FOR EACH ROW for row-level triggers
D. You cannot modify NEW values in a BEFORE UPDATE trigger
Solution
Step 1: Check trigger syntax
The trigger body uses multiple statements and must handle delimiters properly.
Step 2: Identify missing delimiter
Without changing the delimiter, the semicolon inside the body ends the statement early, causing syntax error.
Final Answer:
The syntax is missing a delimiter or semicolon inside the body -> Option A
Quick Check:
Use DELIMITER to define trigger body [OK]
Hint: Use DELIMITER to avoid semicolon conflicts in triggers [OK]
Common Mistakes:
Thinking NEW values can't be changed in BEFORE triggers
Ignoring delimiter changes for multi-statement bodies
Missing FOR EACH ROW clause
5. You want to ensure that before inserting a new order, the quantity is never negative. Which trigger code correctly enforces this using a BEFORE INSERT trigger?
hard
A. CREATE TRIGGER trg BEFORE INSERT ON orders FOR EACH ROW BEGIN IF NEW.quantity < 0 THEN SET NEW.quantity = 0; END IF; END;
B. CREATE TRIGGER trg BEFORE INSERT ON orders FOR EACH ROW BEGIN IF NEW.quantity < 0 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Negative quantity not allowed'; END IF; END;
C. CREATE TRIGGER trg AFTER INSERT ON orders FOR EACH ROW BEGIN IF NEW.quantity < 0 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Negative quantity not allowed'; END IF; END;
D. CREATE TRIGGER trg BEFORE UPDATE ON orders FOR EACH ROW BEGIN IF NEW.quantity < 0 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Negative quantity not allowed'; END IF; END;
Solution
Step 1: Identify correct timing and event
We want to check before inserting, so BEFORE INSERT is correct.
Step 2: Check enforcement method
To stop negative quantity, raising an error with SIGNAL is best. CREATE TRIGGER trg BEFORE INSERT ON orders FOR EACH ROW BEGIN IF NEW.quantity < 0 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Negative quantity not allowed'; END IF; END; uses BEFORE INSERT and SIGNAL correctly.
Step 3: Eliminate wrong options
CREATE TRIGGER trg BEFORE INSERT ON orders FOR EACH ROW BEGIN IF NEW.quantity < 0 THEN SET NEW.quantity = 0; END IF; END; silently changes quantity, not enforcing. CREATE TRIGGER trg AFTER INSERT ON orders FOR EACH ROW BEGIN IF NEW.quantity < 0 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Negative quantity not allowed'; END IF; END; uses AFTER INSERT, too late to stop insert. CREATE TRIGGER trg BEFORE UPDATE ON orders FOR EACH ROW BEGIN IF NEW.quantity < 0 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Negative quantity not allowed'; END IF; END; uses BEFORE UPDATE, not insert.
Final Answer:
CREATE TRIGGER trg BEFORE INSERT ON orders FOR EACH ROW BEGIN IF NEW.quantity < 0 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Negative quantity not allowed'; END IF; END; -> Option B
Quick Check:
Use BEFORE INSERT + SIGNAL to block invalid data [OK]
Hint: Use BEFORE INSERT with SIGNAL to block bad data [OK]
Common Mistakes:
Using AFTER INSERT instead of BEFORE INSERT
Silently fixing data instead of raising error
Checking on UPDATE instead of INSERT
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.