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

后端 未结 12 2504
孤街浪徒
孤街浪徒 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 05:11

    Potential danger of INSERT IGNORE. If you are trying to insert VARCHAR value longer then column was defined with - the value will be truncated and inserted EVEN IF strict mode is enabled.

    0 讨论(0)
  • 2020-11-21 05:12

    If using insert ignore having a SHOW WARNINGS; statement at the end of your query set will show a table with all the warnings, including which IDs were the duplicates.

    0 讨论(0)
  • 2020-11-21 05:14

    Replace Into seems like an option. Or you can check with

    IF NOT EXISTS(QUERY) Then INSERT
    

    This will insert or delete then insert. I tend to go for a IF NOT EXISTS check first.

    0 讨论(0)
  • 2020-11-21 05:17

    I routinely use INSERT IGNORE, and it sounds like exactly the kind of behavior you're looking for as well. As long as you know that rows which would cause index conflicts will not be inserted and you plan your program accordingly, it shouldn't cause any trouble.

    0 讨论(0)
  • 2020-11-21 05:21

    ON DUPLICATE KEY UPDATE is not really in the standard. It's about as standard as REPLACE is. See SQL MERGE.

    Essentially both commands are alternative-syntax versions of standard commands.

    0 讨论(0)
  • 2020-11-21 05:21

    Adding to this. If you use both INSERT IGNORE and ON DUPLICATE KEY UPDATE in the same statement, the update will still happen if the insert finds a duplicate key. In other words, the update takes precedence over the ignore. However, if the ON DUPLICATE KEY UPDATE clause itself causes a duplicate key error, that error will be ignored.

    This can happen if you have more than one unique key, or if your update attempts to violate a foreign key constraint.

    CREATE TABLE test 
     (id BIGINT (20) UNSIGNED AUTO_INCREMENT, 
      str VARCHAR(20), 
      PRIMARY KEY(id), 
      UNIQUE(str));
    
    INSERT INTO test (str) VALUES('A'),('B');
    
    /* duplicate key error caused not by the insert, 
    but by the update: */
    INSERT INTO test (str) VALUES('B') 
     ON DUPLICATE KEY UPDATE str='A'; 
    
    /* duplicate key error is suppressed */
    INSERT IGNORE INTO test (str) VALUES('B') 
     ON DUPLICATE KEY UPDATE str='A';
    0 讨论(0)
提交回复
热议问题