mysql數據庫自增id重新從1排序的兩種方法


mysql默認自增ID是從1開始了,但當我們如果有插入表或使用delete刪除id之后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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM