简体   繁体   中英

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. I use a table to see all the data I stored from the form in the .php file. 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?

It sounds like that you're using that ID in a way that you shouldn't.

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. 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.

Please update your question with further information on, why this is a problem, if this doesn't answer your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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