MySQL 8: INSERT IGNORE statement – Explained with examples

Updated: January 27, 2024 By: Guest Contributor Post a comment

Introduction

MySQL, one of the world’s most popular open-source relational database management systems, offers many commands for managing data effectively. One of the essential commands for data manipulation is the INSERT statement. However, handling unique constraint violations elegantly can be challenging. This is where the INSERT IGNORE statement comes into play. In this tutorial, we will take a deep dive into the INSERT IGNORE statement in MySQL 8, complete with several illustrative examples. By the end of this tutorial, you will possess a firm understanding of how to utilize INSERT IGNORE and why it is a valuable tool in preventing errors and streamlining database operations.

Understanding the INSERT IGNORE Statement

The syntax for the INSERT IGNORE command is very similar to a regular INSERT, with the only addition being the IGNORE keyword:

INSERT IGNORE INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);

When you execute an INSERT IGNORE statement, MySQL tries to insert the new row into your table. If a row would cause a duplicate entry for a PRIMARY KEY or UNIQUE index, MySQL ignores it instead of producing an error. This behavior can be particularly valuable when performing bulk inserts where some rows might duplicate existing entries, and we prefer to keep the operation running smoothly rather than halting on errors.

Basic Examples of INSERT IGNORE

Let’s start with a simple example. Assume you have a table named ‘products’ with the following structure:

CREATE TABLE products (
    id INT AUTO_INCREMENT PRIMARY KEY,
    product_code VARCHAR(255) NOT NULL UNIQUE,
    name VARCHAR(255) NOT NULL,
    price DECIMAL(10, 2) NOT NULL
);

To illustrate the use of INSERT IGNORE, let’s try inserting a product with a duplicate product_code — which is not allowed since product_code is UNIQUE.

INSERT INTO products (product_code, name, price) VALUES
('P123', 'Widget A', 9.99),
('P124', 'Widget B', 12.99);

The above statements successfully insert two new products into our products table. Now, let’s see what happens when we try to insert another product using the same product_code ‘P123’:

INSERT IGNORE INTO products (product_code, name, price) VALUES
('P123', 'Widget C', 11.99);

In this case, since ‘P123’ is a duplicate product_code, the database will ignore this row and no error will be thrown. You won’t see ‘Widget C’ in the products list and no new row will be added, but the application won’t crash due to a duplicate key constraint violation.

Advanced use of INSERT IGNORE

Moving to more advanced scenarios, you can combine INSERT IGNORE with other SQL techniques to perform more complicated operations. Let’s say you want to insert multiple rows and ignore the duplicates.

INSERT IGNORE INTO products (product_code, name, price) VALUES
('P123', 'Widget A', 9.99),
('P125', 'Widget D', 7.99),
('P126', 'Widget E', 4.99);

In this example, the first row is a duplicate and will be ignored, but the other two new products will be inserted successfully.

It is also possible to use INSERT IGNORE with Select statements. Suppose you have another table containing a list of products to be inserted:

CREATE TABLE new_products (
    product_code VARCHAR(255) NOT NULL,
    name VARCHAR(255) NOT NULL,
    price DECIMAL(10, 2) NOT NULL
);

INSERT INTO new_products (product_code, name, price) VALUES
('P127', 'Widget F', 12.99),
('P123', 'Duplicate Widget', 9.99); 

You can insert these products into the products table while ignoring duplicates like this:

INSERT IGNORE INTO products (product_code, name, price)
SELECT product_code, name, price FROM new_products;

This statement will only add ‘Widget F’ to the products table and ignore the duplicate entry for product code ‘P123’.

Impact on Auto-Increment

An important aspect to consider with INSERT IGNORE is its impact on auto-increment columns. If an ignored insert has an AUTO_INCREMENT value, that value is not wasted. On the contrary, with a regular INSERT that results in an error, the auto-increment value is incremented and lost even though the row isn’t inserted. This difference can help conserve the numerical space in your auto-increment columns.

Caveats and Considerations

While INSERT IGNORE is useful, it must be used cautiously as it can potentially mask other unintended errors such as data truncation. If data doesn’t fit into a field as per its definition, instead of throwing an error, MySQL will adjust the value to fit, often resulting in data loss with only a warning. It’s vital to ensure your data is clean and accurate before using INSERT IGNORE to avoid silent issues later on.

Conclusion

To sum up, INSERT IGNORE can be a lifesaver in many bulk insert operations, preventing the process from stopping due to duplicate key entries. Yet, it requires a thorough understanding and mindful application to ensure data integrity is not compromised. With the examples provided, you should now be able to confidently use INSERT IGNORE in your MySQL 8 databases.