MySQL INSERT IGNORE Statement

Summary: in this tutorial, you will learn how to use the MySQL INSERT IGNORE statement to insert data into a table.

Introduction to MySQL INSERT IGNORE statement

When you use the INSERT statement to add multiple rows to a table and if an error occurs during the processing, MySQL terminates the statement and returns an error. Consequently, the table remains unchanged with no inserted rows.

The INSERT IGNORE statement allows you to disregard rows containing invalid data that would otherwise trigger an error and insert only rows that contain valid data.

Here’s the basic syntax of the INSERT IGNORE statement:

INSERT IGNORE INTO table(column_list)
VALUES(value_list),
      (value_list),
      ...;Code language: SQL (Structured Query Language) (sql)

Note that the IGNORE option is an extension of MySQL to the SQL standard.

MySQL INSERT IGNORE example

We will create a new table called subscribers for the demonstration.

CREATE TABLE subscribers (
    id INT PRIMARY KEY AUTO_INCREMENT,
    email VARCHAR(130) NOT NULL UNIQUE
);Code language: SQL (Structured Query Language) (sql)

The UNIQUE constraint ensures that no duplicate email exists in the email column.

The following statement inserts a new row into the  subscribers table:

INSERT INTO subscribers(email)
VALUES('[email protected]');Code language: SQL (Structured Query Language) (sql)

It worked as expected.

Let’s execute another statement that inserts two rows into the  subscribers table:

INSERT INTO subscribers(email)
VALUES('[email protected]'), 
      ('[email protected]');Code language: SQL (Structured Query Language) (sql)

It returns an error.

Error Code: 1062. Duplicate entry '[email protected]' for key 'email'Code language: SQL (Structured Query Language) (sql)

As indicated in the error message, the email [email protected] violates the UNIQUE constraint.

However, if you use the INSERT IGNORE statement instead.

INSERT IGNORE INTO subscribers(email)
VALUES('[email protected]'), 
      ('[email protected]');Code language: SQL (Structured Query Language) (sql)

MySQL returned a message indicating that one row was inserted and the other row was ignored.

1 row(s) affected, 1 warning(s): 1062 Duplicate entry '[email protected]' for key 'email' Records: 2  Duplicates: 1  Warnings: 1Code language: SQL (Structured Query Language) (sql)

To find the detail of the warning, you can use the SHOW WARNINGS command as shown below:

SHOW WARNINGS;Code language: SQL (Structured Query Language) (sql)
MySQL INSERT IGNORE - warning

In conclusion, when you use the INSERT IGNORE statement, instead of issuing an error, MySQL issues a warning in case an error occurs.

If you query data from subscribers table, you will find that only one row was inserted and the row that caused the error was not.

MySQL INSERT IGNORE - subscribers table

MySQL INSERT IGNORE and STRICT mode

When the strict mode is on, MySQL returns an error and aborts the INSERT statement if you try to insert invalid values into a table.

However, if you use the INSERT IGNORE statement, MySQL will issue a warning instead of an error. In addition, it will try to adjust the values to make them valid before adding the value to the table.

Consider the following example.

First, we create a new table named tokens:

CREATE TABLE tokens (
    s VARCHAR(6)
);Code language: SQL (Structured Query Language) (sql)

In this table, the column s accepts only strings whose lengths are less than or equal to six.

Second, insert a string whose length is seven into the tokens table.

INSERT INTO tokens VALUES('abcdefg');Code language: SQL (Structured Query Language) (sql)

MySQL issued the following error because the strict mode is on.

Error Code: 1406. Data too long for column 's' at row 1Code language: SQL (Structured Query Language) (sql)

Third, use the INSERT IGNORE statement to insert the same string.

INSERT IGNORE INTO tokens VALUES('abcdefg');Code language: SQL (Structured Query Language) (sql)

MySQL truncated data before inserting it into the tokens table. In addition, it issues a warning.

MySQL INSERT IGNORE - strict mode

Summary

  • Use the MySQL INSERT IGNORE statement to insert rows into a table and ignore errors for rows that cause errors.
Was this tutorial helpful?