mysql自增步長不是1,id亂了重新排序的解決辦法

1、mysql自增步長不是1的解決辦法

查看配置:
show variables like ‘%increment%’;
在這裏插入圖片描述
如果:
auto_increment_increment=2

執行:
set @@global.auto_increment_increment = 1;
set @@auto_increment_increment =1;

如果:
auto_increment_offset=2

執行:
set @@global.auto_increment_offset =1;
set @@auto_increment_offset =1;
在這裏插入圖片描述

2、mysql自增id亂了重新排序的解決辦法

表名: user

  1. 刪除原有主鍵:
alter table user drop id;
  1. 添加新主鍵字段並設置主鍵:
alter table user add id int(11) not null first;
alter table user modify column id int(11) not null auto_increment,add primary key(id);
  1. 查看錶:
select * from user;

ps:如果曾經的數據都不需要的話,可以直接清空所有數據,並將自增字段恢復從1開始計數

truncate table user;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章