Home  >  Q&A  >  body text

What is the functional difference between the update operation and the replacement operation when MySQL primary key conflicts (as shown in the figure)

曾经蜡笔没有小新曾经蜡笔没有小新2690 days ago847

reply all(2)I'll reply

  • 世界只因有你

    世界只因有你2017-06-07 09:27:03

    There is a difference between

    INSERT ... ON DUPLICATE KEY UPDATE and REPLACE. The former executes UPDATE after INSERT fails, while the latter is equivalent to DELETE and then INSERT. It turns out that the entire line disappears!

    Specifically, if your table has 3 columns:

    id name alias
    1  王五 王二麻子
    

    The effects of these two sentences are different:

    INSERT INTO pri (id, name) VALUES (1, '李四') ON DUPLICATE KEY UPDATE name = '李四'
    
    REPLACE INTO pri (id, name) VALUES (1, '李四')
    

    The former alias remains unchanged, but the latter will become null.

    reply
    0
  • 迷茫

    迷茫2017-06-07 09:27:03

    insert into on duplicate update is to operate on the original record
    replace determines if there is a duplicate, delete it first, and then insert it

    reply
    0
  • Cancelreply