Creating Sequences in Oracle

Creating Sequences in Oracle

To create a sequence in Oracle, use the CREATE SEQUENCE statement.

CREATE OR REPLACE SEQUENCE sequence_name
START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 999
CYCLE NOCACHE

If CYCLE is specified, when maxvalue is reached, it will cycle through allocations starting with minvalue.

If the authority is insufficient, CREATE SEQUENCE authority is granted.

C:\Users\test>sqlplus /nolog

SQL*Plus: Release 11.2.0.2.0 Production on 木 9月 1 19:37:31 2016

Copyright (c) 1982, 2010, Oracle. All rights reserved.

SQL> conn / as sysdba
Connected.
SQL> GRANT CREATE ANY SEQUENCE TO USER002;

Authorization succeeded.

SQL>

Try using a sequence object.

SELECT SEQ01.CURRVAL FROM DUAL;

Then I get an error “ORA-08002: order SEQ01.CURRVAL is not yet defined for this session”. This is because I have not done NEXTVAL, so I will try NEXTVAL first.

SELECT SEQ01.NEXTVAL FROM DUAL;

The results are as follows

1

Next, let’s run CURRVAL.

SELECT SEQ01.CURRVAL FROM DUAL;

The results are as follows

1

Specify “NOORDER” in the ALTER statement below.

ALTER SEQUENCE SEQ01
INCREMENT BY 1 MINVALUE 1 MAXVALUE 10
CYCLE NOCACHE NOORDER

By specifying “NOORDER”, the numbering will not be in order.

This seems to be related to the cache.

コメント

タイトルとURLをコピーしました