在變為自增前,為了保證數據的安全性我們先將表中的數據備份一份,備份是通過mysql命令進行備份,如果已經安裝mysql可視化工具navicat也可右鍵完成備份
1、備份表結構
create table table_bak like table_name; # table_bak備份的表名字可以自己定義,table_name是你要備份的表名
2、備份表數據
insert into table_bak select * from table_name;
3、刪除原來主鍵字段(如id)
alter table table_name drop id;
4、添加主鍵,自增,放在第一位
alter table table_name add id int(11) primary key auto_increment first;
5、檢查沒問題的話,備份的表可以刪了
delete from table_bak;