gpt4 book ai didi

php - mysql 和 PHP 中的自动递增 ID

转载 作者:行者123 更新时间:2023-11-29 04:33:07 24 4
gpt4 key购买 nike

我在 MySQL 和 PHP 中遇到了自动递增 ID 的问题,这是场景,我创建了一个将数据插入数据库的表单。我使用一个表来查看我从 .php 文件中的表单存储的所有数据。我存储了5行数据,当我删除第3行时,我注意到第4行中的ID没有变为3。有没有办法设置Id来替换删除的行?

最佳答案

听起来您正在以不应该的方式使用该 ID。

如您所见here :

When you insert any other value into an AUTO_INCREMENT column, the column is set to that value and the sequence is reset so that the next automatically generated value follows sequentially from the largest column value. For example:

INSERT INTO animals (id,name) VALUES(100,'rabbit');
INSERT INTO animals (id,name) VALUES(NULL,'mouse');
SELECT * FROM animals;
+-----+-----------+
| id | name |
+-----+-----------+
| 1 | dog |
| 2 | cat |
| 3 | penguin |
| 4 | lax |
| 5 | whale |
| 6 | ostrich |
| 7 | groundhog |
| 8 | squirrel |
| 100 | rabbit |
| 101 | mouse |
+-----+-----------+

... 因此,如果是因为您担心“稍后”(删除第 3 行之后)某个值会获得一个 ID,但该 ID 不是最高的,那么您就不需要害怕那个。

如果这不能回答您的问题,请使用更多信息更新您的问题,说明为什么这是一个问题。

关于php - mysql 和 PHP 中的自动递增 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53517278/

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