码迷,mamicode.com
首页 > 数据库 > 详细

Mysql:设置主键自动增长起始值

时间:2019-10-15 00:04:42      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:UNC   null   class   同事   result   创建表   exists   rac   自动增长   

比较郁闷昨天在家使用‘alter table `tablename` AUTO_INCREMENT=10000;’怎么也不起效,但是今天下班时间公司一同事尝试了一下就可以了。搞不明白自己当时是怎么操作的,导致最终不起效。

实现目标: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:设置主键自动增长起始值

标签:UNC   null   class   同事   result   创建表   exists   rac   自动增长   

原文地址:https://www.cnblogs.com/yy3b2007com/p/11674844.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!