How to Insert Multiple Rows in a MySQL Database

Avatar

By squashlabs, Last Updated: July 20, 2023

How to Insert Multiple Rows in a MySQL Database

Basic Syntax

To insert multiple rows into a MySQL table, you can use the INSERT INTO statement followed by the table name and the column names enclosed in parentheses. Each set of values for a row is enclosed in parentheses and separated by commas. Here’s an example:

INSERT INTO my_table (col1, col2, col3)
VALUES ('value1', 'value2', 'value3'),
       ('value4', 'value5', 'value6'),
       ('value7', 'value8', 'value9');

This statement inserts three rows into the “my_table” table, with values for columns “col1”, “col2”, and “col3”.

Related Article: Processing MySQL Queries in PHP: A Detailed Guide

Using SELECT Statement

Another way to insert multiple rows into a MySQL table is by using the SELECT statement. You can select data from another table or create a dummy dataset using the SELECT statement and then insert the selected data into the target table. Here’s an example:

INSERT INTO my_table (col1, col2, col3)
SELECT col1, col2, col3
FROM other_table
WHERE condition;

This statement inserts rows into “my_table” by selecting data from “other_table” based on a specified condition.

Use Cases for Inserting Multiple Rows in MySQL

There are several scenarios where inserting multiple rows in MySQL is useful. Some common use cases include:

Data Migration

When migrating data from one database to another, you may need to insert a large amount of data into the new database. Performing individual insert statements can be time-consuming and inefficient. By using multiple row insertion, you can significantly improve the performance of the migration process.

Related Article: How to Perform a Full Outer Join in MySQL

Batch Processing

In certain applications, you may need to process data in batches. For example, when processing user registrations or log entries, you can collect a batch of records and insert them into the database at once. This approach minimizes the number of database transactions and improves overall performance.

Best Practices for Inserting Multiple Rows in MySQL

When inserting multiple rows in MySQL, it’s important to follow some best practices to ensure efficient and reliable operations. Here are some tips to consider:

Use Prepared Statements

Prepared statements are a powerful feature of MySQL that allows you to optimize and reuse SQL statements. When inserting multiple rows, using prepared statements can improve performance by reducing the overhead of parsing and optimizing the SQL statement for each row.

Here’s an example of using prepared statements in PHP:

$stmt = $pdo->prepare("INSERT INTO my_table (col1, col2, col3) VALUES (?, ?, ?)");

foreach ($data as $row) {
    $stmt->execute([$row['col1'], $row['col2'], $row['col3']]);
}

Related Article: How to Fix MySQL Error Code 1175 in Safe Update Mode

Batch Size Optimization

When inserting a large number of rows, it’s important to optimize the batch size to strike a balance between memory usage and performance. Inserting too many rows in a single batch can consume excessive memory, while inserting too few rows can lead to suboptimal performance. Experiment with different batch sizes to find the optimal value for your specific use case.

Real World Examples of Inserting Multiple Rows in MySQL

Let’s explore some real-world examples of inserting multiple rows in MySQL.

Example 1: Inserting User Data

Suppose you have a CSV file containing user data that needs to be inserted into a MySQL table. You can use the LOAD DATA INFILE statement to efficiently load the data into the table. Here’s an example:

LOAD DATA INFILE '/path/to/data.csv'
INTO TABLE users
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

This statement loads the data from the CSV file into the “users” table, considering the specified field and line terminators. The IGNORE 1 ROWS clause skips the header row in the CSV file.

Related Article: How to Update Records in MySQL with a Select Query

Example 2: Bulk Insertion with Python

If you’re working with Python, you can leverage the MySQL Connector/Python library to perform bulk insertions. Here’s an example:

import mysql.connector

cnx = mysql.connector.connect(user='user', password='password',
                              host='localhost', database='my_database')
cursor = cnx.cursor()

add_data = ("INSERT INTO my_table (col1, col2, col3) "
            "VALUES (%s, %s, %s)")

