A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/mysql/mysql-after-insert-trigger/ below:

MySQL After Insert Trigger - GeeksforGeeks

MySQL After Insert Trigger

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 Trigger

Syntax 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,

Example of an AFTER INSERT Trigger

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 Creation
CREATE TRIGGER log_order_insert
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 a Row into the orders Table:
INSERT INTO orders (order_id, customer_id, order_date, total_amount)
VALUES (1, 123, '2024-07-30', 250.00);
Trigger Execution:
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 Table
CREATE TABLE books (
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 the book_log Table
CREATE TABLE book_log (
log_id INT AUTO_INCREMENT PRIMARY KEY,
book_id INT,
log_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
action VARCHAR(50)
);
Create the AFTER INSERT Trigger
CREATE TRIGGER log_book_insert
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 a New Book to Test the Trigger
INSERT INTO books (title, author, published_date, genre)
VALUES ('The Great Gatsby', 'F. Scott Fitzgerald', '1925-04-10', 'Fiction');
Check the Data in the books Table
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

Conclusion

MySQL'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