Query to Re-index Primary Key of MySQL Database

前端 未结 3 1161
没有蜡笔的小新
没有蜡笔的小新 2020-12-23 21:44

I\'ve got a table in MySQL that has a Primary Key Column.

Lets say:

ID | Value
1  | One
2  | Two
6  | Three
8  | Four
9  | Five

How

相关标签:
3条回答
  • 2020-12-23 22:08

    There is even a simple way to accomplish the result by writing this query

    SET @newid=0;
    UPDATE tablename SET primary_key_id=(@newid:=@newid+1) ORDER BY primary_key_id;
    

    This query will reindex the primary key starts from 1

    0 讨论(0)
  • 2020-12-23 22:15

    I did this in phpmyadmin by unchecking the A_I box (Auto Increment setting), clicking save, and then checking it again, and clicking save again.

    0 讨论(0)
  • 2020-12-23 22:25

    Seems to me you have two options.

    1) create a new table and copy the existing data over.

    2) add another autoincrement field to the existing table, then delete the original column.

    ALTER TABLE tableName ADD NewIdn INT NOT NULL AUTO_INCREMENT KEY
    
    0 讨论(0)
提交回复
热议问题