Skip to content

Instantly share code, notes, and snippets.

@irazasyed
Last active February 1, 2023 19:23
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save irazasyed/4253470 to your computer and use it in GitHub Desktop.
Save irazasyed/4253470 to your computer and use it in GitHub Desktop.
MySQL: Reset id column to auto increment from 1
-- your_table: The table to modify
-- id: The id field/column to reset
SET @num := 0;
UPDATE your_table SET id = @num := (@num+1);
ALTER TABLE your_table AUTO_INCREMENT =1;
@JackG102
Copy link

JackG102 commented Feb 9, 2019

Thank you so much for this solution. I scoured the internet for 2-3 hours and all the solutions I found only included "ALTER TABLE your_table AUTO_INCREMENT =1", which didn't work for me. The first two lines actually made it work for my project to reset my ID column. I am still learning MySQL as I am creating my own CRUD app.

Based off your code, I now have a "button" that resets all the ID numbers.

@deimo72
Copy link

deimo72 commented Jan 28, 2020

Gracias, me funciono perfectamente!.

@vasalie
Copy link

vasalie commented Sep 20, 2020

thanks a lot.

@hellowonders
Copy link

Thanks a lot for you help syed !

@koiosml
Copy link

koiosml commented Nov 20, 2020

Good

@ArslanKAS
Copy link

Awesome work. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment