Oracle Sequence 相關知識(收集)

轉於http://blog.csdn.net/dingxy/article/details/2097447

 

注:你或者是該sequence的owner,或者有ALTER ANY sequence權限才能改動sequence。可
以alter除start值之外的所有sequence參數。如果想要改變start值,必須drop sequence
再re-create。

 

一.簡單介紹

在oracle中sequence就是所謂的序列號,每次取的時候它會自動增加,一般用在需要按
序列號排序的地方。

1、 create sequence
你首先要有create sequence或者create any sequence權限,
create sequence emp_sequence
INCREMENT BY 1 -- 每次加幾個
START WITH 1 -- 從1開始計數
NOMAXVALUE -- 不設置最大值
NOCYCLE -- 一直累加,不循環
CACHE 10; --預分配緩存大小爲10

一旦定義了emp_sequence,你就可以用CURRVAL,NEXTVAL
CURRVAL=返回sequence的當前值
NEXTVAL=增加sequence的值,然後返回sequence值
比如:
emp_sequence.CURRVAL
emp_sequence.NEXTVAL

可以使用sequence的地方:
- 不包含子查詢、snapshot、VIEW的 SELECT 語句
- INSERT語句的子查詢中
- NSERT語句的VALUES中
- UPDATE 的 SET中

可以看如下例子:
INSERT INTO emp VALUES
(empseq.nextval, 'LEWIS', 'CLERK',7902, SYSDATE, 1200, NULL, 20);

SELECT empseq.currval FROM DUAL;

但是要注意的是:
- 第一次NEXTVAL返回的是初始值;隨後的NEXTVAL會自動增加你定義的INCREMENT BY值,
然後返回增加後的值。CURRVAL 總是返回當前sequence的值,但是在第一次NEXTVAL
初始化之後才能使用CURRVAL,否則會出錯。一次NEXTVAL會增加一次sequence的值,
所以如果你在同一個語句裏面使用多個NEXTVAL,其值就是不一樣的。明白?
- 如果指定CACHE值,oracle就可以預先在內存裏面放置一些sequence,這樣存取的快
些。
cache裏面的取完後,oracle自動再取一組到cache。 使用cache或許會跳號, 比如
數據庫突然不正常down掉(shutdown abort),cache中的sequence就會丟失. 所以可
以在create sequence的時候用nocache防止這種情況。

2、 Alter sequence
你或者是該sequence的owner,或者有ALTER ANY sequence權限才能改動sequence。 可
以alter除start值之外的所有sequence參數。如果想要改變start值,必須drop sequence
再re-create。例子:
ALTER sequence emp_sequence
INCREMENT BY 10
MAXVALUE 10000
CYCLE -- 到10000後從頭開始
NOCACHE;

影響sequence的初始化參數:
sequence_CACHE_ENTRIES =
設置能同時被cache的sequence數目。

可以很簡單的Drop sequence
DROP sequence order_seq;

二,英文介紹

PURPOSE:
 To create a sequence. A sequence is a database object from which
 multiple users may generate unique integers. You can use sequences
 to automatically generate primary key values.
 
SYNTAX:
 
CREATE SEQUENCE [schema.]sequence
 [INCREMENT BY integer]
 [START WITH integer]
 [MAXVALUE integer | NOMAXVALUE]
 [MINVALUE integer | NOMINVALUE]
 [CYCLE | NOCYCLE]
 [CACHE integer | NOCACHE]
 [ORDER | NOORDER]
 
where:
 
schema
 is the schema to contain the sequence. If you omit schema, Oracle
 creates the sequence in your own schema.
 
sequence
 is the name of the sequence to be created.
 
INCREMENT BY
 specifies the interval between sequence numbers. This value can be
 any positive or negative Oracle integer, but it cannot be 0. If
 this value is negative, then the sequence descends. If the
 increment is positive, then the sequence ascends. If you omit this
 clause, the interval defaults to 1.
 
MINVALUE
 specifies the sequence's minimum value.
 
NOMINVALUE
 specifies a minimum value of 1 for an ascending sequence or -10
 for a descending sequence.
 
 The default is NOMINVALUE.
 
MAXVALUE
 specifies the maximum value the sequence can generate.
 
NOMAXVALUE
 specifies a maximum value of 10
 for a descending sequence.
 
 The default is NOMAXVALUE.
 
