不同于Oracle:SEQUENCE的区别

2024-04-09 12:20

本文主要是介绍不同于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/NOMAXVALUE最大值
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,当序列的值超出设定的范围时,会从最大值/最小值开始重新进行循环。

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

oracle> CREATE SEQUENCE seq1
START WITH 101
minvalue 100
INCREMENT BY -10
MAXVALUE 130
nocacheCYCLE;#多次执行
oracle> select seq1.nextval from dual;
#返回值依次为:
101->130->120->110>100

Oracle SEQUENCE 特性

START WITH 边界

默认情况下是认为 MINVALUE <= START WITH <= MAXVALUE,超出区间就不能创建SEQUENCE

START WITHMINVALUE小创建失败:

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大:

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 还能正常创建:

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 WITHMINVALUE小太多就不能创建了:

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 ???? MINVALUEoracle> 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 ???? MAXVALUEoracle> 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就能创建

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文档:

    • https://docs.oracle.com/en/database/oracle/oracle-database/23/sqlrf/CREATE-SEQUENCE.html#GUID-E9C78A8C-615A-4757-B2A8-5E6EFB130571

    • https://docs.oracle.com/en/database/oracle/oracle-database/23/sqlrf/Sequence-Pseudocolumns.html

  • GreatSQL SEQUENCE文档:

    • https://greatsql.cn/docs/8032-25/user-manual/5-enhance/sql-compat/5-3-easyuse-ora-syntax-sequence.html
  • ORA-04013,CACHE 值必须小于CYCLE值;解决方案

    • https://www.cnblogs.com/PingPo/p/14312384.html

这篇关于不同于Oracle:SEQUENCE的区别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/888141

相关文章

native和static native区别

本文基于Hello JNI  如有疑惑,请看之前几篇文章。 native 与 static native java中 public native String helloJni();public native static String helloJniStatic();1212 JNI中 JNIEXPORT jstring JNICALL Java_com_test_g

Android fill_parent、match_parent、wrap_content三者的作用及区别

这三个属性都是用来适应视图的水平或者垂直大小,以视图的内容或尺寸为基础的布局,比精确的指定视图的范围更加方便。 1、fill_parent 设置一个视图的布局为fill_parent将强制性的使视图扩展至它父元素的大小 2、match_parent 和fill_parent一样,从字面上的意思match_parent更贴切一些,于是从2.2开始,两个属性都可以使用,但2.3版本以后的建议使

Collection List Set Map的区别和联系

Collection List Set Map的区别和联系 这些都代表了Java中的集合,这里主要从其元素是否有序,是否可重复来进行区别记忆,以便恰当地使用,当然还存在同步方面的差异,见上一篇相关文章。 有序否 允许元素重复否 Collection 否 是 List 是 是 Set AbstractSet 否

javascript中break与continue的区别

在javascript中,break是结束整个循环,break下面的语句不再执行了 for(let i=1;i<=5;i++){if(i===3){break}document.write(i) } 上面的代码中,当i=1时,执行打印输出语句,当i=2时,执行打印输出语句,当i=3时,遇到break了,整个循环就结束了。 执行结果是12 continue语句是停止当前循环,返回从头开始。

Oracle type (自定义类型的使用)

oracle - type   type定义: oracle中自定义数据类型 oracle中有基本的数据类型,如number,varchar2,date,numeric,float....但有时候我们需要特殊的格式, 如将name定义为(firstname,lastname)的形式,我们想把这个作为一个表的一列看待,这时候就要我们自己定义一个数据类型 格式 :create or repla

maven发布项目到私服-snapshot快照库和release发布库的区别和作用及maven常用命令

maven发布项目到私服-snapshot快照库和release发布库的区别和作用及maven常用命令 在日常的工作中由于各种原因,会出现这样一种情况,某些项目并没有打包至mvnrepository。如果采用原始直接打包放到lib目录的方式进行处理,便对项目的管理带来一些不必要的麻烦。例如版本升级后需要重新打包并,替换原有jar包等等一些额外的工作量和麻烦。为了避免这些不必要的麻烦,通常我们

ORACLE 11g 创建数据库时 Enterprise Manager配置失败的解决办法 无法打开OEM的解决办法

在win7 64位系统下安装oracle11g,在使用Database configuration Assistant创建数据库时,在创建到85%的时候报错,错误如下: 解决办法: 在listener.ora中增加对BlueAeri-PC或ip地址的侦听,具体步骤如下: 1.启动Net Manager,在“监听程序”--Listener下添加一个地址,主机名写计

ActiveMQ—Queue与Topic区别

Queue与Topic区别 转自:http://blog.csdn.net/qq_21033663/article/details/52458305 队列(Queue)和主题(Topic)是JMS支持的两种消息传递模型:         1、点对点(point-to-point,简称PTP)Queue消息传递模型:         通过该消息传递模型,一个应用程序(即消息生产者)可以

Oracle Start With关键字

Oracle Start With关键字 前言 旨在记录一些Oracle使用中遇到的各种各样的问题. 同时希望能帮到和我遇到同样问题的人. Start With (树查询) 问题描述: 在数据库中, 有一种比较常见得 设计模式, 层级结构 设计模式, 具体到 Oracle table中, 字段特点如下: ID, DSC, PID; 三个字段, 分别表示 当前标识的 ID(主键), DSC 当

oracle分页和mysql分页

mysql 分页 --查前5 数据select * from table_name limit 0,5 select * from table_name limit 5 --limit关键字的用法:LIMIT [offset,] rows--offset指定要返回的第一行的偏移量,rows第二个指定返回行的最大数目。初始行的偏移量是0(不是1)。   oracle 分页 --查前1-9