当前位置:网站首页>Mysql:设置主键自动增长起始值
Mysql:设置主键自动增长起始值
2022-08-11 04:00:00 【菜鸟是大神】
实现目标: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
边栏推荐
猜你喜欢

Get Qt installation information: including installation directory and various macro addresses

机器学习中什么是集成学习?
![[C Language] Getting Started](/img/5e/484e3d426a6f1cc0d792a9ba330695.png)
[C Language] Getting Started

【FPGA】abbreviation

LeetCode814算题第15天二叉树系列值《814 二叉树剪枝》

机器学习可以应用在哪些场景?机器学习有什么用?

《卫星界》刊评“星辰大海”计划:孙宇晨为太空旅游带来新的机遇

【FPGA】day20-I2C读写EEPROM

Description of ESB product development steps under cloud platform

Provincial level of Echart maps, as well as all prefecture-level download and use
随机推荐
uni-app - 获取汉字拼音首字母(根据中文获取拼音首字母)
The "top pillar" slides, and new growth is extremely difficult to shoulder the heavy responsibility. Is Ali "squatting" to jump higher?
shell monitors gpu usage
Read the article, high-performance and predictable data center network
The impact of programmatic trading and subjective trading on the profit curve!
The development of the massage chair control panel makes the massage chair simple and intelligent
【深度学习】基于卷积神经网络的天气识别训练
C# 一周入门高级编程之《C#-LINQ》Day Four
MySQL数据库存储引擎以及数据库的创建、修改与删除
华南师范宋宇老师课堂对话论文翻译
电力机柜数据监测RTU
C language recv() function, recvfrom() function, recvmsg() function
【FPGA】SDRAM
How to rebuild after pathman_config and pathman_config_params are deleted?
Alibaba Cloud releases 3 high-performance computing solutions
leetcode刷题第13天二叉树系列之《98 BST及其验证》
MYSQLg advanced ------ clustered and non-clustered indexes
直播软件搭建,流式布局,支持单选、多选等
Pinduoduo store business license related issues
console.log alternatives you didn't know about