比較鬱悶昨天在家使用‘alter table `tablename` AUTO_INCREMENT=10000;’怎麼也不起效,可是今天下班時間公司一同事嘗試了一下就能夠了。搞不明白本身當時是怎麼操做的,致使最終不起效。mysql
實現目標:mysql下將自增主鍵的值,從10000開始,即實現自增主鍵的種子爲10000。sql
建立自增主鍵以後,使用alter table `tablename` AUTO_INCREMENT=10000實現修改表起始值。spa
drop table if exists `trace_test`; CREATE TABLE `trace_test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ; alter table `trace_test` AUTO_INCREMENT=10000; insert into `trace_test`(`name`)values('name2'); select * from `trace_test`;
Result:code
id name 10000 name2
drop table if exists `trace_test`; CREATE TABLE `trace_test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT 10000 DEFAULT CHARSET=utf8 ; insert into `trace_test`(`name`)values('name2'); select * from `trace_test`;
Result:blog
id name 10000 name2
drop table if exists `trace_test`; CREATE TABLE `trace_test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ; insert into `trace_test`(`name`)values('name1'); select * from `trace_test`; truncate table `trace_test`; alter table `trace_test` AUTO_INCREMENT=10000; insert into `trace_test`(`name`)values('name2'); select * from `trace_test`;
Result1:rem
id name 10000 name
Result2:table
id name 10000 name2
drop table if exists trace_test; CREATE TABLE trace_test ( id int(20) NOT NULL AUTO_INCREMENT, name varchar(255) DEFAULT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ; insert into trace_test(name)values('name1'); select * from trace_test; delete from `trace_test`; alter table trace_test AUTO_INCREMENT=10000; insert into trace_test(name)values('name2'); select * from trace_test;
Result1:class
id name
10000 name
Result2:test
id name
10000 name2