Inserting multiple rows in MySQL?

MySQLMySQLi Database

Insert multiple rows in MySQL with the help of “values”. You can enclose the values with parentheses set with comma separation. The syntax is as follows to insert multiple rows in MySQL.

insert into yourTableName(yourColumnName1,yourColumnName2,..............yourColumnNameN) values(value1,value2,...valueN),(value1,value2,...valueN),(value1,value2,...valueN),...........((value1,value2,...valueN);

Let us now first create a table in MySQL −

mysql> create table InsertMultipleRowsDemo
   -> (
   -> Id int,
   -> Name varchar(200),
   -> Age int
   -> );
Query OK, 0 rows affected (0.45 sec)

Apply the above syntax to insert multiple rows. The query is as follows −

mysql> insert into InsertMultipleRowsDemo(Id,Name,Age) values(1,'John',23),(2,'Carol',24),(3,'Johnson',21),(4,'Smith',20),(5,'David',26);
Query OK, 5 rows affected (0.31 sec)
Records: 5 Duplicates: 0 Warnings: 0

Display all records with the help of select statement. The query is as follows −

mysql> select *from InsertMultipleRowsDemo;

The following is the output −

+------+---------+------+
| Id   | Name    | Age  |
+------+---------+------+
| 1    | John    | 23   |
| 2    | Carol   | 24   |
| 3    | Johnson | 21   |
| 4    | Smith   | 20   |
| 5    | David   | 26   |
+------+---------+------+
5 rows in set (0.00 sec)
raja
Published on 21-Dec-2018 14:36:44
Advertisements