官方文档地址:http://docs.oracle.com/cd/B19306_01/server.102/b14231/views.htm#i1106548
Creating Sequences
CREATESEQUENCE
system privilege. To create a sequence in anotheruser's schema, you must have the CREATE ANY SEQUENCE
privilege.
Create a sequence usingthe CREATE SEQUENCE
statement. For example, thefollowing statement creates a sequence used to generate employeenumbers for the empno
column of the emp
table:
CREATE SEQUENCE emp_sequence INCREMENT BY 1 START WITH 1 NOMAXVALUE NOCYCLE CACHE 10;
NOCYCLE
option indicatesthat the sequence cannot generate more values after reaching itsmaximum or minimum value.
CACHE
clause preallocates a set of sequencenumbers and keeps them in memory so that sequence numbers can beaccessed faster. When the last of the sequence numbers in the cachehas been used, the database reads another set of numbers into thecache.
SHUTDOWN ABORT
statement is issued), sequence numbersthat have been cached but not used are lost. Also, sequence numbersthat have been used but not saved are lost as well. The databasemight also skip cached sequence numbers after an export and import.See OracleDatabase Utilities for details.
---下边是创建约束的语法:
文档地址:http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_6015.htm#SQLRF01314
Specify the schema to contain the sequence. If you omitschema
,then Oracle Database creates the sequence in your own schema.
Specify the name of the sequence to be created.
If you specify none of the following clauses, then you create anascending sequence that starts with 1 and increases by 1 with noupper limit. Specifying only INCREMENT
BY
-1 creates a descending sequence that starts with -1 and decreaseswith no lower limit.
-
Tocreate a sequence that increments without bound, for ascendingsequences, omit the
MAXVALUE
parameter or specifyNOMAXVALUE
. For descending sequences, omit theMINVALUE
parameter or specify theNOMINVALUE
. -
To create a sequence that stops at a predefined limit, for anascending sequence, specify a value for the
MAXVALUE
parameter. For a descending sequence, specify a value for theMINVALUE
parameter. Also specifyNOCYCLE
.Any attempt to generate a sequence number once the sequence hasreached its limit results in an error. -
To create a sequence that restarts after reaching apredefined limit, specify values for both the
MAXVALUE
andMINVALUE
parameters. Also specifyCYCLE
. If you do not specifyMINVALUE
,then it defaults toNOMINVALUE
, which is the value1.
INCREMENTBY
This integer value can be any positive or negativeinteger, but it cannot be 0.
--这个整数值可以是任意的正数或者负数,但不能是0.
降序的产生就是由于参数INCREMENTBY可以为正数或者负数导致。
This value can have 28 or fewer digits. The absolute ofthis value must be less than the difference ofMAXVALUE
and MINVALUE
. If this value isnegative, then the sequence descends. If the value is positive,then the sequence ascends. If you omit thisclause, then the interval defaults to 1.
STARTWITH
Note:
This value is not necessarily the value to which an ascendingcycling sequence cycles after reaching its maximum or minimumvalue.MAXVALUE MAXVALUE
must be equal to orgreater than START
WITH
and must begreater than MINVALUE
.
NOMAXVALUE NOMAXVALUE
to indicate a maximum value of1027 for an ascending sequence or -1 for a descendingsequence. This is the default.
MINVALUE MINVALUE
must be less than or equalto START
WITH
and must be less thanMAXVALUE
.
NOMINVALUE NOMINVALUE
to indicate a minimum value of 1 for anascending sequence or -1026 for a descending sequence.This is the default.
CYCLE CYCLE
to indicate that the sequence continues togenerate values after reaching either its maximum or minimumvalue.
After an ascending sequence reaches its maximum value,it generates its minimum value.--在一个升序序列到达它的最大值之后,它将产生最小值。
After a descending sequence reaches its minimum, itgenerates its maximum value.
NOCYCLE NOCYCLE
to indicate that the sequence cannot generatemore values after reaching its maximum or minimum value. This isthe default.
CACHE
You cannot cache more values than will fit in a givencycle of sequence numbers. Therefore, the maximum value allowed forCACHE
must be less than the value determined by thefollowing formula:
(CEIL (MAXVALUE - MINVALUE)) / ABS (INCREMENT)
If a system failure occurs, all cached sequence valuesthat have not been used in committed DML statements are lost. Thepotential number of lost values is equal to the value of theCACHE
parameter.
--如果系统错误发生,在已提交的DML语句中还没使用的所有缓存的序列值都将丢失。丢失值的可能数量和CACHE参量的值相等。
Note:
Oracle recommends using theCACHE
setting to enhanceperformance if you are using sequences in a Real ApplicationClusters environment.
NOCACHE NOCACHE
to indicate that values of the sequence arenot preallocated. If you omit both CACHE
andNOCACHE
, the database caches 20 sequence numbers bydefault.
--下边是怎么引用一个序列
文档地址:http://docs.oracle.com/cd/B19306_01/server.102/b14231/views.htm#sthref3081
Referencing a Sequence
A sequence is referenced in SQL statements with theNEXTVAL
and CURRVAL
pseudocolumns; eachnew sequence number is generated by a reference to the sequencepseudocolumn NEXTVAL
, while the current sequencenumber can be repeatedly referenced using the pseudo-columnCURRVAL
.
NEXTVAL
and CURRVAL
are not reservedwords or keywords and can be used as pseudocolumn names in SQLstatements such as SELECT
, INSERT
, orUPDATE
.
Generating Sequence Numbers with NEXTVAL
To generate and use a sequence number, referenceseq_name.NEXTVAL
. For example, assume acustomer places an order. The sequence number can be referenced ina values list. For example:
INSERT INTO Orders_tab (Orderno, Custno) VALUES (Order_seq.NEXTVAL, 1032);
Or, the sequence number can be referenced in theSET
clause of an UPDATE
statement. Forexample:
UPDATE Orders_tab SET Orderno = Order_seq.NEXTVAL WHERE Orderno = 10112;
The sequence number can also be referenced outermostSELECT
of a query or subquery. For example:
SELECT Order_seq.NEXTVAL FROM dual;
As defined, the first reference toorder_seq.NEXTVAL
returns the value 1. Each subsequentstatement that references order_seq.NEXTVAL
generatesthe next sequence number (2, 3, 4,. . .). The pseudo-columnNEXTVAL
can be used to generate as many new sequencenumbers as necessary. However, only a single sequence number can begenerated for each row. In other words, if NEXTVAL
isreferenced more than once in a single statement, then the firstreference generates the next number, and all subsequent referencesin the statement return the same number.
Once a sequence number is generated, the sequence number isavailable only to the session that generated the number.Independent of transactions committing or rolling back, other usersreferencing order_seq.NEXTVAL
obtain unique values. Iftwo users are accessing the same sequence concurrently, then thesequence numbers each user receives might have gaps becausesequence numbers are also being generated by the other user.
Using Sequence Numbers with CURRVAL
To use or refer to thecurrent sequence value of your session, referenceseq_name.CURRVAL
. CURRVAL
can only be used if seq_name.NEXTVAL
hasbeen referenced in the current user session (in the current or aprevious transaction). CURRVAL
can be referenced asmany times as necessary, including multiple times within the samestatement. The next sequence number is not generated untilNEXTVAL
is referenced. Continuing with the previousexample, you would finish placing the customer's order by insertingthe line items for the order:
INSERT INTO Line_items_tab (Orderno, Partno, Quantity) VALUES (Order_seq.CURRVAL, 20321, 3); INSERT INTO Line_items_tab (Orderno, Partno, Quantity) VALUES (Order_seq.CURRVAL, 29374, 1);
Assuming the INSERT
statement given in the previoussection generated a new sequence number of 347, both rows insertedby the statements in this section insert rows with order numbers of347.
Uses and Restrictions of NEXTVAL and CURRVAL
CURRVAL
and NEXTVAL
can be used in thefollowing places:
-
VALUES
clause ofINSERT
statements -
The
SELECT
list of aSELECT
statement -
The
SET
clause of anUPDATE
statement
CURRVAL
and NEXTVAL
cannot be used inthese places:
-
A subquery
-
A view query or materialized view query
-
A
SELECT
statement with theDISTINCT
operator -
A
SELECT
statement with aGROUP
BY
orORDER
BY
clause -
A
SELECT
statement that is combined with anotherSELECT
statement with theUNION,
INTERSECT
, orMINUS
set operator -
The
WHERE
clause of aSELECT
statement -
DEFAULT
value of a column in aCREATE
TABLE
orALTER
TABLE
statement -
The condition of a
CHECK
constraint