Multiple Inserts for a single column in MySQL?

MySQLMySQLi Database

The syntax for multiple inserts for a single column in MySQL is as follows −

INSERT INTO yourTableName(yourColumnName) values(‘yourValue1'),(‘yourValue2'),(‘yourValue3'),(‘yourValue4'),.........N;

To understand the above syntax, let us create a table. The query to create a table is as follows −

mysql> create table InsertMultipleDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> UserName varchar(10),
   -> UserRole varchar(20)
   -> ,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (3.14 sec)

Now you can insert some records in the table using insert command. Here is the query to insert multiple values using insert command −

mysql> insert into InsertMultipleDemo(UserRole) values('Admin'),('Author'),('Driver'),('Guest');
Query OK, 4 rows affected (0.17 sec)
Records: 4 Duplicates: 0 Warnings: 0

As you can see above, 4 rows are affected.

Display all records from the table using select statement. The query is as follows −

mysql> select UserRole from InsertMultipleDemo;

The following is the output −

+----------+
| UserRole |
+----------+
| Admin    |
| Author   |
| Driver   |
| Guest    |
+----------+
4 rows in set (0.00 sec)
raja
Published on 27-Feb-2019 13:07:31
Advertisements