
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Multiple Insert or Batch Insert in MySQL Query
You need to use VALUES() with comma separation for multiple insert or batch insert at a time. Use the following syntax that does not produce an invalid MySQL query on insert. The syntax is as follows:
INSERT INTO yourTableName VALUES(yourValue1),(yourValue1),(yourValue2),(yourValue3),(yourValue4),(yourValue5),.......N;
To understand the above syntax, let us create a table. The query to create a table is as follows:
mysql> create table addMultipleValues -> ( -> Counter int NOT NULL -> ); Query OK, 0 rows affected (0.60 sec)
Now you can insert batch records in the table using VALUES() with comma separation. The query to insert record is as follows:
mysql> insert into addMultipleValues values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10); Query OK, 10 rows affected (0.27 sec) Records: 10 Duplicates: 0 Warnings: 0
Now display all records from the table using select statement. The query is as follows:
mysql> select *from addMultipleValues;
The following is the output:
+---------+ | Counter | +---------+ | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | | 7 | | 8 | | 9 | | 10 | +---------+ 10 rows in set (0.00 sec)
Advertisements