mysql实现sequence功能的代码
作者:江上一叶舟 时间:2024-01-27 14:30:20
mysql实现sequence功能
1.建立sequence记录表
CREATE TABLE `sys_sequence` (
`seq_name` varchar(50) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
`min_value` int(11) NOT NULL,
`max_value` int(11) NOT NULL,
`current_value` int(11) NOT NULL,
`increment_value` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`seq_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
2.建立sequence基础函数
DELIMITER $$
CREATE DEFINER=`root`@`%` FUNCTION `_nextval`(name varchar(50)) RETURNS int(11)
begin
declare _cur int;
declare _maxvalue int; -- 接收最大值
declare _increment int; -- 接收增长步数
set _increment = (select increment_value from sys_sequence where seq_name = name);
set _maxvalue = (select max_value from sys_sequence where seq_name = name);
set _cur = (select current_value from sys_sequence where seq_name = name);
update sys_sequence -- 更新当前值
set current_value = _cur + increment_value
where seq_name = name ;
if(_cur + _increment >= _maxvalue) then -- 判断是都达到最大值
update sys_sequence
set current_value = min_value
where seq_name = name ;
end if;
return _cur;
end$$
DELIMITER ;
3.插入想要建立的sequence
INSERT INTO `mydb`.`sys_sequence`
(`seq_name`,
`min_value`,
`max_value`,
`current_value`,
`increment_value`)
VALUES
('seq_name1', 1, 99999999, 1, 1);
4.使用sequence
select _nextval('seq_name1');
来源:https://blog.csdn.net/laladebon/article/details/82256194
标签:mysql,sequence,序列
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 读取DICOM头文件的实例
2021-12-03 19:30:48
![](https://img.aspxhome.com/file/2023/2/68362_0s.jpg)
SQLServe 重复行删除方法
2024-01-26 18:39:24
详解如何在Go项目中输出版本信息
2024-04-26 17:17:20
Ubuntu 18.04 安装mysql5.7
2024-01-26 07:53:09
Python Tkinter GUI编程实现Frame切换
2023-05-08 07:36:28
![](https://img.aspxhome.com/file/2023/1/115511_0s.png)
python读取指定字节长度的文本方法
2022-04-17 10:42:45
修改mysql最大连接数的方法
2010-03-09 13:57:00
python通过nmap扫描在线设备并尝试AAA登录(实例代码)
2021-08-06 23:23:42
asp利用aspjpeg给图片生成PNG透明水印
2009-03-20 14:01:00
pytest allure添加环境信息实例讲解
2023-07-19 17:51:45
![](https://img.aspxhome.com/file/2023/8/89098_0s.png)
Python基础之输入,输出与高阶赋值详解
2023-05-21 10:00:32
详解python3中zipfile模块用法
2021-10-19 02:10:25
![](https://img.aspxhome.com/file/2023/2/130802_0s.png)
关于tensorflow的几种参数初始化方法小结
2023-10-15 12:05:26
Python字符串常用方法以及其应用场景详解
2022-02-15 18:39:53
Python代码调试的几种方法总结
2022-06-14 18:21:27
![](https://img.aspxhome.com/file/2023/5/71475_0s.jpg)
详解Python中sorted()和sort()的使用与区别
2022-05-06 17:39:09
Python类中的装饰器在当前类中的声明与调用详解
2021-03-07 10:29:35
![](https://img.aspxhome.com/file/2023/5/130565_0s.jpg)
Python Flask框架模板操作实例分析
2022-09-21 02:56:23
![](https://img.aspxhome.com/file/2023/0/84060_0s.png)
Python 如何实时向文件写入数据(附代码)
2022-11-10 13:40:03
![](https://img.aspxhome.com/file/2023/2/93682_0s.png)
Python爬取数据保存为Json格式的代码示例
2022-10-13 17:11:36