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

人生之路不会是一帆风顺的,我们会遇上顺境,也会遇上逆境,在所有成功路上折磨你的,背后都隐藏着激励你奋发向上的动机,人生没有如果,只有后果与结果,成熟,就是用微笑来面对一切小事。

导读:本篇文章讲解 Mysql:设置主键自动增长起始值,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

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

方案1)使用alter table `tablename` AUTO_INCREMENT=10000

创建自增主键之后,使用alter table `tablename` AUTO_INCREMENT=10000实现修改表起始值。

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/124574.html

(0)

相关推荐

发表回复

登录后才能评论
半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!