Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL regenerate ID to keep autoincrement

Tags:

sql

mysql

I'm looking for pure SQL (or MySQL) syntax and I don't know if it's possible else I will create an php script but ...

I have a basic table with one primary key : ID and somes columns. I have no dependance with others tables.

At the moment my rows look like :

ID   Column1   Column2  
22   test      test
26   test2     test2
33   test3     test3
...

Now I want to regenerate all my ID to keep order. Example: I set start at 22

ID   Column1   Column2  
22   test      test
23   test2     test2
24   test3     test3
...
like image 548
Kakawait Avatar asked Oct 18 '25 19:10

Kakawait


1 Answers

Go and look there : Reorder / reset auto increment primary key

But as said there it will ruined the relationship you already have. But since you don't it's ok

ALTER TABLE `table` DROP `id`;
ALTER TABLE `table` AUTO_INCREMENT = 1;
ALTER TABLE `table` ADD `id` int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;
like image 153
yokoloko Avatar answered Oct 20 '25 09:10

yokoloko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!