How to update id set from 1?

前端 未结 4 1300
清歌不尽
清歌不尽 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:14

    Of course there is a way:

    set @counter = 0;
    update table_name
    set id  = (@counter := @counter + 1);
    

    EDIT

    To avoid problem with duplicate keys you can run something like this before to temporary change current ids to negative equivalents:

    update table_name
    set id  = 0 - id;
    

提交回复
热议问题