Mysql设置主键自动增长起始值的方案总结

在MySQL 中,当主键定义为自增长后,这个主键的值就不再需要用户输入数据了,而由数据库系统根据定义自动赋值,下面这篇文章主要给大家介绍了关于Mysql设置主键自动增长起始值的相关资料,需要的朋友可以参考下

实现目标:mysql下将自增主键的值,从10000开始,即实现自增主键的种子为10000。

方案1)使用alter table `tablename` AUTO_INCREMENT=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

方案2)创建表时设置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 AUTO_INCREMENT 10000 DEFAULT CHARSET=utf8 ; insert into `trace_test`(`name`)values('name2'); select * from `trace_test`;

Result:

id     name
10000  name2

3)如果表已有数据,truncate 之后设置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 ; 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

4)如果表已有数据,delete from之后设置auto_increment=10000,可行。

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

总结

到此这篇关于Mysql设置主键自动增长起始值的文章就介绍到这了,更多相关Mysql主键自动增长起始值内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是Mysql设置主键自动增长起始值的方案总结的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 数据库