在文章 《mysql实现sequence功能的代码》 更新时间:2019年03月14日 11:06:11 作者:江上一叶舟 基础之上进行的实验完善。在mysql数据库上实现oracle 循环sequence功能。mysql 操作版本mysql 8.0.28 -- 1.建立sequence记录表 drop table crm_sequence; CREATE TABLE crm_sequence ( seq_name varchar(64) NOT NULL, min_value int NOT NULL, max_value int NOT NULL, current_value int NOT NULL, increment_value int NOT NULL DEFAULT '1', PRIMARY KEY (seq_name) ) ENGINE=InnoDB ; -- 2.建立_nextval函数,循环使用sequence drop FUNCTION _nextval; DELIMITER $$ CREATE FUNCTION _nextval(name varchar(64)) RETURNS int DETERMINISTIC begin declare _cur int; declare _maxvalue int; -- 接收最大值 declare _increment int; -- 接收增长步数 set _increment = (select increment_value from crm_sequence where seq_name = name); set _maxvalue = (select max_value from crm_sequence where seq_name = name); set _cur = (select current_value from crm_sequence where seq_name = name); if(_cur + _increment >= _maxvalue) then -- 判断下一值是否达到最大值,达到则重置当前值为最小值 update crm_sequence set current_value = min_value where seq_name = name ; else update crm_sequence -- 以指定步长更新当前值 set current_value = _cur + increment_value where seq_name = name ; end if; set _cur = (select current_value from crm_sequence where seq_name = name); return _cur; end$$ DELIMITER ; -- 3. 建立_currval函数,获取sequence当前值 drop FUNCTION _currval; DELIMITER $$ CREATE FUNCTION _currval(name varchar(64)) RETURNS int DETERMINISTIC begin declare _cur int; set _cur = (select current_value from crm_sequence where seq_name = name); return _cur; end$$ DELIMITER ; -- 4.向crm_sequence表中新建想要的sequnce INSERT INTO crm_sequence (seq_name, min_value, max_value, current_value, increment_value) VALUES ('business_group_id', 1, 99999999, 1, 1); -- 5.使用样例,使用sequence select _currval('business_group_id'); select _nextval('business_group_id'); select _currval('business_group_id');
mysql部分实现类似oracle sequence功能的测试
来源:这里教程网
时间:2026-03-01 16:35:54
作者:
编辑推荐:
下一篇:
相关推荐
-
雷神推出 MIX PRO II 迷你主机:基于 Ultra 200H,玻璃上盖 + ARGB 灯效
2 月 9 日消息,雷神 (THUNDEROBOT) 现已宣布推出基于英
-
制造商 Musnap 推出彩色墨水屏电纸书 Ocean C:支持手写笔、第三方安卓应用
2 月 10 日消息,制造商 Musnap 现已在海外推出一款 Oce
热文推荐
- MySQL 8.0新特性-并行查询innodb_parallel_read_threads
- 分析SQL给出索引优化建议的工具(美团开源)
分析SQL给出索引优化建议的工具(美团开源)
26-03-01 - 《MySQL 8从入门到精通(视频教学版)》简介
《MySQL 8从入门到精通(视频教学版)》简介
26-03-01 - 【北亚数据库数据恢复】使用delete未加where子句删除全表数据的Mysql数据库数据恢复
- 如何远程管理天翼云RDS数据库
如何远程管理天翼云RDS数据库
26-03-01 - 【北亚数据库数据恢复】误操作导致数据丢失的华为云mysql数据恢复案例
【北亚数据库数据恢复】误操作导致数据丢失的华为云mysql数据恢复案例
26-03-01 - 新版本 | GreatSQL 5.7.36正式发布,这些新增特性不容错过~
- 电源供电系列高稳定性抗干扰VK3604A 四键感应触摸/4路触控芯片原厂
电源供电系列高稳定性抗干扰VK3604A 四键感应触摸/4路触控芯片原厂
26-03-01 - web前端培训-MySQL的索引下推解析
web前端培训-MySQL的索引下推解析
26-03-01 - DEVOS勒索病毒,20G数据库2小时完成解密
DEVOS勒索病毒,20G数据库2小时完成解密
26-03-01
