Last Updated : 23 Jul, 2025
An "AFTER INSERT" trigger in MySQL automatically executes specified actions after a new row is inserted into a table. It is used to perform tasks such as updating related tables, logging changes or performing calculations, ensuring immediate and consistent data processing.
In this article, We will learn about the MySQL After Insert Trigger in detail by understanding various examples and so on.
What is an AFTER INSERT TriggerSyntax for Creating an AFTER INSERT Trigger
The basic syntax for creating an "AFTER INSERT" trigger is as follows:
CREATE TRIGGER trigger_name
AFTER INSERT ON table_name
FOR EACH ROW
BEGIN
-- trigger logic here
END;
where,
Suppose that you have an 'orders' table and want to create an "AFTER INSERT" trigger that logs the details of an order into a table called 'order_log'.
Trigger CreationCREATE TRIGGER log_order_insertInsert a Row into the orders Table:
AFTER INSERT ON orders
FOR EACH ROW
BEGIN
INSERT INTO order_log (order_id, customer_id, order_date, total_amount)
VALUES (NEW.order_id, NEW.customer_id, NEW.order_date, NEW.total_amount);
END;
INSERT INTO orders (order_id, customer_id, order_date, total_amount)Trigger Execution:
VALUES (1, 123, '2024-07-30', 250.00);
INSERT INTO order_log (order_id, customer_id, order_date, total_amount)
VALUES (1, 123, '2024-07-30', 250.00);
Explanation:
Output:
orders Table:
order_id
customer_id
order_date
total_amount
1
123
2024-07-30
250.00
order_log Table:
order_id
customer_id
order_date
total_amount
1
123
2024-07-30
250.00
Example 2:Suppose one has a database for a library system. There should be a table for holding data about the books and another for logging each new book added into the library.
Create the books TableCREATE TABLE books (Create the book_log Table
book_id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255),
author VARCHAR(255),
published_date DATE,
genre VARCHAR(100),
added_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE book_log (Create the AFTER INSERT Trigger
log_id INT AUTO_INCREMENT PRIMARY KEY,
book_id INT,
log_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
action VARCHAR(50)
);
CREATE TRIGGER log_book_insertInsert a New Book to Test the Trigger
AFTER INSERT ON books
FOR EACH ROW
BEGIN
INSERT INTO book_log (book_id, log_date, action)
VALUES (NEW.book_id, NOW(), 'Book Added');
END;
INSERT INTO books (title, author, published_date, genre)Check the Data in the books Table
VALUES ('The Great Gatsby', 'F. Scott Fitzgerald', '1925-04-10', 'Fiction');
SELECT * FROM books;
Output:
book_id
title
author
published_date
genre
added_date
1
The Great Gatsby
F. Scott Fitzgerald
1925-04-10
Fiction
2024-07-30 12:34:56
ConclusionMySQL's "AFTER INSERT" triggers are very useful in performing an action based on a new row being inserted into a table. They improve the working of your database by logging, updating related tables, and integrity checking of your data—all without manual intervention. Applying and understanding these triggers will help you automate operations, enforce business rules, and maintain consistency over your data.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4