Is there any easy way to add multiple records in a single MySQL query?


You can easily add multiple items with only one insert command. 

The syntax is as follows −

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

Let us first create a table −

mysql> create table DemoTable
(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   Value1 int,
   Value2 int,
   Value3 int
);
Query OK, 0 rows affected (0.79 sec)

Insert multiple records in the table using insert command −

mysql> insert into DemoTable(Value1,Value2,Value3) values(10,20,40),(100,148,120),(150,670,1000),
(100000,200000,409999);
Query OK, 4 rows affected (0.17 sec)
Records : 4 Duplicates : 0 Warnings : 0

Following is the query to display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----+--------+--------+--------+
| Id | Value1 | Value2 | Value3 |
+----+--------+--------+--------+
| 1  | 10     | 20     | 40     |
| 2  | 100    | 148    | 120    |
| 3  | 150    | 670    | 1000   |
| 4  | 100000 | 200000 | 409999 |
+----+--------+--------+--------+
4 rows in set (0.00 sec)

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

100 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements