“INSERT IGNORE” vs “INSERT … ON DUPLICATE KEY UPDATE”

后端 未结 12 2527
孤街浪徒
孤街浪徒 2020-11-21 04:30

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

12条回答
  •  忘掉有多难
    2020-11-21 04:59

    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.

提交回复
热议问题