天天看點

ORACLE SEQUENCE 詳解

1.    about sequences(關于序列)

序列消除了串行化并且提高了應用程式一緻性。(想象一下沒有序列的日子怎麼辦?)

to create a sequence inyour own schema, you must have the <code>create</code> <code>sequence</code> system

privilege. 在自己模式下建立序列需要create sequence權限

to create a sequence inanother user's schema, you must have the <code>create</code> <code>any</code> <code>sequence</code> system

privilege. 在其他使用者模式下建立序列需要create any sequence權限。

文法:syntax

ORACLE SEQUENCE 詳解

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

-- create sequence

create sequence seq_t

minvalue  1

maxvalue  999999999999999999999999999

start  with  1

increment  by  1

cache  20;

語義semantics:

cache參數最大值為:

注意1:如果系統發生故障,所有緩存的沒有被dml語句使用并送出的序列值将丢失。潛在丢失值數量等于cache的數量。

注:order這個選項是對于并行系統而已確定sequence的in order,如果是單機sequence都是in order的。

例子:

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

create  sequence seq1

start  with  200

increment  by  10

maxvalue  200

cycle

nocache;

select  seq1.nextval   from dual;

結果:1

前提:

the sequence must be in your own schema, or youmust have the <code>alter</code> object privilege on

the sequence, or you must have the <code>alter</code> <code>any</code> <code>sequence</code> systemprivilege.

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

文法:

ORACLE SEQUENCE 詳解

語義:

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

sql&gt; 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&gt; alter sequence seq_t maxvalue 1;

alter sequence seq_t maxvalue 1

*

ora-04004: minvalue must be less than maxvalue

       thesequence must be in your own schema or you must have the drop any sequence system privilege.

删除序列必須要有drop  any  sequence權限

ORACLE SEQUENCE 詳解

<code>currval</code> and <code>nextval</code> can

be used in the following places:

·        <code>values</code> clause of <code>insert</code> statements 

·        the <code>select</code> list of a <code>select</code> statement

·        the <code>set</code> clause of an <code>update</code> statement

<code>currval</code> and <code>nextval</code> cannot

be used in these places: 不能用于以下場景

·        a subquery 子查詢

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

·        a <code>select</code> statement with the <code>distinct</code> operator 含distinct關鍵字查詢

·        a <code>select</code> statement with a <code>group</code> <code>by</code> or <code>order</code> <code>by</code> clause帶order

by 查詢語句

·        a <code>select</code> statement that is combined with

another <code>select</code> statement with the <code>union,</code> <code>intersect</code>,

or <code>minus</code> set operator含union, interest,minus操作符

·        the <code>where</code> clause of a <code>select</code> statement用在where條件中

·        <code>default</code> value of a column in a <code>create</code> <code>table</code> or <code>alter</code> <code>table</code> statement 列的預設值

·        the condition of a <code>check</code> constraint  

check限制