Home » MySQL INSERT IGNORE

MySQL INSERT IGNORE

by Online Tutorials Library

MySQL INSERT IGNORE

Insert Ignore statement in MySQL has a special feature that ignores the invalid rows whenever we are inserting single or multiple rows into a table. We can understand it with the following explanation, where a table contains a primary key column.

The primary key column cannot stores duplicate values into a table. For example, student_roll_number should always be unique for every student. Similarly, the employee_id in the company should always be distinct into the employee table. When we try to insert a duplicate record into a table with a primary key column, it produces an error message. However, if we use the INSERT IGNORE statement to add duplicate rows into a table with a primary key column, MySQL does not produce any error. This statement is preferred when we are trying to insert records in bulk, and resulting errors can interrupt the execution process. As a result, it does not store any record into a table. In such a case, the INSERT IGNORE statement only generates the warnings.

Below are the cases where an INSERT IGNORE statement avoids error:

  • When we will try to insert a duplicate key where the column of a table has a PRIMARY or UNIQUE KEY constraint.
  • When we will try to add a NULL value where the column of a table has a NOT NULL constraint.
  • When we will try to insert a record to a partitioned table where the entered values do not match the format of listed partitions.

Syntax

The following are a syntax to use the INSERT IGNORE statement in MySQL:

MySQL INSERT IGNORE Example

Let us understand how INSERT IGNORE statement works in MySQL with the help of an example. First, we need to create a table named “Student” using the following statement:

The UNIQUE constraint ensures that we cannot insert duplicate values into the email column. Next, it is required to insert the records into the table. We can execute the below statement to add data into a table:

Finally, execute the SELECT statement to verify the insert operation:

We can see the below output where we have three rows into the table:

MySQL INSERT IGNORE

Let us execute the below statement that will try to add two records into the table:

It will produce an error: ERROR 1062 (23000): Duplicate entry ‘[email protected]‘ for key ‘student.Email’ because of the email violets the UNIQUE constraint.

Now, let us see what happened if we use the INSERT IGNORE statement into the above query:

MySQL will produce a message: one row added, and the other row was ignored.

We can see the detailed warning using the SHOW WARNINGS command:

MySQL INSERT IGNORE

Thus, we can say that if we use the INSERT IGNORE statement, MySQL gives a warning instead of issuing an error.

MySQL INSERT IGNORE and STRICT mode

In MySQL, STRICT MODE handles invalid or missing values that are going to be added into a table using INSERT OR UPDATE statement. If the strict mode is ON, and we are trying to add invalid values into a table using the INSERT statement, the statement is aborted, and we will get an error message.

However, if we use the INSERT IGNORE command, MySQL produces a warning message instead of throwing an error. Also, this statement tries to truncate values to make them valid before inserting it into the table.

Let us understand it with the help of an example. First, we are going to create a table named “Test” using the below statement:

In the above table, the name column only accepts the string whose length is less than or equal to five characters. Now, execute the below statement to insert the records into a table.

We can see that the specified name validates the name column constraint so it will be added successfully. Execute the SELECT statement to verify the result. It will give the output as below:

MySQL INSERT IGNORE

Next, insert the name whose length is greater than five:

MySQL does not add values and gives an error message because the strict mode is ON. However, if we use the INSERT IGNORE statement to insert the same string, it will give the warning message instead of throwing an error.

Finally, we have executed the SHOW WARNINGS command to check the warning message. The below output explains it more clearly that shows MySQL tries to truncate data before inserting it into a table.

MySQL INSERT IGNORE


You may also like