How to insert an array of values in a MySQL table with a single INSERT?

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable
(
   ClientId int,
   ClientName varchar(50)
);
Query OK, 0 rows affected (0.62 sec)

Insert some records in the table using insert command. Here, we are inserting multiple values using only a single INSERT −

mysql> insert into DemoTable values(101,'Adam'),(102,'Chris'),(103,'Robert'),(104,'Sam'),(105,'Mike');
Query OK, 5 rows affected (0.16 sec)
Records: 5 Duplicates: 0 Warnings: 0

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----------+------------+
| ClientId | ClientName |
+----------+------------+
|      101 | Adam       |
|      102 | Chris      |
|      103 | Robert     |
|      104 | Sam        |
|      105 | Mike       |
+----------+------------+
5 rows in set (0.00 sec)
raja
Published on 03-Oct-2019 11:02:18
Advertisements