Create a Trigger to Log Inserts
In this step, you will learn how to create a trigger in MySQL that logs insert operations into a separate table. Triggers are special stored programs that automatically execute in response to certain events on a table, such as INSERT
, UPDATE
, or DELETE
. This is useful for auditing changes, enforcing business rules, or performing other actions based on data modifications.
First, let's connect to the MySQL server. Open a terminal and execute the following command:
mysql -u root
You will be prompted for the root password. Enter the password and press Enter.
Next, create a database named mydatabase
if it doesn't already exist:
CREATE DATABASE IF NOT EXISTS mydatabase;
USE mydatabase;
Now, let's create a table named products
:
CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
price DECIMAL(10, 2) NOT NULL
);
We also need a table to log the insert operations. Let's create a table named product_logs
:
CREATE TABLE product_logs (
log_id INT AUTO_INCREMENT PRIMARY KEY,
product_id INT,
product_name VARCHAR(255),
product_price DECIMAL(10, 2),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Now, let's create the trigger. The trigger will be executed after each insert operation on the products
table. The trigger will insert a record into the product_logs
table with the details of the inserted product.
CREATE TRIGGER products_after_insert
AFTER INSERT ON products
FOR EACH ROW
BEGIN
INSERT INTO product_logs (product_id, product_name, product_price)
VALUES (NEW.id, NEW.name, NEW.price);
END;
Let's break down the trigger definition:
CREATE TRIGGER products_after_insert
: This statement creates a trigger named products_after_insert
.
AFTER INSERT ON products
: This specifies that the trigger will be executed after each insert operation on the products
table.
FOR EACH ROW
: This indicates that the trigger will be executed for each row that is inserted into the products
table.
BEGIN ... END
: This block contains the SQL statements that will be executed when the trigger is activated.
NEW.id
, NEW.name
, NEW.price
: These refer to the values of the id
, name
, and price
columns of the newly inserted row.
Now, let's insert some data into the products
table to test the trigger:
INSERT INTO products (name, price) VALUES ('Laptop', 1200.00);
INSERT INTO products (name, price) VALUES ('Mouse', 25.00);
Finally, let's check the product_logs
table to see if the trigger has inserted the log records:
SELECT * FROM product_logs;
You should see two records in the product_logs
table, corresponding to the two insert operations we performed on the products
table.
+--------+------------+--------------+---------------+---------------------+
| log_id | product_id | product_name | product_price | created_at |
+--------+------------+--------------+---------------+---------------------+
| 1 | 1 | Laptop | 1200.00 | 2024-10-27 12:00:00 |
| 2 | 2 | Mouse | 25.00 | 2024-10-27 12:00:00 |
+--------+------------+--------------+---------------+---------------------+
2 rows in set (0.00 sec)