简体   繁体   English

mysql和PHP中的自动增量ID

[英]Auto Increment ID in mysql and PHP

I'm facing a problem with the auto-increment ID in MySQL and PHP, here's the scene, I created a form that inserts the data to my database. 我在MySQL和PHP中遇到自动递增ID的问题,这是现场情况,我创建了一个将数据插入数据库的表单。 I use a table to see all the data I stored from the form in the .php file. 我使用表格查看从表单存储在.php文件中的所有数据。 I stored 5 rows of data, when I delete the 3rd row, I noticed that the ID in the 4th row doesn't change to 3. Is there a way to set the Id to replace the deleted row? 我存储了5行数据,删除第3行时,我注意到第4行中的ID不会更改为3。有没有办法设置ID来替换已删除的行?

It sounds like that you're using that ID in a way that you shouldn't. 听起来您正在以不应该使用的方式使用该ID。

As you can read 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. 当您将其他任何值插入到AUTO_INCREMENT列中时,该列将设置为该值并重置顺序,以便下一个自动生成的值从最大列的值开始依次出现。 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     |
+-----+-----------+

... So if it was because you are afraid that some value 'later on' (after the deletion of your 3rd row), will get an ID, that isn't the highest one, then you don't need to fear that. ...因此,如果是因为您担心某个“后续”值(删除第三行之后)会获得一个ID(不是最高的ID),那么您就不必担心。

Please update your question with further information on, why this is a problem, if this doesn't answer your question. 请更新您的问题,并提供更多有关以下信息的信息:如果这不能解决您的问题,为什么会出现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM