Skip to content

Instantly share code, notes, and snippets.

@anasabbasi
Created January 20, 2016 09:00
Show Gist options
  • Save anasabbasi/a1a03e76e546fda7bf62 to your computer and use it in GitHub Desktop.
Save anasabbasi/a1a03e76e546fda7bf62 to your computer and use it in GitHub Desktop.
Reindex a MySQL table's primary key field. Helpful, if rows have been deleted from the middle and you have an OCD, like me.
SET @newid=0;
UPDATE tablename SET primary_key_id=(@newid:=@newid+1) ORDER BY primary_key_id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment