mysql數(shù)據(jù)庫自增id重新從1排序的兩種方法
mysql默認(rèn)自增ID是從1開始了,但當(dāng)我們?nèi)绻胁迦氡砘蚴褂胐elete刪除id之后ID就會(huì)不會(huì)從1開?了.
使用mysql時(shí),通常表中會(huì)有一個(gè)自增的id字段,但當(dāng)我們想將表中的數(shù)據(jù)清空重新添加數(shù)據(jù)時(shí),希望id重新從1開始計(jì)數(shù),用以下兩種方法均可.
通常的設(shè)置自增字段的方法,創(chuàng)建表格時(shí)添加:
create table table1(id int auto_increment primary key,...)
創(chuàng)建表格后添加:
alter table table1 add id int auto_increment primary key 自增字段,一定要設(shè)置為primary key.
例子,代碼如下:
alter table tablename drop column id;
alter table tablename add id mediumint(8) not null primary key auto_increment first;//phpfensi.com
方法二:alter table tablename auto_increment=0