SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
The INSERT IGNORE
statement is specific to MySQL. It allows you to insert rows into a table, but if a row would cause a duplicate entry in a UNIQUE index or PRIMARY KEY, MySQL ignores the error and does not insert the offending row.
INSERT IGNORE INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
If the row would not result in a duplicate key, the INSERT IGNORE
statement adds the row to the table.
If the row would result in a duplicate key of a UNIQUE index or PRIMARY KEY, MySQL ignores the error and does not add the row to the table.
Let's assume we have a table called students
with a UNIQUE constraint on the column student_id
.
CREATE TABLE students ( student_id INT UNIQUE, name VARCHAR(255) );
Now, consider the following:
INSERT IGNORE INTO students (student_id, name) VALUES (1, 'John Doe'); INSERT IGNORE INTO students (student_id, name) VALUES (1, 'Jane Smith');
In the above scenario, the first INSERT IGNORE
statement will succeed since there is no student with student_id = 1
. However, the second INSERT IGNORE
statement will not produce an error even though there is already a student with student_id = 1
. Instead, it will simply ignore the second insert.
While INSERT IGNORE
is useful in cases where you want to avoid errors due to duplicate key entries, it can mask other potential issues because it suppresses errors. Use it judiciously.
If the table has multiple unique indexes, INSERT IGNORE
will ignore errors for all of them. This means if you have multiple potential duplicate key issues, they will all be ignored.
There's another statement, INSERT ... ON DUPLICATE KEY UPDATE
, which not only checks for duplicate key conflicts but also allows you to update the existing row in case of a conflict.
Always be aware of the implications when using features like INSERT IGNORE
to ensure that you're not inadvertently introducing data integrity issues or overlooking other problems.
INSERT IGNORE
is used to insert rows into a table while ignoring duplicate key errors.INSERT IGNORE INTO employees (employee_id, employee_name) VALUES (1, 'John Doe');
INSERT IGNORE
skips the conflicting row and continues with the next.INSERT IGNORE INTO employees (employee_id, employee_name) VALUES (1, 'Jane Doe');
INSERT IGNORE
prevents the insertion of duplicate records based on unique key constraints.CREATE TABLE users (user_id INT PRIMARY KEY, username VARCHAR(255) UNIQUE); INSERT IGNORE INTO users (user_id, username) VALUES (1, 'john_doe');
INSERT IGNORE
ignores errors, such as duplicate key violations, without generating an error message.INSERT IGNORE INTO products (product_id, product_name) VALUES (101, 'Widget');
INSERT IGNORE
works well with unique constraints, preventing the insertion of rows with duplicate unique key values.CREATE TABLE departments (department_id INT PRIMARY KEY, department_name VARCHAR(255) UNIQUE); INSERT IGNORE INTO departments (department_id, department_name) VALUES (1, 'HR');
INSERT IGNORE INTO sales (sale_id, amount) VALUES (101, 5000);
INSERT IGNORE
can be used to insert rows without considering foreign key constraints.INSERT IGNORE INTO orders (order_id, product_id) VALUES (1, 101);
INSERT IGNORE INTO customers (customer_id, customer_name) VALUES (1, 'John Doe');
REPLACE INTO customers (customer_id, customer_name) VALUES (1, 'Jane Doe');