BEFORE trigger execution in SQL - Time & Space Complexity
When a BEFORE trigger runs in a database, it does some work before a data change happens.
We want to know how the time it takes grows as the data changes grow bigger.
Analyze the time complexity of this BEFORE trigger example.
CREATE TRIGGER check_salary
BEFORE INSERT ON employees
FOR EACH ROW
BEGIN
IF NEW.salary < 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Salary must be positive';
END IF;
END;
This trigger checks each new employee's salary before inserting it.
Look at what repeats when many rows are inserted.
- Primary operation: The trigger runs once for each new row inserted.
- How many times: It runs exactly as many times as there are rows inserted.
Each new row causes the trigger to run once, so the work grows with the number of rows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 trigger checks |
| 100 | 100 trigger checks |
| 1000 | 1000 trigger checks |
Pattern observation: The total work grows directly with the number of rows inserted.
Time Complexity: O(n)
This means the time to run the trigger grows in a straight line with the number of rows inserted.
[X] Wrong: "The trigger runs once no matter how many rows are inserted."
[OK] Correct: Actually, the trigger runs once for each row, so more rows mean more trigger executions.
Understanding how triggers scale helps you write efficient database logic and shows you think about performance in real situations.
"What if the trigger was defined FOR EACH STATEMENT instead of FOR EACH ROW? How would the time complexity change?"
Practice
BEFORE trigger in SQL?Solution
Step 1: Understand trigger timing
ABEFOREtrigger 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 toBEFOREtriggers.Final Answer:
To execute code before a data modification happens -> Option CQuick Check:
BEFORE trigger = runs before data change [OK]
- Confusing BEFORE with AFTER triggers
- Thinking triggers delete data automatically
- Assuming triggers create backups
BEFORE INSERT trigger in SQL?Solution
Step 1: Identify correct trigger timing and event
The question asks for aBEFORE INSERTtrigger, 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; matchesCREATE 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 DQuick Check:
BEFORE INSERT syntax = CREATE TRIGGER trg BEFORE INSERT ON table_name FOR EACH ROW BEGIN ... END; [OK]
- Using AFTER instead of BEFORE
- Mixing INSERT with UPDATE or DELETE
- Omitting FOR EACH ROW clause
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?
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 AQuick Check:
BEFORE trigger can stop invalid inserts [OK]
- Thinking insert succeeds despite error
- Assuming silent ignore instead of error
- Believing trigger changes data automatically
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?
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 AQuick Check:
Use DELIMITER to define trigger body [OK]
- Thinking NEW values can't be changed in BEFORE triggers
- Ignoring delimiter changes for multi-statement bodies
- Missing FOR EACH ROW clause
quantity is never negative. Which trigger code correctly enforces this using a BEFORE INSERT trigger?Solution
Step 1: Identify correct timing and event
We want to check before inserting, soBEFORE INSERTis 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 BQuick Check:
Use BEFORE INSERT + SIGNAL to block invalid data [OK]
- Using AFTER INSERT instead of BEFORE INSERT
- Silently fixing data instead of raising error
- Checking on UPDATE instead of INSERT
Ready to prove it?
Pass the test and this pattern moves unknown
