
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Implement MySQL trigger in the first table to insert records in the second table?
For this, the syntax is as follows −
DELIMITER // create trigger yourTriggerName before insert on yourTableName1 for each row begin insert into yourTableName2 values (yourValue1,yourValue2,...N); end ; // DELIMITER ;
Let us first create a table −
mysql> create table DemoTable1 ( StudentId int, StudentName varchar(40) ); Query OK, 0 rows affected (0.69 sec)
Here is the query to create second table −
mysql> create table DemoTable2( Id int, Name varchar(40) ); Query OK, 0 rows affected (0.61 sec)
Here is the query for trigger before insert −
mysql> DELIMITER // mysql> create trigger afterinserttrigger before insert on DemoTable1 for each row begin insert into DemoTable2 values (10,'Chris'); end ; // Query OK, 0 rows affected (0.13 sec) mysql> DELIMITER ;
Let us insert the record into first table −
mysql> insert into DemoTable1 values(1,'David'); Query OK, 1 row affected (0.21 sec)
Now you can display all records from the first table using select statement −
mysql> select *from DemoTable1;
This will produce the following output −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 1 | David | +-----------+-------------+ 1 row in set (0.00 sec)
Let us check the table records of second table −
mysql> select *from DemoTable2;
This will produce the following output −
+------+-------+ | Id | Name | +------+-------+ | 10 | Chris | +------+-------+ 1 row in set (0.00 sec)
- Related Questions & Answers
- A single MySQL query to insert records (not all) in the second table from the first table
- Insert values from the first table to the second table using two SELECT statements in a single MySQL query
- MySQL trigger to insert row into another table?
- A single MySQL query to select value from first table and insert in the second?
- INSERT INTO table if a table exists in MySQL else implement CREATE TABLE and create the table
- Fetch maximum ID value from the first table and insert to all the IDs in another table with MySQL INSERT INTO select?
- Implement GREATEST() in MySQL and update the table?
- Can we insert records in a MySQL table without auto_increment values?
- How can I update a field in a MySQL database table by adding a value in the second table with a value from the first table?
- Get the second last row of a table in MySQL?
- Delete all the records from a MySQL table?
- Count duplicates records in MySQL table?
- Insert the data into Table C IF The data is not in Table B while Comparing to Table A in MySQL?
- Take all records from one MySQL table and insert it to another?
- How to get the second last record from a table in MySQL?
Advertisements