How to update id set from 1?

前端 未结 4 1345
清歌不尽
清歌不尽 2021-01-03 03:46

I have an id i.e primary key and auto increment. Is there any query to update my existing id and make my id start from 1 and next id 2

4条回答
  •  耶瑟儿~
    2021-01-03 04:15

    Is there any query to update my existing id and make my id start from 1 and next id 2 and so on

    What you can do is transfer the content of your table to another table. Reset the auto increment counter, insert your data back into the original table but let MySQL assign the primary key.

    Assuming your table name is mytable You do it like this:

    CREATE TABLE mytable_tmp select * from mytable;
    TRUNCATE TABLE mytable;
    ALTER TABLE mytable AUTO_INCREMENT = 1;
    INSERT INTO mytable(name) SELECT name FROM mytable_tmp ORDER BY id;
    DROP TABLE mytable_tmp;
    

提交回复
热议问题