mysql数据库自增id重新从1排序的两种方法

使用mysql时,通常表中会有一个自增的id字段,但当我们想将表中的数据清空重新添加数据时,希望id重新从1开始计数,用以下两种方法均可:

通常的设置自增字段的方法:
创建表格时添加:


create table table1(id int auto_increment primary key,...)

创建表格后添加:


alter table table1 add id int auto_increment primary key 自增字段,一定要设置为primary key.


例子


alter table tablename drop column id;
alter table tablename add id mediumint(8) not null primary key auto_increment first;

方法二:

alter table tablename auto_increment=0

猜你喜欢

转载自www.cnblogs.com/HKROnline-SyncNavigator/p/11021008.html