
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to do a batch insert in MySQL?
To do a batch insert, we need to use all column names with parenthesis, separated by ‘,’.
Let us see an example. First, we will create a table. The following is the CREATE command to create a table.
mysql> CREATE table MultipleRecordWithValues - > ( - > id int, - > name varchar(100) - > ); Query OK, 0 rows affected (0.88 sec)
The following is the syntax of batch insert.
INSERT into yourTableName values(column1,column2,....N),(column1,column2,....N),(column1,column2,....N),...........N;
Applying the above syntax to insert batch records.
mysql> insert into MultipleRecordWithValues values(1,'John'),(2,'Carol'),(3,'Bob'),(4,'Smith'); Query OK, 4 rows affected (0.16 sec) Records: 4 Duplicates: 0 Warnings: 0
Since 4 rows are affected, it implies that we have inserted records successfully. To check if all records are present in the table, use the SELECT command.
mysql> select *from MultipleRecordWithValues;
The following is the output.
+------+-------+ | id | name | +------+-------+ | 1 | John | | 2 | Carol | | 3 | Bob | | 4 | Smith | +------+-------+ 4 rows in set (0.00 sec)
- Related Articles
- How to multiple insert or batch insert at a time in MySQL query?
- How to Execute Batch Insert Update in Hibernate?
- How do I insert a NULL value in MySQL?
- How to batch update MySQL table?
- Using MySQL in Batch Mode
- How to insert a Python object in MySQL?
- How do I insert multiple values in a column with a single MySQL query?
- How to form a MySQL Conditional Insert?
- How do we insert/store a file into MySQL database using JDBC?
- How can we run MySQL statements in batch mode?
- How to insert a Python tuple in a MySQL database?
- How to insert an array of values in a MySQL table with a single INSERT?
- How to easily insert datetime in MySQL?
- How to prevent duplicate INSERT in MySQL?
- How to get the MySQL interactive output format in batch mode also?

Advertisements