START WITH
 specifies the first sequence number to be generated. You can use
 this option to start an ascending sequence at a value greater than
 its minimum or to start a descending sequence at a value less than
 its maximum. For ascending sequences, the default value is the
 sequence's minimum value. For descending sequences, the default
 value is the sequence's maximum value.
 
CYCLE
 specifies that the sequence continues to generate values after
 reaching either its maximum or minimum value. After an ascending
 sequence reaches its maximum value, it generates its minimum value.
 After a descending sequence reaches its minimum, it generates its
 maximum.
 
NOCYCLE
 specifies that the sequence cannot generate more values after
 reaching its maximum or minimum value.
 
 The default is NOCYCLE.
 
CACHE
 specifies how many values of the sequence Oracle preallocates and
 keeps in memory for faster access. The minimum value for this
 parameter is 2. For sequences that cycle, this value must be less
 than the number of values in the cycle.
 
NOCACHE
 specifies that values of the sequence are not preallocated.
 
 If you omit both the CACHE parameter and the NOCACHE option, Oracle
 caches 20 sequence numbers by default. However, if you are using
 Oracle with the Parallel Server option in parallel mode and you
 specify the ORDER option, sequence values are never cached,
 regardless of whether you specify the CACHE parameter or the NOCACHE
 option.
 
ORDER
 guarantees that sequence numbers are generated in order of request.
 You may want to use this option if you are using the sequence
 numbers as timestamps. Guaranteeing order is usually not important
 for sequences used to generate primary keys.
 
NOORDER
 does not guarantee sequence numbers are generated in order of
 request.
 
 If you omit both the ORDER and NOORDER options, Oracle chooses
 NOORDER by default. Note that the ORDER option is only necessary to
 guarantee ordered generation if you are using Oracle with the
 Parallel Server option in parallel mode. If you are using exclusive
 mode, sequence numbers are always generated in order.
 
PREREQUISITES:
 To create a sequence in your own schema, you must have CREATE
 SEQUENCE privilege.
 
 To create a sequence in another user's schema, you must have CREATE
 ANY SEQUENCE privilege. If you are using Trusted Oracle in DBMS MAC
 mode, your DBMS label must dominate the creation label of the owner
 of the schema to contain the sequence.
 
SEE:
ALTER SEQUENCE, DROP SEQUENCE

問題:開始偶把cache 設置爲100000000,結果,第二天來了以後,Sequence的當前值,就變成100000000。沒有設置的時候,(採用默認值)也是一樣,最後就至少設置成nocache了。根據cache的說法,不知道會不會影響速度呢?Need deep study。

三,truncate 與delete。 區別

truncate和不帶where子句的delete, 以及drop都會刪除表內的數據 。

注意:這裏說的delete是指不帶where子句的delete語句

TRUNCATE和Delete有以下幾點區別

  1、TRUNCATE在各種表上無論是大的還是小的都非常快。如果有ROLLBACK命令Delete將被撤銷,而TRUNCATE則不會被撤銷。

  2、TRUNCATE是一個DDL語言,向其他所有的DDL語言一樣,他將被隱式提交,不能對TRUNCATE使用ROLLBACK命令。

  3、TRUNCATE將重新設置高水平線和所有的索引。在對整個表和索引進行完全瀏覽時,經過TRUNCATE操作後的表比Delete操作後的表要快得多。

  4、TRUNCATE不能觸發任何Delete觸發器。

  5、不能授予任何人清空他人的表的權限。

  6、當表被清空後表和表的索引講重新設置成初始大小,而delete則不能。

  7、不能清空父表。

  TRUNCATE TABLE (schema)table_name Drop(REUSE) STORAGE

  在默認是 Drop STORAGE 當使用Drop STORAGE時將縮短表和表索引,將表收縮到最小範圍,並重新設置NEXT參數。REUSE STORAGE不會縮短表或者調整NEXT參數

  在特殊情況下使用 REUSE STORAGE

  一個實際應用的典型例子:你用sqlldr加載一個1000萬記錄的數據表,但是加載了多一半的時候你發現有問題,這個時候你想清空表重新加載。那麼最好 reuse storage ,這樣再次加載就不需要再次尋找空閒空間了。

8、truncate 只能對TABLE,delete 可以是table,view,synonym。

9、TRUNCATE TABLE 的對象必須是本模式下的,或者有drop any table的權限 而 DELETE 則是對象必須是本模式下的,或被授予 DELETE ON SCHEMA.TABLE 或DELETE ANY TABLE的權限。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章