IGNORE
IGNORE is an optional keyword used in SQL statements such as INSERT, UPDATE, and DELETE. It allows the execution of the operation to continue even if some errors occur, specifically those related to duplicate key violations. In essence, it tells MySQL to treat errors as warnings and go ahead with the command if possible.
Example
Output
Explanation
The INSERT IGNORE
statement prevents an error from occurring and skipping the row that would cause the error when inserting data into a table. In the provided example, if ‘9999’ already exists in the ‘emp_no’ column in the ‘employees’ table, the database engine would omit this row and carry on processing any subsequent rows. The output displays ‘Query OK, 0 rows affected’ meaning no row was inserted due to the IGNORE
clause.
Example
Output
If id 1 already exists, there will not be any output and no new entry will be recorded in the table.
Explanation
The INSERT OR IGNORE
command in SQL checks during the insertion of new data. If the data already exists, it will simply ignore and not insert the new data into the table. The use of IGNORE
prevents errors that occur due to duplications, or violations of unique constraints. This sample query attempts to insert record with id 1, name ‘John’, and dept_id 101 into the employees table. If a record with id 1 already exists, SQL ignores the insertion.