不同于Oracle:SEQUENCE的区别

不同于Oracle:SEQUENCE的区别

前言

在使用Oracle数据库SEQUENCE功能时,发现Oracle对边界处理比较奇怪。刚好GreatSQL也支持SEQUENCE,就拿来一起比较一下。

先说结论:GreatSQL 的使用基本和Oracle基本一致,但是对 START WITH 的边界限制有所不同。

本次测试使用数据库的版本号

sql 复制代码
# 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

  1. INCREMENT BY的值大于0时(递增序列),可以用NOMAXVALUE;
  2. INCREMENT BY的值小于0时(递减序列),可以用NOMINVALUE。

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.

CYCLE/NOCYCLE

如果是CYCLE,当序列的值超出设定的范围时,会从最大值/最小值开始重新进行循环。

递增数列从最小值开始循环,递减数列从最大值开始循环。

sql 复制代码
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 WITHMINVALUE小创建失败:

sql 复制代码
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 WITHMAXVALUE大:

sql 复制代码
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 WITHMINVALUE小1 的时候,SEQUENCE 还能正常创建:

sql 复制代码
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,创建后不能使用:

sql 复制代码
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 WITHMINVALUE小太多就不能创建了:

sql 复制代码
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 WITHMAXVALUE大1就能创建

sql 复制代码
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,创建后不能使用:

sql 复制代码
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那样的特殊情况

sql 复制代码
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 会有两个特殊情况。

相关文档


Enjoy GreatSQL :)

关于 GreatSQL

GreatSQL是适用于金融级应用的国内自主开源数据库,具备高性能、高可靠、高易用性、高安全等多个核心特性,可以作为MySQL或Percona Server的可选替换,用于线上生产环境,且完全免费并兼容MySQL或Percona Server。

相关链接: GreatSQL社区 Gitee GitHub Bilibili

GreatSQL社区:

社区有奖建议反馈: greatsql.cn/thread-54-1...

社区博客有奖征稿详情: greatsql.cn/thread-100-...

(对文章有疑问或者有独到见解都可以去社区官网提出或分享哦~)

技术交流群:

微信&QQ群:

QQ群:533341697

微信群:添加GreatSQL社区助手(微信号:wanlidbc )好友,待社区助手拉您进群。

相关推荐
敖云岚1 小时前
【Redis】分布式锁的介绍与演进之路
数据库·redis·分布式
LUCIAZZZ2 小时前
HikariCP数据库连接池原理解析
java·jvm·数据库·spring·springboot·线程池·连接池
我在北京coding2 小时前
300道GaussDB(WMS)题目及答案。
数据库·gaussdb
小Tomkk2 小时前
阿里云 RDS mysql 5.7 怎么 添加白名单 并链接数据库
数据库·mysql·阿里云
明月醉窗台3 小时前
qt使用笔记二:main.cpp详解
数据库·笔记·qt
沉到海底去吧Go4 小时前
【图片自动识别改名】识别图片中的文字并批量改名的工具,根据文字对图片批量改名,基于QT和腾讯OCR识别的实现方案
数据库·qt·ocr·图片识别自动改名·图片区域识别改名·pdf识别改名
老纪的技术唠嗑局4 小时前
重剑无锋,大巧不工 —— OceanBase 中的 Nest Loop Join 使用技巧分享
数据库·sql
未来之窗软件服务4 小时前
JAVASCRIPT 前端数据库-V6--仙盟数据库架构-—-—仙盟创梦IDE
数据库·数据库架构·仙盟创梦ide·东方仙盟·东方仙盟数据库
一只爱撸猫的程序猿6 小时前
构建一个简单的智能文档问答系统实例
数据库·spring boot·aigc
nanzhuhe6 小时前
sql中group by使用场景
数据库·sql·数据挖掘