There are 2 ways, but both are expensive to use in everyday life.
First I’ll use as an example your situation, you deleted id 4 in which case it would be something like this:
update tabela
set id = id - 1
where id > 4
Or, when it is a large amount and you don’t know exactly which ones were deleted, the "right" would be to save your data a temporary or auxiliary table.
Drop your table, and create it again with the id of 0. But I don’t know if it’s viable.
Or instead of dropping the entire table, simply delete the records and reset the auto-increment id like this:
ALTER TABLE 'tabela' AUTO_INCREMENT=0
That sounds like bad practice to me. Do you have any real reason or are you just curious?
– utluiz
Be careful with that. This practice can detonate your performance database. Imagine The person of id = 3 is related in 30 more tables. By changing the id from 3 to 2, all other relationships would break with this and an update to these other tables would also be required. This type of update has no need to happen.
– uaiHebert
uaiHebert, as I mentioned in the question I have no key connected to Id. The table is little modified and little accessed and I know that in most cases is not a good practice. Anyway thanks for the tips.
– Joao Paulo
I was going to ask the same thing, when I typed the title of the question I found your question. Thanks!
– HiHello