gpt4 book ai didi

mysql - REPLACE INTO,它是否重新使用 PRIMARY KEY?

转载 作者:IT老高 更新时间:2023-10-29 00:07:40 24 4
gpt4 key购买 nike

MySQL 中的REPLACE INTO 函数以删除和插入行的方式工作。在我的表中,主键 (id) 是自动递增的,所以我希望它先删除然后在数据库尾部插入一个带有 id 的表。

但是,它做了意想不到的事情,并用相同的 id 插入了它!这是预期的行为,还是我在这里遗漏了什么? (我在调用 REPLACE INTO 语句时没有设置 id)

最佳答案

如果您的表中有另一个您必须具有的 UNIQUE 索引,这是预期的行为,否则它会按照您的预期添加行。请参阅文档:

REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted. See Section 13.2.5, “INSERT Syntax”.

https://dev.mysql.com/doc/refman/5.5/en/replace.html

这确实也很有意义,因为 mySQL 还能如何找到要替换的行?它只能扫描整个表,这会很耗时。我创建了一个 SQL Fiddle 来演示这一点,请看一下 here

关于mysql - REPLACE INTO,它是否重新使用 PRIMARY KEY?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12205158/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com