一、序列app
1.1 序列的定義和做用less
Use the CREATE
SEQUENCE
statement to create a sequence, which is a database object from which multiple users may generate unique integers. You can use sequences to automatically generate primary key values.ide
When a sequence number is generated, the sequence is incremented, independent of the transaction committing or rolling back. If two users concurrently increment the same sequence, then the sequence numbers each user acquires may have gaps, because sequence numbers are being generated by the other user. One user can never acquire the sequence number generated by another user. After a sequence value is generated by one user, that user can continue to access that value regardless of whether the sequence is incremented by another user.ui
Sequence numbers are generated independently of tables, so the same sequence can be used for one or for multiple tables. It is possible that individual sequence numbers will appear to be skipped, because they were generated and used in a transaction that ultimately rolled back. Additionally, a single user may not realize that other users are drawing from the same sequence.this
After a sequence is created, you can access its values in SQL statements with the CURRVAL
pseudocolumn, which returns the current value of the sequence, or the NEXTVAL
pseudocolumn, which increments the sequence and returns the new valuecode
1.2 在哪裏使用序列和哪裏不能使用 Where to Use Sequence Valuesip
You can use CURRVAL
and NEXTVAL
in the following locations:ci
The select list of a SELECT
statement that is not contained in a subquery, materialized view, or viewrem
The select list of a subquery in an INSERT
statementinput
The VALUES
clause of an INSERT
statement
The SET
clause of an UPDATE
statement
Restrictions on Sequence Values You cannot use CURRVAL
and NEXTVAL
in the following constructs:
A subquery in a DELETE
, SELECT
, or UPDATE
statement
A query of a view or of a materialized view
A SELECT
statement with the DISTINCT
operator
A SELECT
statement with a GROUP
BY
clause or ORDER
BY
clause
A SELECT
statement that is combined with another SELECT
statement with the UNION
, INTERSECT
, or MINUS
set operator
The WHERE
clause of a SELECT
statement
The DEFAULT
value of a column in a CREATE
TABLE
or ALTER
TABLE
statement
The condition of a CHECK
constraint
Within a single SQL statement that uses CURRVAL
or NEXTVAL
, all referenced LONG
columns, updated tables, and locked tables must be located on the same database.
1.3 何時序列增加 Within a single SQL statement containing a reference to NEXTVAL
, Oracle increments the sequence once:
For each row returned by the outer query block of a SELECT
statement. Such a query block can appear in the following places:
A top-level SELECT
statement
An INSERT
... SELECT
statement (either single-table or multitable). For a multitable insert, the reference to NEXTVAL
must appear in the VALUES
clause, and the sequence is updated once for each row returned by the subquery, even though NEXTVAL
may be referenced in multiple branches of the multitable insert.
A CREATE
TABLE
... AS
SELECT
statement
A CREATE
MATERIALIZED
VIEW
... AS
SELECT
statement
For each row updated in an UPDATE
statement
For each INSERT
statement containing a VALUES
clause
For each INSERT
... [ALL
| FIRST
] statement (multitable insert). A multitable insert is considered a single SQL statement. Therefore, a reference to the NEXTVAL
of a sequence will increase the sequence only once for each input record coming from the SELECT
portion of the statement. If NEXTVAL
is specified more than once in any part of the INSERT
... [ALL
|FIRST
] statement, then the value will be the same for all insert branches, regardless of how often a given record might be inserted.
For each row merged by a MERGE
statement. The reference to NEXTVAL
can appear in the merge_insert_clause
or the merge_update_clause
or both. The NEXTVALUE
value is incremented for each row updated and for each row inserted, even if the sequence number is not actually used in the update or insert operation. If NEXTVAL
is specified more than once in any of these locations, then the sequence is incremented once for each row and returns the same value for all occurrences of NEXTVAL
for that row.
For each input row in a multitable INSERT
ALL
statement. NEXTVAL
is incremented once for each row returned by the subquery, regardless of how many occurrences of theinsert_into_clause
map to each row.
1.4 序列的一些特殊狀況
If any of these locations contains more than one reference to NEXTVAL
, then Oracle increments the sequence once and returns the same value for all occurrences of NEXTVAL
.
If any of these locations contains references to both CURRVAL
and NEXTVAL
, then Oracle increments the sequence and returns the same value for both CURRVAL
and NEXTVAL
.
二、視圖 view
2.1 視圖的一些特殊狀況
Rows cannot be deleted through a view if the view definition contains the DISTINCT keyword.
三、約束狀態 Constraint State
3.1 DEFERRABLE
DEFERRABLE Clause The DEFERRABLE
and NOT
DEFERRABLE
parameters indicate whether or not, in subsequent transactions, constraint checking can be deferred until the end of the transaction using the SET
CONSTRAINT
(S
) statement. If you omit this clause, then the default is NOT
DEFERRABLE
.
Specify NOT
DEFERRABLE
to indicate that in subsequent transactions you cannot use the SET
CONSTRAINT
[S
] clause to defer checking of this constraint until the transaction is committed. The checking of a NOT
DEFERRABLE
constraint can never be deferred to the end of the transaction.
If you declare a new constraint NOT
DEFERRABLE
, then it must be valid at the time the CREATE
TABLE
or ALTER
TABLE
statement is committed or the statement will fail.
Specify DEFERRABLE
to indicate that in subsequent transactions you can use the SET
CONSTRAINT
[S
] clause to defer checking of this constraint until a COMMIT
statement is submitted. If the constraint check fails, then the database returns an error and the transaction is not committed. This setting in effect lets you disable the constraint temporarily while making changes to the database that might violate the constraint until all the changes are complete.
You cannot alter the deferrability of a constraint. Whether you specify either of these parameters, or make the constraint NOT
DEFERRABLE
implicitly by specifying neither of them, you cannot specify this clause in an ALTER
TABLE
statement. You must drop the constraint and re-create it.
指定爲DEFERRABLE才能使用INITIALLY clause,它指定CREATE 或ALTER 語句是否執行檢查
INITIALLY Clause The INITIALLY
clause establishes the default checking behavior for constraints that are DEFERRABLE
. The INITIALLY
setting can be overridden by a SET
CONSTRAINT
(S
) statement in a subsequent transaction.
Specify INITIALLY
IMMEDIATE
to indicate that Oracle should check this constraint at the end of each subsequent SQL statement. If you do not specify INITIALLY
at all, then the default is INITIALLY
IMMEDIATE
.
If you declare a new constraint INITIALLY
IMMEDIATE
, then it must be valid at the time the CREATE
TABLE
or ALTER
TABLE
statement is committed or the statement will fail.
Specify INITIALLY
DEFERRED
to indicate that Oracle should check this constraint at the end of subsequent transactions.
This clause is not valid if you have declared the constraint to be NOT
DEFERRABLE
, because a NOT
DEFERRABLE
constraint is automatically INITIALLY
IMMEDIATE
and cannot ever be INITIALLY
DEFERRED
.