替换成与更新
我最近发现了 MYSQL 函数 REPLACE INTO
,因此与 INSERT INTO
、UPDATE
相比,它更胜一筹.我在其他使用的函数上使用 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:
- 检查用户输入退出
- 查询数据库中的行是否存在
- 更新行
我的插入的标准程序是
- 检查用户输入是否存在
- 检查行是否存在
- 如果没有,则插入
- 如果不是,则返回错误
所以,总的来说.REPLACE INTO
与 UPDATE
.什么时候可以接受,利弊?
So, overall. REPLACE INTO
vs UPDATE
. When acceptable, pros and cons?
Replace 旨在缓解以下情况:
Replace was designed to ease the following case:
- 检查是否存在具有相同PK的记录
- 如果是,删除该行并为给定的记录插入一个新行
- 如果没有,插入一条新记录
根据文档,REPLACE 等同于 INSERT,除了删除之前删除了 PK 的任何现有记录.
As per documentation REPLACE is equivalent to INSERT, apart from deleting any existing record having the PK is beeing deleted before.