ORACLE SEQUENCE 詳解

來源:互聯網
上載者:User

1.    About Sequences(關於序列)

序列是資料庫物件一種。多個使用者可以通過序列產生連續的數字以此來實現主鍵欄位的自動、唯一增長,並且一個序列可為多列、多表同時使用。

序列消除了序列化並且提高了應用程式一致性。(想象一下沒有序列的日子怎麼辦。) 2.   Creating Sequences(建立序列)

前提:Prerequisites

To create a sequence inyour own schema, you must have the CREATE SEQUENCE system privilege. 在自己模式下建立序列需要create sequence許可權

To create a sequence inanother user's schema, you must have the CREATE ANY SEQUENCE system privilege. 在其他使用者模式下建立序列需要create any sequence許可權。

文法:Syntax


如果不加條件陳述式,預設建立的序列格式如下:

-- Create sequence

create sequence SEQ_T

minvalue  1

maxvalue  999999999999999999999999999

start  with  1

increment  by  1

cache  20;

 

語義Semantics:

INCREMENT BY指定序列增長步長。可以為正(升序)、負整數(降序),但不能為0。最高精度28。

START WITH: 指定序列起始數。預設為序列最小值。

MAXVALUE :指定序列最大值。最大28位。必須大於等於起始值且大於等於序列最小值。

NOMAXVALUE:  無最大值(實際為10^27或-1)。default

MINVALUE :指定序列最小值。

NOMINVALUE  :無最小值(實際為1或-10^26)。Default

CYCLE  :指定序列達到最大值或最小值後繼續從頭開始產生。

NOCYCLE :不迴圈產生。Default.

CACHE :指定資料庫記憶體中預分配的序列值個數,以便快速擷取。最小cache值為2。

Cache參數最大值為:

(CEIL (MAXVALUE - MINVALUE)) / ABS (INCREMENT)

注意1:如果系統發生故障,所有緩衝的沒有被DML語句使用並提交的序列值將丟失。潛在丟失值數量等於cache的數量。

NOCACHE  :不指定緩衝數,預設緩衝20

ORDER :指定order條件保證序列按請求順序產生。此條件適用於RAC環境。

NOORDER :不保證序列按請求順序產生。

 

例子:

CREATE SEQUENCE customers_seq
 START WITH     1000
 INCREMENT BY   1
 NOCACHE
 NOCYCLE;

 

注意2:帶有cycle條件序列當達到最大值後,下一個值從最小值minvalue開始迴圈。

CREATE  SEQUENCE seq1

START  WITH  200

INCREMENT  BY  10

MAXVALUE  200

CYCLE

NOCACHE;

 

SELECT  seq1.nextval   FROM dual;

結果:1 3.   ALTER SEQUENCE(修改序列)

 

前提:

The sequence must be in your own schema, or youmust have the ALTER object privilege on

the sequence, or you must have the ALTER ANY SEQUENCE systemprivilege.

修改自己模式序列需要alter object許可權,修改其他模式序列需要alter any sequence許可權。

 

文法:

 

語義:

 

1)如果想以不同的數字重新開始序列,必須刪除重建。

SQL> alter sequence seq_t start with 2;

alter sequence seq_t start with 2

                     *

ERROR at line 1:

ORA-02283: cannot alter starting sequencenumber

2)修改的maxvalue必須大於序列當前值。

SQL> alter sequence seq_t maxvalue 1;

alter sequence seq_t maxvalue 1

*

ERROR at line 1:

ORA-04004: MINVALUE must be less than MAXVALUE

 

 

例子:

ALTER SEQUENCE customers_seq 
   MAXVALUE 1500;

 

ALTER SEQUENCE customers_seq 
   CYCLE
   CACHE 5; 

  4.   DROP SEQUENCE(刪除序列)

前提:

       Thesequence must be in your own schema or you must have the DROP ANY SEQUENCE system privilege.

 

刪除序列必須要有drop  any  sequence許可權

 

文法:

 

例子:

DROP SEQUENCE oe.customers_seq; 

  5.    NEXTVAL and CURRVAL的使用限制

CURRVAL and NEXTVAL can be used in the following places:

·        VALUES clause of INSERT statements 

·        The SELECT list of a SELECT statement

·        The SET clause of an UPDATE statement

CURRVAL and NEXTVAL cannot be used in these places: 不能用於以下情境

·        A subquery 子查詢

·        A view query or materialized view query 視圖或物化視圖查詢

·        A SELECT statement with the DISTINCT operator 含distinct關鍵字查詢

·        A SELECT statement with a GROUP BY or ORDER BY clause帶order by 查詢語句

·        A SELECT statement that is combined with another SELECT statement with the UNION, INTERSECT, or MINUS set operator含union, interest,minus操作符

·        The WHERE clause of a SELECT statement用在where條件中

·        DEFAULT value of a column in a CREATE TABLE or ALTER TABLE statement 列的預設值

·        The condition of a CHECK constraint   check約束

 

 

 

 

 

 

 

 

 --------------------------------------

Dylan    Presents.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.