MySQL query to insert multiple records quickly

MySQLMySQLi Database

To insert multiple records quickly, use a single INSERT and follow the below syntax −

insert into yourTableName values(yourValue1,yourValue2,...N),(yourValue1,yourValue2,...N).....N;

To understand the above syntax, let us create a table −

mysql> create table DemoTable2007
(
   Amount1 int,
   Amount2 int,
   Amount3 int
);
Query OK, 0 rows affected (1.36 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable2007 values(450,600,700),(1000,200,3000),
   (800,900,1200),(1300,1500,2000),(40000,50000,6700);
Query OK, 5 rows affected (0.11 sec)
Records: 5  Duplicates: 0  Warnings: 0

Display all records from the table using select statement −

mysql> select * from DemoTable2007;

This will produce the following output −

+---------+---------+---------+
| Amount1 | Amount2 | Amount3 |
+---------+---------+---------+
|     450 |     600 |     700 |
|    1000 |     200 |    3000 |
|     800 |     900 |    1200 |
|    1300 |    1500 |    2000 |
|   40000 |   50000 |    6700 |
+---------+---------+---------+
5 rows in set (0.00 sec)
raja
Published on 02-Jan-2020 05:56:38
Advertisements