--创建一个sequence
HR@prod> create sequence test_seq
2 increment by 10
3 start with 100
4 maxvalue 200
5 minvalue 10
6 cycle
7 cache 10;
Sequence created.
--创建一个样例表
HR@prod> create table test (id number,name varchar2(20));
Table created.
--作为insert的一个value
HR@prod> insert into test values(test_seq.nextval,'mary');
1 row created.
HR@prod> select * from test;
ID NAME
---------- --------------------
100 mary
HR@prod> select test_seq.currval from dual;
CURRVAL
----------
100
HR@prod> select test_seq.nextval from dual;
NEXTVAL
----------
110
HR@prod> insert into test values(test_seq.nextval,'mike');
1 row created.
HR@prod> select * from test;
ID NAME
---------- --------------------
100 mary
120 mike
HR@prod> select test_seq.currval from dual;
CURRVAL
----------
200
--序列循环使用,到达顶点,从最小值重新开始序列。如果没有最小值,从1开始;
HR@prod> select test_seq.nextval from dual;
NEXTVAL
----------
10
*************************
HR@prod> col sequence_name for a20
HR@prod> select sequence_name,min_value,max_value,increment_by,last_number from user_sequences where sequence_name='TEST_SEQ'
SEQUENCE_NAME MIN_VALUE MAX_VALUE INCREMENT_BY LAST_NUMBER
-------------------- ---------- ---------- ------------ -----------
TEST_SEQ 10 200 10 110
HR@prod> select test_seq.currval from dual;
CURRVAL
----------
10
************
没有cache的sequence
HR@prod> create sequence test_seq2
increment by 2
start with 10
maxvalue 100
minvalue 5
cycle
nocache;
HR@prod> select test_seq2.currval from dual;
CURRVAL
----------
16
HR@prod> select sequence_name,min_value,max_value,increment_by,last_number from user_sequences where sequence_name='TEST_SEQ2'
SEQUENCE_NAME MIN_VALUE MAX_VALUE INCREMENT_BY LAST_NUMBER
-------------------- ---------- ---------- ------------ -----------
TEST_SEQ2 5 100 2 18