An AFTER INSERT trigger is a special set of instructions that run automatically right after a new row is added to a table. It lets you perform extra actions based on the new data.
Click to reveal answer
beginner
When exactly does an AFTER INSERT trigger execute?
It executes immediately after the new row is successfully inserted into the table, allowing you to react to the insertion.
Click to reveal answer
intermediate
Can an AFTER INSERT trigger modify the row that was just inserted?
No, AFTER INSERT triggers cannot change the inserted row because the insertion is already done. To modify data before insert, use BEFORE INSERT triggers.
Click to reveal answer
beginner
How do you access the new row's data inside an AFTER INSERT trigger?
You use the keyword NEW followed by the column name, like NEW.column_name, to get the values of the inserted row.
Click to reveal answer
beginner
Give a simple example use case for an AFTER INSERT trigger.
You can use it to log every new user added to a users table into a separate audit table, keeping track of who was added and when.
Click to reveal answer
When does an AFTER INSERT trigger run in MySQL?
AWhen a row is deleted
BRight after a new row is inserted
CWhen a row is updated
DBefore a new row is inserted
✗ Incorrect
AFTER INSERT triggers run immediately after a new row is successfully inserted into the table.
Which keyword lets you access the new row's data inside an AFTER INSERT trigger?
ANEW
BOLD
CCURRENT
DROW
✗ Incorrect
The NEW keyword is used to access the inserted row's column values inside the trigger.
Can an AFTER INSERT trigger modify the inserted row's data?
AOnly if the trigger is BEFORE INSERT
BYes, always
COnly if the table has no primary key
DNo, it cannot
✗ Incorrect
AFTER INSERT triggers cannot modify the inserted row because the insertion is already complete.
Which of these is a common use of AFTER INSERT triggers?
ALog inserted data to another table
BPrevent insertion of duplicate rows
CValidate data before insert
DUpdate the inserted row's values
✗ Incorrect
AFTER INSERT triggers are often used to log or audit inserted data after it is saved.
What happens if an AFTER INSERT trigger fails during execution?
AThe insert still succeeds but trigger actions are skipped
BThe database shuts down
CThe insert is rolled back
DThe trigger runs again automatically
✗ Incorrect
If an AFTER INSERT trigger fails, the entire insert operation is rolled back to keep data consistent.
Explain what an AFTER INSERT trigger is and when it runs in MySQL.
Think about what happens right after you add a new row to a table.
You got /3 concepts.
Describe a practical example where you might use an AFTER INSERT trigger.
Consider how you might keep track of changes automatically.
You got /3 concepts.
Practice
(1/5)
1. What does an AFTER INSERT trigger do in MySQL?
easy
A. It deletes rows after insertion.
B. It runs before a row is inserted into a table.
C. It runs automatically after a new row is added to a table.
D. It prevents rows from being inserted.
Solution
Step 1: Understand trigger timing
An AFTER INSERT trigger 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 C
Quick Check:
AFTER INSERT trigger = runs after insert [OK]
Hint: AFTER INSERT triggers run only after data is added [OK]
Common Mistakes:
Confusing AFTER with BEFORE triggers
Thinking triggers delete or block inserts
Assuming triggers run before insertion
2. Which of the following is the correct syntax to create an AFTER INSERT trigger in MySQL?
easy
A. CREATE TRIGGER trg AFTER UPDATE ON table_name FOR EACH ROW BEGIN ... END;
B. CREATE TRIGGER trg BEFORE INSERT ON table_name FOR EACH ROW BEGIN ... END;
C. CREATE TRIGGER trg AFTER DELETE ON table_name FOR EACH ROW BEGIN ... END;
D. CREATE TRIGGER trg AFTER INSERT ON table_name FOR EACH ROW BEGIN ... END;
Solution
Step 1: Identify trigger timing and event
The question asks for an AFTER INSERT trigger, so the syntax must include AFTER INSERT.
Step 2: Match syntax structure
The correct syntax is CREATE 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 D
Quick Check:
AFTER INSERT trigger syntax = CREATE TRIGGER trg AFTER INSERT ON table_name FOR EACH ROW BEGIN ... END; [OK]
Hint: AFTER INSERT triggers use 'AFTER INSERT' in syntax [OK]
Common Mistakes:
Using BEFORE instead of AFTER
Using wrong event like UPDATE or DELETE
Omitting FOR EACH ROW clause
3. Given this trigger and table:
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?
medium
A. [{"msg": "User Alice added"}]
B. [{"msg": "User 1 added"}]
C. Empty result set
D. Syntax error
Solution
Step 1: Understand trigger action on insert
The trigger inserts a log message using NEW.name after 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 A
Quick Check:
NEW.name = 'Alice' logged [OK]
Hint: NEW accesses inserted row data inside AFTER INSERT [OK]
Common Mistakes:
Using NEW.id instead of NEW.name for message
Expecting no logs created
Confusing syntax causing errors
4. You wrote this trigger but get an error:
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?
medium
A. Missing BEGIN and END block for multiple statements
B. AFTER INSERT triggers cannot insert into other tables
C. FOR EACH ROW is not allowed in AFTER triggers
D. Trigger name cannot be 'trg'
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 A
Quick Check:
Triggers need BEGIN...END for statements [OK]
Hint: Always use BEGIN...END for trigger statements [OK]
Common Mistakes:
Assuming single statement triggers don't need BEGIN...END
Thinking AFTER triggers can't modify other tables
Misunderstanding FOR EACH ROW usage
5. You want to create an AFTER INSERT trigger on a sales table that updates a summary table with the total sales amount. Which approach is correct?
hard
A. Use OLD.amount in the trigger to subtract from the summary table's total.
B. Use NEW.amount in the trigger to add to the summary table's total after each insert.
C. Use a BEFORE INSERT trigger to update the summary table before inserting sales.
D. Use a DELETE trigger to update the summary table after sales are removed.
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.amount gives 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 B
Quick Check:
AFTER INSERT + NEW = update summary correctly [OK]
Hint: Use NEW in AFTER INSERT to access inserted data [OK]
Common Mistakes:
Using OLD in INSERT triggers (OLD is NULL)
Using BEFORE INSERT instead of AFTER INSERT
Using DELETE triggers for insert updates
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.