
- 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
Update MySQL table on INSERT command with triggers?
Let us first create a table −
mysql> create table DemoTable1 -> ( -> Id int, -> FirstName varchar(20) -> ); Query OK, 0 rows affected (0.52 sec)
Here is the query to create second table −
mysql> create table DemoTable2 -> ( -> EmployeeId int, -> EmployeeName varchar(20) -> ); Query OK, 0 rows affected (0.51 sec)
Let us now create a trigger to update MySQL table on insert command −
mysql> DELIMITER // mysql> CREATE TRIGGER updateDemoOnInsert -> AFTER INSERT ON DemoTable2 -> FOR EACH ROW BEGIN -> insert into DemoTable1 values(110,'Adam'); -> END -> // Query OK, 0 rows affected (0.14 sec) mysql> DELIMITER ;
Insert some records in the table using insert command −
mysql> insert into DemoTable2 values(101,'Mike'); Query OK, 1 row affected (0.22 sec)
Display all records from the second table using select statement −
mysql> select * from DemoTable2;
This will produce the following output −
+------------+--------------+ | EmployeeId | EmployeeName | +------------+--------------+ | 101 | Mike | +------------+--------------+ 1 row in set (0.00 sec)
Display all records from the first table using select statement −
mysql> select * from DemoTable1;
This will produce the following output −
+------+-----------+ | Id | FirstName | +------+-----------+ | 110 | Adam | +------+-----------+ 1 row in set (0.00 sec)
- Related Articles
- How to insert DATE in MySQL table with TRIGGERS?
- Implement INSERT … ON DUPLICATE KEY UPDATE in MySQL
- Implementing INSERT… ON DUPLICATE KEY UPDATE in MySQL
- Update a MySQL table with Java MySQL
- Show constraints on table command in MySQL?
- Update table with duplicate ids in MySQL
- Insert record in a MySQL table with Java
- Update a table based on StudentId value in MySQL?
- How can we use WHERE clause with MySQL INSERT INTO command?
- How we can find all the triggers associated with a particular MySQL table?
- MySQL INSERT INTO SELECT into a table with AUTO_INCREMENT
- Why does the update command in MySQL insist on using slanted single quotes?
- MySQL command to copy table?
- Update multiple values in a table with MySQL IF Statement
- How to generate a “create table” command based on an existing table in MySQL?

Advertisements