mysql重置自增長屬性

kngnng發表於2013-04-09
重置mysql自增長欄位

我們知道mysql沒有類似oracle的sequence特性,設定的自增長需要使用auto_increment,這個屬性是基於會話的,每個會話連線庫後跟新表都會使用自己會話的auto_increment_id,當我們需要重置自增長ID,使其從1開始計數,我們需要如下操作。

環境描述:
表test(id int,name varchar2(10))

1、查詢表的狀態,包括自增長屬性
SELECT * FROM information_schema.tables WHERE table_name='test';
查詢後auto_increment標識為1
2、插入幾條資料
insert into test(name) values('a');
insert into test(name) values('b');
insert into test(name) values('c');
insert into test(name) values('d');
3、查詢表狀態
SELECT * FROM information_schema.tables WHERE table_name='test';
查詢後auto_increment標識為5,再插入新的資料id就會是5
4、這時我們清空表的資料,從新連線會話都會仍然記錄5
5、重置auto_increment
ALTER TABLE test AUTO_INCREMENT=1;
前提是清空表後再執行
在查詢表的狀態
auto_increment標識為1

這時才將屬性置回1

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26252014/viewspace-758051/,如需轉載,請註明出處,否則將追究法律責任。

相關文章