Oracle Database 建立與查詢 Sequence

來源:互聯網
上載者:User

相關的 Oracle Database Sequence 資訊如下:

建立 Sequence 文法

create sequence <schema>.<table_name>_s
increment by 1
start with 1
maxvalue 99999999999999999999
minvalue 1
nocycle;
 
其中 :
incerment: 每次增加的數值
start    : Sequence 開始數值
maxvalue : Sequence 最大值
nocycle  : 當 Sequence 達最大值時,不重頭開始

取得下一筆 Sequence 值

select <Sequence_name>.nextval[@db_link]
  from dual;

查詢目前 Sequence 值

文法 1:
select <Sequence_name>.currval[@db_link]
  from dual;
 
Mark 提供: 若 "目前所在的 DB Session" 尚未執行 nextval, 就先執行 currval, 則會得到
ORA-08002: sequence DB_SESSION_ID.CURRVAL is not yet defined in this session 錯誤.
 
文法 2:
select last_number - 1
   from all_sequences
 where sequence_name = '<Sequence_name>';

修改 Sequence

如: Alter Sequence <Sequence_name> MaxValue 999999999999999;

注意事項

1) Sequence 不能直接用在 "等式 :=" 之後.如: vID := tomSeq.nextval;
2) Sequence 可以直接用在 "SQL" 之中.如: insert into tomTable values( tomSeq.nextval, ... );

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.