Fastest way to insert with multiple values in a single MySQL query?

MySQLMySQLi Database

Do not use the below query for this −

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

You can use below query as the fastest way to insert with multiple values in a single query −

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

Let us first create a table −

mysql> create table DemoTable1466
-> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> Name varchar(20),
   -> Age int
   -> );
Query OK, 0 rows affected (0.98 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1466(Name,Age) values('Chris',25),('David',29),('Mike',32);
Query OK, 3 rows affected (0.10 sec)
Records: 3  Duplicates: 0  Warnings: 0

Display all records from the table using select statement −

mysql> select * from DemoTable1466;

This will produce the following output −

+----+-------+------+
| Id | Name  | Age  |
+----+-------+------+
|  1 | Chris |   25 |
|  2 | David |   29 |
|  3 | Mike  |   32 |
+----+-------+------+
3 rows in set (0.00 sec)
raja
Published on 10-Dec-2019 11:39:59
Advertisements