mysql-12序列使用
mysql序列是一组整数:1,2,3....,由于一张数据表只能有一个字段自增主键,如果你想实现其他字段自动增加,就可以使用mysql序列来实现。
使用auto_increment来定义列
drop table if EXISTS test_autoincrement ;
create table `test_autoincrement`(
`id` int UNSIGNED not null auto_increment,
PRIMARY KEY (id),
`name` varchar(20) not null,
`date` date not null,
`origin` varchar(30)
);
insert into test_autoincrement (name,date,origin) values
('housefly','2001-09-10','kitchen');
-- 使用last_insert_id() 可以查看当前操作的次数
select LAST_INSERT_ID();
select * from test_autoincrement;
insert into test_autoincrement values
(null,"millipede",'2001-09-10','driverway'),
(null,"grasshopper",'2001-09-10','front yard');
-- 即便插入多行,也只显示插入第一行时产生的值
select LAST_INSERT_ID();
select * from test_autoincrement;
![](https://img2018.cnblogs.com/blog/1418970/201811/1418970-20181115170851429-816107452.png)
重置序列
先删除列,再添加列
drop table if EXISTS test_autoincrement ;
create table `test_autoincrement`(
`id` int UNSIGNED not null auto_increment,
PRIMARY KEY (id),
`name` varchar(20) not null,
`date` date not null,
`origin` varchar(30)
);
insert into test_autoincrement (name,date,origin) values
('housefly','2001-09-10','kitchen'),
("millipede",'2001-09-10','driverway'),
("grasshopper",'2001-09-10','front yard');
alter table test_autoincrement drop id;
select * from test_autoincrement;
alter table test_autoincrement add id int(3) UNSIGNED not null auto_increment FIRST,
add primary key (id);
select * from test_autoincrement;
![](https://img2018.cnblogs.com/blog/1418970/201811/1418970-20181115172553359-1178335844.png)
设置序列的开始值
建表时设置序列值
create table `test_autoincrement`
(`id` int(3) UNSIGNED not null auto_increment,
primary key (id),
`name` varchar(20) not null )
engine=innodb auto_increment=100 charset=utf8;
insert into test_autoincrement values
(null,"tom"),
(null,"jerry");
select * from test_autoincrement;
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 手把手教你更优雅的享受 DeepSeek
· 腾讯元宝接入 DeepSeek R1 模型,支持深度思考 + 联网搜索,好用不卡机!
· AI工具推荐:领先的开源 AI 代码助手——Continue
· 探秘Transformer系列之(2)---总体架构
· V-Control:一个基于 .NET MAUI 的开箱即用的UI组件库