Insert multiple rows in a single MySQL query

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable1384
   -> (
   -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> StudentName varchar(20),
   -> StudentAge int
   -> );
Query OK, 0 rows affected (0.63 sec)

Insert some records in the table using insert command. Here, we are inserting multiple rows in a single query −

mysql> insert into DemoTable1384(StudentName,StudentAge) values('Chris Brown',21),('David Miller',22),
-> ('Carol Taylor',19),('Adam Smith',23);
Query OK, 4 rows affected (0.11 sec)
Records: 4  Duplicates: 0  Warnings: 0

Display all records from the table using select statement −

mysql> select * from DemoTable1384;

This will produce the following output −

+-----------+--------------+------------+
| StudentId | StudentName  | StudentAge |
+-----------+--------------+------------+
|         1 | Chris Brown  |         21 |
|         2 | David Miller |         22 |
|         3 | Carol Taylor |         19 |
|         4 | Adam Smith   |         23 |
+-----------+--------------+------------+
4 rows in set (0.00 sec)
raja
Published on 11-Nov-2019 10:09:47
Advertisements