data = [
    ('value1', 'value2', 'value3'),
    ('value4', 'value5', 'value6'),
    ('value7', 'value8', 'value9')
]

cursor.executemany(add_data, data)

cnx.commit()

cursor.close()
cnx.close()

In this example, we establish a connection to the MySQL database, define the INSERT statement with placeholders, and execute the statement using the executemany() method. Finally, we commit the changes and close the cursor and connection.

Performance Considerations for Inserting Multiple Rows in MySQL

When dealing with large datasets and performing multiple row insertions in MySQL, performance becomes a critical factor. Here are some considerations to optimize the performance of inserting multiple rows:

Disable Indexes

Indexes can significantly impact the performance of insert operations. If you’re inserting a large number of rows, consider disabling indexes before the insertion and re-enabling them afterward. This can greatly improve the performance of the insertion process.

ALTER TABLE my_table DISABLE KEYS;

-- Perform multiple row insertion here

ALTER TABLE my_table ENABLE KEYS;

Related Article: How to Use MySQL Query String Contains

Use Extended Insert Syntax

The extended insert syntax allows you to group multiple rows in a single insert statement, reducing the number of round trips to the database server. By default, MySQL uses the extended insert syntax when inserting multiple rows, but it’s worth confirming that your database configuration enables this feature.

Advanced Techniques for Inserting Multiple Rows in MySQL

In addition to the basic techniques we’ve covered so far, MySQL provides some advanced techniques for inserting multiple rows efficiently. Let’s explore a couple of these techniques:

INSERT INTO … ON DUPLICATE KEY UPDATE

If you need to insert multiple rows but update the existing rows if a duplicate key violation occurs, you can use the INSERT INTO … ON DUPLICATE KEY UPDATE statement. This statement allows you to specify the columns to update in case of a duplicate key violation.

Here’s an example:

INSERT INTO my_table (col1, col2, col3)
VALUES ('value1', 'value2', 'value3'),
       ('value4', 'value5', 'value6'),
       ('value7', 'value8', 'value9')
ON DUPLICATE KEY UPDATE col3 = VALUES(col3);

In this example, if a duplicate key violation occurs, the “col3” column will be updated with the new value specified in the VALUES clause.

Related Article: How to Resolve Secure File Priv in MySQL

INSERT IGNORE

The INSERT IGNORE statement allows you to insert multiple rows while ignoring any duplicate key violations. If a duplicate key violation occurs, the statement will silently skip the insertions and continue with the remaining rows.

Here’s an example:

INSERT IGNORE INTO my_table (col1, col2, col3)
VALUES ('value1', 'value2', 'value3'),
       ('value4', 'value5', 'value6'),
       ('value7', 'value8', 'value9');

In this example, if a duplicate key violation occurs, the row will be ignored, and the insertion will continue with the remaining rows.

Code Snippet Ideas for Inserting Multiple Rows in MySQL

Here are some code snippets that demonstrate different approaches to inserting multiple rows in MySQL:

Using PHP PDO

$data = [
    ['value1', 'value2', 'value3'],
    ['value4', 'value5', 'value6'],
    ['value7', 'value8', 'value9']
];

$sql = "INSERT INTO my_table (col1, col2, col3) VALUES ";

$placeholders = [];
$values = [];

foreach ($data as $row) {
    $placeholders[] = '(?, ?, ?)';
    $values = array_merge($values, $row);
}

$sql .= implode(', ', $placeholders);

$stmt = $pdo->prepare($sql);
$stmt->execute($values);

Related Article: Securing MySQL Access through Bash Scripts in Linux

Using Python MySQL Connector

import mysql.connector

cnx = mysql.connector.connect(user='user', password='password',
                              host='localhost', database='my_database')
cursor = cnx.cursor()

data = [
    ('value1', 'value2', 'value3'),
    ('value4', 'value5', 'value6'),
    ('value7', 'value8', 'value9')
]

stmt = "INSERT INTO my_table (col1, col2, col3) VALUES (%s, %s, %s)"

