-1

I have three tables with same fields and same field's name. Now I want to insert same data into three tables using only one insert query.

My tables are: temp1:

+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | varchar(20) | NO   | PRI | NULL    |       |
| name    | varchar(20) | YES  |     | NULL    |       |
| address | varchar(20) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+

temp2:

+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | varchar(20) | NO   | PRI | NULL    |       |
| name    | varchar(20) | YES  |     | NULL    |       |
| address | varchar(20) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+

temp3:

+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | varchar(20) | NO   | PRI | NULL    |       |
| name    | varchar(20) | YES  |     | NULL    |       |
| address | varchar(20) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+

I want to insert same value for each table.

I have tried this code:

insert into temp1,temp2,temp3 values('abcd','sangho','rajpur');

Please help me. its really important.

GMB
  • 216,147
  • 25
  • 84
  • 135
  • Why would you have 3 tables with the same schema AND the same data in them?? I cant think of a relational database rule that does not break – RiggsFolly Sep 02 '19 at 14:02
  • You generally _can't_ insert into three different tables without using three separate insert statements. But...why are you maintaining three separate tables with identical structure in the first place? – Tim Biegeleisen Sep 02 '19 at 14:03
  • 2
    Possible duplicate of [MySQL Insert into multiple tables? (Database normalization?)](https://stackoverflow.com/questions/5178697/mysql-insert-into-multiple-tables-database-normalization) – GMB Sep 02 '19 at 14:06

1 Answers1

0

Since SQL doesn't support such statements, you could just chain the two statements using a semicolon like so

INSERT INTO temp1 VALUES(1, 2, 3);INSERT INTO temp2 VALUES(1, 2, 3);

Also take a look at this answer using transactions.

mohkamfer
  • 435
  • 3
  • 12