首页 > 数据库 >不同于Oracle:SEQUENCE的区别

不同于Oracle:SEQUENCE的区别

时间:2024-04-08 09:55:19浏览次数:18  
标签:sequence create SEQUENCE 不同于 START Oracle MY FIRST

不同于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

  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
nocache
 CYCLE;

#多次执行
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 ???? 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就能创建

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 会有两个特殊情况。

相关文档


Enjoy GreatSQL

标签:sequence,create,SEQUENCE,不同于,START,Oracle,MY,FIRST
From: https://www.cnblogs.com/greatsql/p/18120493

相关文章

  • oracle 数据库精简模式磁盘空间回收处理
            最近遇到的项目中,需要部署几套oracle19cRAC数据库,在进行存储磁盘卷划分的时候,发现只能分配精简卷模式的磁盘,出于性能以及安全考虑,咨询存储原厂答曰该powerstore存储只支持精简磁盘卷模式。    自oracle12c开始,asm开始支持精简卷模式,asm磁盘组......
  • 【SQL】Oracle的内连接、左外连接、右外连接及全外连接
    理解Oracle的各种连接方法的最有效的方法就是“躬亲”,在实践中去深刻理解内连接,左外连接,右外连接,全外连接的概念的和效果。1.创建测试表并准备测试数据sec@ora10g>createtablea(anumber(1),bnumber(1),cnumber(1));sec@ora10g>createtableb(anumber(1),dnumber(1),e......
  • Oracle EBS 查询用户密码
    程序包头: CREATEORREPLACEPACKAGEcux_fnd_web_secISFUNCTIONget_user_pass(p_fnd_userINVARCHAR2,p_guest_loginINVARCHAR2DEFAULT'GUEST/ORACLE')RETURNVARCHAR2;FUNCTIONget_apps_pass(p_guest_loginINV......
  • 【OracleEBS】 科目余额SQL
     selectgb.period_year会计年度,gb.period_name会计期间,gcck.concatenated_segments科目代码,gl_flexfields_pkg.get_description_sql(gcck.chart_of_accounts_id,'',......
  • 【OracleEBS】 用PL/SQL运行工作流
      declarev_itemtypevarchar2(30):='TPLEAVE';--ItemTypeInternalNamv_processvarchar2(30):='DEFAULT_PROCESS';--ProcessNamev_itemkeyvarchar2(30);beginselectrcv_transactions_s.nextvalintov_itemkeyfromd......
  • 【OracleEBS】 根据组织id得到帐套id和公司名称
      declarel_org_information3varchar2(150);--帐套idl_company_descvarchar2(150);--公司中文描述beginselecto3.org_information3,o3.attribute3intol_org_information3,l_company_descfromhr_all_organization_unitso,hr_a......
  • 【OracleEBS】 在PL/SQL中调用Oracle ERP请求
      procedureprc_do_import_request(prm_orginnumber,prm_appcodeoutnumber,prm_appmsgoutvarchar2)iscustom_exceptionexception;successboolean;v_request_......
  • 【OracleEBS】 订单暂挂问题sql解决
    ---查询请购单的状态select*frompo_requisition_headers_allporwherepor.requisition_header_id=63578;---修改请购单状态为未提交审批updatepo_requisition_headers_allporhsetporh.authorization_status='INCOMPLETE'whereporh.requisition_header_id=6......
  • Oracle 实现当月日历
    selectmax(su)su,max(mo)mo,max(tu)tu,max(we)we,max(th)th,max(fr)fr,max(sa)safrom(selectcasewhend=1thenddendsu,casewhend=2thenddendmo,casewhend=3thenddendtu,casewhend=4thenddendwe,casewhend=5t......
  • Oracle 提取第5条到第10条的数据
    DEMO --创建表createtablecux_num_temp(namevarchar2(100),agenumber,addtimedate);---插入测试数据insertintocux_num_temp(name,age,addtime)values('123',99,sysdate);insertintocux_num_temp(name,age,addtime)values(&#......