INSERT trigger in SQL - Time & Space Complexity
When a new row is added to a table, an INSERT trigger runs automatically. We want to understand how the time it takes to run this trigger changes as more rows are inserted.
How does the work inside the trigger grow when many inserts happen?
Analyze the time complexity of the following INSERT trigger.
CREATE TRIGGER trg_after_insert
AFTER INSERT ON orders
FOR EACH ROW
BEGIN
INSERT INTO audit_log(order_id, action_time)
VALUES (NEW.id, NOW());
END;
This trigger adds a record to an audit_log table every time a new order is inserted.
Look for repeated actions that affect performance.
- Primary operation: The trigger runs once for each inserted row.
- How many times: Exactly once per row inserted.
Each inserted row causes one trigger execution, which does a simple insert into audit_log.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 trigger runs, 10 audit inserts |
| 100 | 100 trigger runs, 100 audit inserts |
| 1000 | 1000 trigger runs, 1000 audit inserts |
Pattern observation: The work grows directly with the number of inserted rows.
Time Complexity: O(n)
This means the total work grows in a straight line as you add more rows.
[X] Wrong: "The trigger runs once no matter how many rows are inserted."
[OK] Correct: The trigger runs once for each row, so more rows mean more trigger executions.
Understanding how triggers scale helps you design efficient databases and avoid surprises when data grows.
"What if the trigger contained a loop that processed all rows in the table? How would the time complexity change?"
Practice
INSERT trigger in a database?Solution
Step 1: Understand what triggers do
Triggers are special procedures that run automatically in response to certain events in the database.Step 2: Focus on INSERT triggers
An INSERT trigger specifically runs when a new row is added to a table.Final Answer:
To automatically run code when a new row is added to a table -> Option BQuick Check:
INSERT trigger = runs on new row insert [OK]
- Confusing INSERT triggers with UPDATE or DELETE triggers
- Thinking triggers create or drop tables
- Believing triggers run manually
INSERT trigger in SQL?Solution
Step 1: Identify correct event and timing
For an INSERT trigger, the event is INSERT and timing can be BEFORE or AFTER. CREATE TRIGGER trg AFTER INSERT ON table_name FOR EACH ROW BEGIN ... END; uses AFTER INSERT which is valid.Step 2: Check syntax structure
CREATE TRIGGER trg AFTER INSERT ON table_name FOR EACH ROW BEGIN ... END; uses correct syntax: CREATE TRIGGER name AFTER INSERT ON table FOR EACH ROW BEGIN ... END;Final Answer:
CREATE TRIGGER trg AFTER INSERT ON table_name FOR EACH ROW BEGIN ... END; -> Option AQuick Check:
Correct INSERT trigger syntax = CREATE TRIGGER trg AFTER INSERT ON table_name FOR EACH ROW BEGIN ... END; [OK]
- Using DELETE or UPDATE instead of INSERT
- Wrong order of keywords
- Missing FOR EACH ROW clause
CREATE TRIGGER trg_log AFTER INSERT ON employees FOR EACH ROW BEGIN INSERT INTO log_table VALUES (NEW.id, NEW.name); END;
What happens when a new employee row is inserted?
Solution
Step 1: Understand the trigger action
The trigger runs AFTER a new row is inserted into employees. It inserts a row into log_table using NEW.id and NEW.name.Step 2: Confirm NEW usage
NEW refers to the newly inserted row's values, so the insert into log_table uses correct values.Final Answer:
A new row is added to log_table with the inserted employee's id and name -> Option AQuick Check:
NEW accesses inserted row data [OK]
- Thinking NEW is invalid outside triggers
- Assuming trigger deletes data
- Confusing AFTER and BEFORE timing
CREATE TRIGGER trg_check BEFORE INSERT ON orders FOR EACH ROW BEGIN IF NEW.quantity < 0 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Quantity cannot be negative'; END IF; END;
Solution
Step 1: Check trigger syntax for multiple statements
Inside BEGIN...END, multiple statements need delimiters (like semicolons) or the delimiter must be changed before creating the trigger.Step 2: Confirm other parts are valid
NEW can be used in BEFORE INSERT triggers, SIGNAL SQLSTATE is valid in many SQL dialects, and BEFORE INSERT is allowed.Final Answer:
Missing delimiter to separate statements inside BEGIN...END -> Option DQuick Check:
Multiple statements need delimiters [OK]
- Forgetting to change delimiter before trigger creation
- Misusing NEW in triggers
- Confusing BEFORE and AFTER timing rules
INSERT trigger that prevents inserting duplicate email addresses into a users table. Which approach is best?Solution
Step 1: Understand when to prevent duplicates
To stop duplicates before they happen, use a BEFORE INSERT trigger to check if the email already exists.Step 2: Why other options fail
AFTER INSERT triggers run after insertion, so deleting duplicates after insert is inefficient and can cause issues. Updating existing rows or logging duplicates does not prevent duplicates.Final Answer:
Use a BEFORE INSERT trigger to check if NEW.email exists and raise an error if yes -> Option CQuick Check:
Prevent duplicates before insert with BEFORE INSERT trigger [OK]
- Trying to fix duplicates after insertion
- Using triggers to update instead of prevent
- Ignoring unique constraints and relying only on triggers
Ready to prove it?
Pass the test and this pattern moves unknown
