AFTER INSERT triggers in MySQL - Time & Space Complexity
When using AFTER INSERT triggers in MySQL, it's important to understand how the time to run your database operations changes as you add more data.
We want to know how the trigger affects the total work the database does when new rows are inserted.
Analyze the time complexity of the following AFTER INSERT trigger.
CREATE TRIGGER after_insert_example
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.
Let's find the repeating parts in this trigger.
- Primary operation: The trigger runs an INSERT into audit_log for each new row inserted into orders.
- How many times: Once per inserted row, because the trigger is FOR EACH ROW.
As you insert more rows, the trigger runs once for each row.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 inserts into audit_log |
| 100 | 100 inserts into audit_log |
| 1000 | 1000 inserts into audit_log |
Pattern observation: The work grows directly with the number of inserted rows.
Time Complexity: O(n)
This means the time to complete the inserts and trigger actions grows linearly with the number of rows inserted.
[X] Wrong: "The trigger runs only once no matter how many rows are inserted."
[OK] Correct: In MySQL, AFTER INSERT triggers defined FOR EACH ROW run once per inserted row, so the work adds up with more rows.
Understanding how triggers add work helps you design efficient databases and explain your choices clearly in conversations.
What if the trigger was defined FOR EACH STATEMENT instead of FOR EACH ROW? How would the time complexity change?
Practice
AFTER INSERT trigger do in MySQL?Solution
Step 1: Understand trigger timing
AnAFTER INSERTtrigger runs after a new row is successfully inserted into a table.Step 2: Identify trigger purpose
This trigger is used to perform actions like logging or updating related tables after insertion.Final Answer:
It runs automatically after a new row is added to a table. -> Option CQuick Check:
AFTER INSERT trigger = runs after insert [OK]
- Confusing AFTER with BEFORE triggers
- Thinking triggers delete or block inserts
- Assuming triggers run before insertion
Solution
Step 1: Identify trigger timing and event
The question asks for an AFTER INSERT trigger, so the syntax must includeAFTER INSERT.Step 2: Match syntax structure
The correct syntax isCREATE TRIGGER trg AFTER INSERT ON table_name FOR EACH ROW BEGIN ... END;.Final Answer:
CREATE TRIGGER trg AFTER INSERT ON table_name FOR EACH ROW BEGIN ... END; -> Option DQuick Check:
AFTER INSERT trigger syntax = CREATE TRIGGER trg AFTER INSERT ON table_name FOR EACH ROW BEGIN ... END; [OK]
- Using BEFORE instead of AFTER
- Using wrong event like UPDATE or DELETE
- Omitting FOR EACH ROW clause
CREATE TABLE users (id INT, name VARCHAR(20));
CREATE TABLE logs (msg VARCHAR(50));
DELIMITER $$
CREATE TRIGGER after_user_insert AFTER INSERT ON users
FOR EACH ROW BEGIN
INSERT INTO logs VALUES (CONCAT('User ', NEW.name, ' added'));
END$$
DELIMITER ;
INSERT INTO users VALUES (1, 'Alice');
SELECT * FROM logs;What will the SELECT query return?
Solution
Step 1: Understand trigger action on insert
The trigger inserts a log message usingNEW.nameafter a user is added.Step 2: Check inserted data and log content
Inserting (1, 'Alice') triggers log insertion with message 'User Alice added'.Final Answer:
[{"msg": "User Alice added"}] -> Option AQuick Check:
NEW.name = 'Alice' logged [OK]
- Using NEW.id instead of NEW.name for message
- Expecting no logs created
- Confusing syntax causing errors
CREATE TRIGGER trg AFTER INSERT ON users
FOR EACH ROW
INSERT INTO logs VALUES ('New user added');
INSERT INTO logs VALUES ('Trigger fired');What is the problem?
Solution
Step 1: Check trigger body syntax
When a trigger has more than one statement or uses procedural code, it needs BEGIN ... END block.Step 2: Identify missing block
The trigger has multiple statements but lacks BEGIN ... END, which is required for clarity and to avoid errors.Final Answer:
Missing BEGIN and END block for multiple statements -> Option AQuick Check:
Triggers need BEGIN...END for statements [OK]
- Assuming single statement triggers don't need BEGIN...END
- Thinking AFTER triggers can't modify other tables
- Misunderstanding FOR EACH ROW usage
Solution
Step 1: Identify correct trigger timing for updating after insert
AFTER INSERT triggers run after new rows are added, so they can safely update summary data using inserted values.Step 2: Use NEW to access inserted row data
Inside the trigger,NEW.amountgives the inserted sale amount to add to the summary.Final Answer:
Use NEW.amount in the trigger to add to the summary table's total after each insert. -> Option BQuick Check:
AFTER INSERT + NEW = update summary correctly [OK]
- Using OLD in INSERT triggers (OLD is NULL)
- Using BEFORE INSERT instead of AFTER INSERT
- Using DELETE triggers for insert updates
Ready to prove it?
Pass the test and this pattern moves unknown
