
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL trigger to insert row into another table?
Let us first create a table. The CREATE command is used to create a table.
mysql> create table Table1 -> ( -> id int, -> name varchar(100) -> ); Query OK, 0 rows affected (0.62 sec)
Let us now create another table.
mysql> create table Table2 -> ( -> id int, -> name varchar(100) -> ); Query OK, 0 rows affected (0.49 sec)
Now, the following is how you can create a trigger.
mysql> delimiter # mysql> create trigger Table1Trigger after insert on Table1 -> for each row -> begin -> insert into Table2(id, name) values (new.id, new.name); -> end# Query OK, 0 rows affected (0.29 sec) mysql> delimiter ;
To create a trigger, we need to change the delimiter.
Inserting the row into Table1 activates the trigger and inserts the records into Table2. To insert record in Table1.
mysql> insert into Table1 values(1,'John'),(2,'Smith'),(3,'Carol'); Query OK, 3 rows affected (0.28 sec) Records: 3 Duplicates: 0 Warnings: 0
To check if the records are inserted in both tables or not.
mysql> select *from Table1;
Here is the output that shows record inserted successfully in Table1.
+------+-------+ | id | name | +------+-------+ | 1 | John | | 2 | Smith | | 3 | Carol | +------+-------+ 3 rows in set (0.00 sec)
To check for second table.
mysql> select *from Table2;
The following is the output that shows record inserted successfully in Table2.
+------+-------+ | id | name | +------+-------+ | 1 | John | | 2 | Smith | | 3 | Carol | +------+-------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL statement to copy data from one table and insert into another table
- MySQL query for INSERT INTO using values from another table?
- How can we insert a new row into a MySQL table?
- Implement MySQL trigger in the first table to insert records in the second table?
- Insert JSON into a MySQL table?
- How to ceate MySQL Trigger before Insert?
- Insert data from one table to another in MySQL?
- Fetch maximum ID value from the first table and insert to all the IDs in another table with MySQL INSERT INTO select?
- How do I INSERT INTO from one MySQL table into another table and set the value of one column?
- MySQL INSERT INTO SELECT resulting in multiple rows inserted at once from another table
- How to insert values from one table into another in PostgreSQL?
- Insert datetime into another datetime field in MySQL?
- MySQL INSERT INTO SELECT into a table with AUTO_INCREMENT
- Insert values in a table by MySQL SELECT from another table in MySQL?
- Select some data from a database table and insert into another table in the same database with MySQL

Advertisements