So I have a table where a column that was given an auto_increment value accidentally got started form 300 instead of 1,2,3,4……i’m a beginner and i do not know how to change it back to 1,2,3,4……screenshot of table
how to change the 307, 308 to 1,2,3,4…?
I tried to update the table but that did not work.
5
Answers
Alter table to drop the auto_increment, update, alter table to add the auto_increment
https://dbfiddle.uk/eBQh6cj8
Step-1) First take backup of your table data.
Step-2) Truncate the table by using the below SQL query.
Step-3) then again insert the into your table using backup data.
The DBCC CHECKIDENT management command is used to reset identity counter
many times we need to just reseed to next Id available
This will check the table and reset to the next ID.
You can get help from the link below:
Reset identity seed after deleting records in SQL Server
My mother says: the mountain that can be seen is not far away, don’t stop trying
With MySQL 8.0 you can use a window function to calculate the row numbers and then update the table:
Then make sure the next id won’t be a high value:
You can try to set the auto_increment to 1, MySQL will automatically advances that to the highest id value in the table, plus 1.
Be aware that this doesn’t guarantee subsequent rows will use consecutive values. You can get non-consecutive values if:
You insert greater values explicitly, overriding the auto-increment.
You roll back transactions. Id values generated by auto-increment are not recycled if you roll back.
You delete rows.
Occasionally InnoDB will skip a number anyway. It does not guarantee consecutive values — it only guarantees unique values. You should not rely on the auto-increment to be the same as a row number.
Here is a one approach to your problem.
Please take note of the following points before proceeding:
Step1: Generating dummy test table as per your test case.
Step2: Remove AUTO_INCREMENT for the column and set the Ids manually.
Step3: Enable AUTO_INCREMENT again for future record insertions.