What are practical differences between `REPLACE` and `INSERT … ON DUPLICATE KEY UPDATE` in MySQL?

前端 未结 8 1476
清酒与你
清酒与你 2020-11-28 06:59

What I need is to set the values of all the fields of a record with a particular key (the key is composite actually), inserting the record if there is no record with such a

8条回答
  •  野性不改
    2020-11-28 07:15

    In what particular cases can REPLACE be preferred over INSERT ... ON DUPLICATE KEY UPDATE and vice versa?

    I've just found out the hard way that in the case of tables with a FEDERATED storage engine INSERT...ON DUPLICATE KEY UPDATE statements are accepted, but fail (with an Error 1022: Can't write; duplicate key in table...) if a duplicate-key violation occurs - see corresponding bullet point on this page of the MySQL Reference Manual.

    Fortunately, I was able to use REPLACE instead of INSERT...ON DUPLICATE KEY UPDATE within my after insert trigger to achieve the desired outcome of replicating changes to a FEDERATED table.

提交回复
热议问题