实现目标:mysql下将自增主键的值,从10000开始,即实现自增主键的种子为10000。
创建自增主键之后,使用alter table `tablename` AUTO_INCREMENT=10000实现修改表起始值。
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:
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:
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:
id name 10000 name
Result2:
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:
id name 10000 name
Result2:
id name 10000 name2