替换成与更新

替换成与更新

问题描述:

我最近发现了 MYSQL 函数 REPLACE INTO,因此与 INSERT INTOUPDATE 相比,它更胜一筹.我在其他使用的函数上使用 REPLACE INTO 吗?什么时候可以用replace into over Update into

I recently found the MYSQL function REPLACE INTO, so it got the ball rolling in comparison to INSERT INTO, UPDATE.. When shall I use REPLACE INTO over the other used functions? When is it acceptable to use replace into over Update into

我当前更新的标准程序是:

Standard procedure for my current update would be:

  1. 检查用户输入退出
  2. 查询数据库中的行是否存在
  3. 更新行

我的插入的标准程序是

  1. 检查用户输入是否存在
  2. 检查行是否存在
  3. 如果没有,则插入
  4. 如果不是,则返回错误

所以,总的来说.REPLACE INTOUPDATE.什么时候可以接受,利弊?

So, overall. REPLACE INTO vs UPDATE. When acceptable, pros and cons?

Replace 旨在缓解以下情况:

Replace was designed to ease the following case:

  1. 检查是否存在具有相同PK的记录
  2. 如果是,删除该行并为给定的记录插入一个新行
  3. 如果没有,插入一条新记录

根据文档,REPLACE 等同于 INSERT,除了删除之前删除了 PK 的任何现有记录.

As per documentation REPLACE is equivalent to INSERT, apart from deleting any existing record having the PK is beeing deleted before.