While executing an INSERT
statement with many rows, I want to skip duplicate entries that would otherwise cause failure. After some research, my options appear
Something important to add: When using INSERT IGNORE and you do have key violations, MySQL does NOT raise a warning!
If you try for instance to insert 100 records at a time, with one faulty one, you would get in interactive mode:
Query OK, 99 rows affected (0.04 sec)
Records: 100 Duplicates: 1 Warnings: 0
As you see: No Warnings! This behavior is even wrongly described in the official Mysql Documentation.
If your script needs to be informed, if some records have not been added (due to key violations) you have to call mysql_info() and parse it for the "Duplicates" value.