The Replace
performs two actions, first it performs deletion after insertion, this may cause some problems like:
- If you have a foreign key restriction pointing to that line -
Replace
will fail.
- If your external key is configured for cascading deletion, the
Replace
will cause the rows of other tables to be deleted
- The uninformed fields will have lost information, precisely because it deletes the row if it exists and inserts another at the end of the table only with the fields and data passed in the query.
Already using the INSERT
... ON DUPLICATE KEY UPDATE
this problem does not occur, it is therefore recommended that you choose.
Another problem of
REPLACE INTO
is that the uninformed fields will have lost information, precisely because it deletes the row if it exists and inserts another at the end of the table only with the fields and data passed in thequery
.– Roberto de Campos
good observation added the answer, thanks. @Robertofagundes
– Luiz Santos