Computer >> Computer tutorials >  >> Programming >> MySQL

What is the use of MySQL IGNORE INSERT statement?


Basically, IGNORE INSERT statement is used to prevent the insertion of duplicate data into MySQL table. If we will use the INSERT IGNORE command rather than the INSERT command then if a record doesn't duplicate an existing record, MySQL inserts it as usual but if the record is a duplicate, then the IGNORE keyword tells MySQL to discard it silently without generating an error. Its syntax is as follows −

Syntax

INSERT INGORE INTO table_name(…)

Here, table_name is the name of the table in which we want to insert the values.

Example

The following example does not error out and at the same time, it will not insert duplicate records as well.

mysql> INSERT IGNORE INTO person_tbl (last_name, first_name)
    -> VALUES( 'Jay', 'Thomas');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT IGNORE INTO person_tbl (last_name, first_name)
    -> VALUES( 'Jay', 'Thomas');
Query OK, 0 rows affected (0.00 sec)