3

So I have a MySQL database that I'm using with a PHP site. Over the course of weeks of development, I'm left with something like:

Table: users

id  | name
-----------
  12| Bob
  34| Jen
 155| John
 154| Kyle

Except this goes on for hundreds of records and the ids are in the thousands.

I'm looking for a script I can run to re-name the keys to their lowest value (preserving their respective rows), and then reset the AUTO_INCREMENT to the next id

The desired output would be:

Table: users

id  | name
-----------
   1| Bob
   2| Jen
   3| Kyle
   4| John

And ALTER TABLE users AUTO_INCREMENT = 5;

(Notice Kyle and John)

I realize I would have to fix anything referencing the users.id.

Does anyone know a way to do this in MySQL or with a PHP script?

1
  • 1
    I did this earlier by removing droping the ID column and creating it again, I'm not sure how this will affect the ordering however. Edit: I suggest you perform a backup before trying anything like this however.
    – Eddie
    Jul 8, 2011 at 19:28

2 Answers 2

6

remove index on id

do something like this:

SET @rank:=0;
update users
set id=@rank:=@rank+1
order by id;

add index on id

2
  • This worked but it didn't fix the default order. It still lists as 1,2,4,3 i.imgur.com/YB6oE.png (stop deleting your comments and making me looks silly =[) Jul 8, 2011 at 19:59
  • @imoda If you do a simple query like select * from t, you don't have to expect any order. The order of table rows is undefined. Sometimes you can get different results when running the same query multiple times.
    – Karolis
    Jul 8, 2011 at 20:36
1

Why do you need to change the order of the names in the database? You can just add an ORDER BY name clause on your sql query to get the results in alphabetical order by name.

4
  • I don't want to sort them alphabetically by name. I want them to sort by id by default, that way I don't have to tag on ORDER BY parameters site wide for my queries. Jul 8, 2011 at 20:10
  • 3
    Try alter table yourtable order by id, though if you're using InnoDB, this will most likely have no effect.
    – Marc B
    Jul 8, 2011 at 20:24
  • I had a moment where I thought that perhaps REPLACE...SELECT would work, but the documentation says that you cannot reference the same table name as both the REPLACE (actually in the insert documentation) target and the SELECT target. I think your best bet may be to use an PHP script to create temporary copy of your Users table, then use REPLACE...SELECT with an ORDER BY, and finish by dropping your temporary table. This is all, of course, assuming that the statement recommended by @Marc B didn't work. Jul 9, 2011 at 1:11
  • Another thought is to create a function that will return the results of the Users in alphabetical order using ORDER BY, then you only have to write the ORDER BY clause once, plus it may DRY up your code ^.^ Jul 9, 2011 at 1:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.