cursor.executemany(stmt, data)

cnx.commit()

cursor.close()
cnx.close()

Error Handling for Inserting Multiple Rows in MySQL

When inserting multiple rows in MySQL, it’s important to handle any potential errors that may occur during the insertion process. Here are some error handling techniques:

Check for Duplicate Key Violations

If you have unique constraints or primary keys defined on your table, it’s crucial to check for duplicate key violations before inserting the rows. You can use the IGNORE keyword or the ON DUPLICATE KEY UPDATE statement to handle these violations gracefully.

Related Article: Creating a Bash Script for a MySQL Database Backup

Use Transactions

Enclosing your multiple row insertion statements within a transaction can help ensure data integrity and provide a way to rollback the changes in case of errors. Transactions allow you to group multiple statements into a single logical unit, making it easier to manage errors and maintain data consistency.

Here’s an example of using transactions in MySQL:

START TRANSACTION;

-- Perform multiple row insertion here

COMMIT; -- or ROLLBACK in case of errors

Advanced Multiple Rows Insertion with MySQL

In addition to the techniques covered so far, MySQL provides some advanced features that can simplify and optimize multiple row insertion.

INSERT INTO … SELECT

The INSERT INTO … SELECT statement allows you to insert multiple rows into a table by selecting data from another table or subquery. This technique can be useful when you want to combine data from different sources or apply transformations before inserting the rows.

Here’s an example:

INSERT INTO my_table (col1, col2, col3)
SELECT col1, col2, col3
FROM other_table
WHERE condition;

In this example, the SELECT statement retrieves the desired data from “other_table” based on a specified condition, and the result is inserted into “my_table”.

Related Article: Converting MySQL Query Results from True to Yes

MySQL Features to Insert Multiple Rows

MySQL provides various features and options to simplify the process of inserting multiple rows. Some notable features include:

LOAD DATA INFILE

The LOAD DATA INFILE statement allows you to efficiently load data from a file into a MySQL table. This feature is particularly useful when dealing with large datasets or performing bulk insertions.

INSERT INTO … ON DUPLICATE KEY UPDATE

As mentioned earlier, the INSERT INTO … ON DUPLICATE KEY UPDATE statement allows you to handle duplicate key violations and update existing rows if necessary. This feature can simplify the logic and reduce the number of database queries required for multiple row insertion.

Related Article: How To Import a SQL File With a MySQL Command Line

Different Ways to Insert Multiple Rows with MySQL

In MySQL, there are multiple ways to insert multiple rows into a table, depending on your specific requirements and the tools or programming languages you are using. Some common methods include:

Using INSERT INTO … VALUES

The basic method of inserting multiple rows is by using the INSERT INTO … VALUES syntax, where you specify the values for each row explicitly.

Using INSERT INTO … SELECT

As mentioned earlier, you can use the INSERT INTO … SELECT statement to insert multiple rows by selecting data from another table or subquery.

Related Article: Tutorial on Database Sharding in MySQL

Using LOAD DATA INFILE

The LOAD DATA INFILE statement allows you to load data from a file into a MySQL table. This method is particularly useful when dealing with large datasets or performing bulk insertions.

Using programming language libraries and frameworks

If you’re using a programming language like PHP, Python, or Java, you can leverage the corresponding database libraries or frameworks to perform multiple row insertions. These libraries often provide convenient APIs and methods to simplify the insertion process.

Using Stored Procedures in MySQL

Stored procedures are a powerful feature in MySQL databases that allow you to execute predefined sets of SQL statements. This article provides a tutorial on how to use... read more

Using SQL Between for Date Ranges in MySQL and PostgreSQL

This article serves as a guide for using SQL BETWEEN in MySQL and PostgreSQL to work with date ranges. It covers various topics such as querying dates, best practices,... read more

Tutorial: Working with SQL Data Types in MySQL

Handling SQL data types in MySQL databases can be a complex task for software engineers. This tutorial provides a technical guide on working with SQL data types in... read more