how to insert data into multiple tables using single query in mysql?

how to insert data into multiple tables using single query in mysql – MySQL Insert into multiple tables? Using the Insert query, i can add insert data into multiple tables using stored procedure in PHP with MySQL.

how to insert data into multiple tables using single query in mysql

You can use stored procedure to insert into two tables in a single query.

BEGIN;
INSERT INTO members (membername, password)
  VALUES('tamilrokers', 'tamilrokers');
INSERT INTO profiles (memberid, information, homepage) 
  VALUES(LAST_INSERT_ID(),'Welcome Pakainfo!', 'https://www.pakainfo.com');
COMMIT;

How to insert into two tables using a single MySQL query?

mysql> create table memberTbl
(
   EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   EmployeeFirstName varchar(20)
);
Query OK, 0 rows affected (0.56 sec)
mysql> create table memberTbl2
(
   ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   ClientName varchar(20),
   ClientAge int
);
Query OK, 0 rows affected (0.76 sec)

create stored procedure to insert into two tables

mysql> DELIMITER //
   mysql> CREATE PROCEDURE insert_into_twoTables(name varchar(100),age int)
      BEGIN
         INSERT INTO memberTbl(EmployeeFirstName) VALUES(name);
         INSERT INTO memberTbl2(ClientName,ClientAge) VALUES(name,age);
      END
      //
   Query OK, 0 rows affected (0.14 sec)
mysql> DELIMITER ;

Don’t miss : How To Insert Data Into MySQL Database Table Using PHP?

Now call the stored procedure with the help of CALL command −

mysql> call insert_into_twoTables('Tom',38);
Query OK, 1 row affected, 1 warning (0.41 sec)

insert data into multiple tables mysql

BEGIN;
INSERT INTO members (membername, password)
  VALUES('tamilrokers', 'tamilrokers');
INSERT INTO profiles (memberid, information, homepage) 
  VALUES(LAST_INSERT_ID(),'Welcome To Pakainfo!', 'https://www.pakainfo.com');
COMMIT;

I hope you get an idea about how to insert data into multiple tables using single query in mysql.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment