不同于Oracle:SEQUENCE的区别
不同于Oracle:SEQUENCE的区别
前言
在使用Oracle数据库SEQUENCE功能时,发现Oracle对边界处理比较奇怪。刚好GreatSQL也支持SEQUENCE,就拿来一起比较一下。
先说结论:GreatSQL 的使用基本和Oracle基本一致,但是对 START WITH 的边界限制有所不同。
本次测试使用数据库的版本号
# Oracle版本 BANNER -------------------------------------------------------------------------------- Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production PL/SQL Release 11.2.0.4.0 - Production CORE 11.2.0.4.0 Production TNS for Linux: Version 11.2.0.4.0 - Production NLSRTL Version 11.2.0.4.0 - Production # GreatSQL版本 greatsql> \S ... Server version: 8.0.32-25 GreatSQL, Release 25, Revision 79f57097e3f ... 1 row in set (0.00 sec)
SEQUENCE 使用介绍
SEQUENCE 有以下几个常用的参数
参数名 | 介绍 |
---|---|
START WITH | 起始值 |
INCREMENT BY | 步长 |
MINVALUE/NOMINVALUE | 最小值 |
MAXVALUE/NOMINVALUE | 最大值 |
CYCLE/NOCYCLE | 是否回收 |
CACHE/NOCACHE | (cache性能好但有丢数据的风险) |
INCREMENT BY 怎么用
INCREMENT BY 的值大于0时,为递增序列
INCREMENT BY 的值小于0时,为递减序列
何时能使用NOMINVALUE &NOMINVALUE
- INCREMENT BY的值大于0时(递增序列),可以用NOMAXVALUE;
- INCREMENT BY的值小于0时(递减序列),可以用NOMINVALUE。
To create a sequence that increments without bound, for ascending sequences, omit the
MAXVALUE
parameter or specifyNOMAXVALUE
. For descending sequences, omit theMINVALUE
parameter or specify theNOMINVALUE
.
CYCLE/NOCYCLE
如果是CYCLE
,当序列的值超出设定的范围时,会从最大值/最小值开始重新进行循环。
递增数列从最小值开始循环,递减数列从最大值开始循环。
oracle> CREATE SEQUENCE seq1 START WITH 101 minvalue 100 INCREMENT BY -10 MAXVALUE 130 nocache CYCLE; #多次执行 oracle> select seq1.nextval from dual; #返回值依次为: 101->130->120->110>100
Oracle SEQUENCE 特性
START WITH 边界
默认情况下是认为 MINVALUE
>= START WITH
>= MAXVALUE
,超出区间就不能创建SEQUENCE
START WITH
比MINVALUE
小创建失败:
oracle> create SEQUENCE MY_FIRST_SEQUENCE start with -2 increment by -1 minvalue 1 maxvalue 100 nocycle nocache; 2 3 4 5 6 7 create SEQUENCE MY_FIRST_SEQUENCE * ERROR at line 1: ORA-04006: START WITH ???? MINVALUE
START WITH
比MAXVALUE
大:
oracle> create SEQUENCE MY_SECOND_SEQUENCE start with 101 increment by -1 minvalue 1 maxvalue 100 nocycle nocache; 2 3 4 5 6 7 create SEQUENCE MY_SECOND_SEQUENCE * ERROR at line 1: ORA-04008: START WITH ???? MAXVALUE
特殊情况
在使用SEQUENCE的时候发现有两种特殊情况:
一 、当INCREMENT BY < 0 处于递减数列时
递减数列,START WITH
比 MINVALUE
小1 的时候,SEQUENCE 还能正常创建:
oracle> create SEQUENCE MY_FIRST_SEQUENCE start with -2 increment by -1 minvalue -1 maxvalue 100 nocycle nocache; 2 3 4 5 6 7 Sequence created.
但是SEQUENCE 是 NOCYCLE
,创建后不能使用:
oracle> select MY_FIRST_SEQUENCE.nextval from dual; select MY_FIRST_SEQUENCE.nextval from dual * ERROR at line 1: ORA-08004: ?? MY_FIRST_SEQUENCE.NEXTVAL goes below MINVALUE ?????
START WITH
比MINVALUE
小太多就不能创建了:
oracle> create SEQUENCE MY_FIRST_SEQUENCE start with -3 increment by -1 minvalue -1 maxvalue 100 nocycle nocache; 2 3 4 5 6 7 create sequence MY_FIRST_SEQUENCE * ERROR at line 1: ORA-04006: START WITH ???? MINVALUE oracle> drop SEQUENCE MY_FIRST_SEQUENCE; Sequence dropped. oracle> create SEQUENCE MY_FIRST_SEQUENCE start with 101 increment by -1 minvalue 1 maxvalue 100 nocycle nocache; 2 3 4 5 6 7 create sequence MY_FIRST_SEQUENCE * ERROR at line 1: ORA-04008: START WITH ???? MAXVALUE oracle> create sequence MY_FIRST_SEQUENCE start with -1 increment by -1 minvalue 1 maxvalue 100 nocycle nocache; 2 3 4 5 6 7 create sequence MY_FIRST_SEQUENCE * ERROR at line 1: ORA-04006: START WITH ???? MINVALUE
二、当INCREMENT BY > 0 处于递增数列时
递增数列时情况相反
START WITH
比MAXVALUE
大1就能创建
oracle> create sequence MY_FIRST_SEQUENCE start with 101 increment by 1 minvalue 1 maxvalue 100 nocycle nocache; 2 3 4 5 6 7 Sequence created.
但是 SEQUENCE 为 NOCYCLE
,创建后不能使用:
oracle> select MY_FIRST_SEQUENCE.nextval from dual; select MY_FIRST_SEQUENCE.nextval from dual * ERROR at line 1: ORA-08004: ?? MY_FIRST_SEQUENCE.NEXTVAL exceeds MAXVALUE ?????
sequence Specify the name of the sequence to be created. The name must satisfy the requirements listed in "Database Object Naming Rules". If you specify none of the clauses INCREMENT BY through GLOBAL, then you create an ascending sequence that starts with 1 and increases by 1 with no upper limit. Specifying only INCREMENT BY -1 creates a descending sequence that starts with ‐1 and decreases with no lower limit. To create a sequence that increments without bound, for ascending sequences, omit the MAXVALUE parameter or specify NOMAXVALUE. For descending sequences, omit the MINVALUE parameter or specify the NOMINVALUE. To create a sequence that stops at a predefined limit, for an ascending sequence, specify a value for the MAXVALUE parameter. For a descending sequence, specify a value for the MINVALUE parameter. Also specify NOCYCLE. Any attempt to generate a sequence number once the sequence has reached its limit results in an error. To create a sequence that restarts after reaching a predefined limit, specify values for both the MAXVALUE and MINVALUE parameters. Also specify CYCLE.
GreatSQL 特性
GreatSQL 的使用就比较严格了: MINVALUE
>= START WITH
>= MAXVALUE
没发现像Oracle那样的特殊情况
greatsql> create sequence MY_FIRST_SEQUENCE -> start with -1 -> increment by 1 -> minvalue 1 -> maxvalue 100 -> nocycle -> nocache; ERROR 8582 (HY000): create sequence failed, cause START WITH should between MINVALUE and MAXVALUE! greatsql> create sequence MY_FIRST_SEQUENCE -> start with 101 -> increment by 1 -> minvalue 1 -> maxvalue 100 -> nocycle -> nocache; ERROR 8582 (HY000): create sequence failed, cause START WITH should between MINVALUE and MAXVALUE! greatsql> create sequence MY_FIRST_SEQUENCE -> start with 102 -> increment by 1 -> minvalue 1 -> maxvalue 100 -> nocycle -> nocache; ERROR 8582 (HY000): create sequence failed, cause START WITH should between MINVALUE and MAXVALUE! greatsql> create sequence MY_FIRST_SEQUENCE -> start with 101 -> increment by -1 -> minvalue 1 -> maxvalue 100 -> nocycle -> nocache; ERROR 8582 (HY000): create sequence failed, cause START WITH should between MINVALUE and MAXVALUE! greatsql> create sequence MY_FIRST_SEQUENCE -> start with -1 -> increment by -1 -> minvalue 1 -> maxvalue 100 -> nocycle -> nocache; ERROR 8582 (HY000): create sequence failed, cause START WITH should between MINVALUE and MAXVALUE! greatsql> create sequence MY_FIRST_SEQUENCE -> start with 0 -> increment by -1 -> minvalue 1 -> maxvalue 100 -> nocycle -> nocache; ERROR 8582 (HY000): create sequence failed, cause START WITH should between MINVALUE and MAXVALUE! greatsql> drop sequence MY_FIRST_SEQUENCE; ERROR 1046 (3D000): No database selected greatsql> create sequence MY_FIRST_SEQUENCE -> start with -10 -> increment by -1 -> minvalue -9 -> maxvalue 100 -> nocycle -> nocache; ERROR 8582 (HY000): create sequence failed, cause START WITH should between MINVALUE and MAXVALUE!
总结
GreatSQL 和 Oracle 对 START WITH
的边界定义基本一致,都是 MINVALUE
>= START WITH
>= MAXVALUE
,但是 Oracle 会有两个特殊情况。
相关文档
-
SEQUENCE Oracle文档:
-
GreatSQL SEQUENCE文档:
-
ORA-04013,CACHE 值必须小于CYCLE值;解决方案
Enjoy GreatSQL :)
关于 GreatSQL
GreatSQL是适用于金融级应用的国内自主开源数据库,具备高性能、高可靠、高易用性、高安全等多个核心特性,可以作为MySQL或Percona Server的可选替换,用于线上生产环境,且完全免费并兼容MySQL或Percona Server。
相关链接: GreatSQL社区 Gitee GitHub Bilibili
GreatSQL社区:
社区有奖建议反馈: https://greatsql.cn/thread-54-1-1.html
社区博客有奖征稿详情: https://greatsql.cn/thread-100-1-1.html
(对文章有疑问或者有独到见解都可以去社区官网提出或分享哦~)
技术交流群:
微信&QQ群:
QQ群:533341697
微信群:添加GreatSQL社区助手(微信号:wanlidbc
)好友,待社区助手拉您进群。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
月之暗面真的很强?我们来用一个问题评测这些 ChatGPT 平替
号称平替甚至超越 ChatGPT 的产品层出不穷,今天就来做一个横向评测。本次评测的对象有: ChatGPT 3.5 ChatGPT 4 Google Gemini Anthropic Claude 3 Sonnet Perplexity devv 月之暗面 Kimi Chat 评测方法很简单: 打开一个新会话 提问 What is Bytebase 挑这个问题的原是因为我们最了解这个主题,只有这样,才能通过细节判定高下。大家也可以根据自己的主题进行验证。好,下面进入正题。 ChatGPT 3.5 比较简短,基本也对。但是「Organize, document, and share their databases, schemas, and related documentation」这句是 AI 自己提炼的总结,过度发挥了。这里把 Bytebase 描述成了一个数据库文档工具,不过后面一句话又掰回来了。 ChatGPT 4 比 3.5 要丰富很多,不过有两个问题: 没有抓住对象。应该点出针对 Developer 和 DBA。 出现了一个幻觉,Bytebase 并不支持 SQLite。...
- 下一篇
RocketMQ 之 IoT 消息解析:物联网需要什么样的消息技术?
作者:林清山(隆基) 前言: 从初代开源消息队列崛起,到 PC 互联网、移动互联网爆发式发展,再到如今 IoT、云计算、云原生引领了新的技术趋势,消息中间件的发展已经走过了 30 多个年头。 目前,消息中间件在国内许多行业的关键应用中扮演着至关重要的角色。随着数字化转型的深入,客户在使用消息技术的过程中往往同时涉及交叉场景,比如同时进行物联网消息、微服务消息的处理,同时进行应用集成、数据集成、实时分析等,企业需要为此维护多套消息系统,付出更多的资源成本和学习成本。 在这样的背景下,2022 年,RocketMQ 5.0 正式发布,相对于 RocketMQ 4.0,架构走向云原生化,并且覆盖了更多的业务场景。 物联网消息场景 我们先来了解一下物联网的场景是什么?消息在物联网里面有什么作用? 物联网肯定是最近几年最火的技术趋势之一,有大量的研究机构、行业报告都提出了物联网快速发展的态势: 首先,物联网设备规模爆发式增长,预测会在 2025 年达到 200 多亿台。 其次,物联网的数据规模快速增长,来自物联网的数据增速接近 28%,并且未来有 90% 以上的实时数据来自物联网场景。这也就意味...
相关文章
文章评论
共有0条评论来说两句吧...