sequence is used to generate a unique integer database objects in a multiuser environment. The sequence generator
generates sequential numbers, which can be used to automatically generate primary key values, and to coordinate multi-line or multi-table's primary key operation.
No sequence, the value of the order can only be generated by
programming.
To create a sequence in your own schema, you must have the CREATE SEQUENCE system privilege.
To create a sequence in another user's schema,you must have the CREATE ANY SEQUENCE system privilege.
Formats of a sequence is:
create sequence sequence_name
minvalue or nominvalue
maxvalue or nomaxvalue
start with
increment by
cycle or nocycle
cache
note:nomaxvalue,nominvalue,nocycle can default.
cache max vulue is equal to (ceil(maxvalue - minvalue)) / abs (increment).
*example*
If a sequence has been created,then we can get the nextVal.
At the first time NEXTVAL return initial,then NEXTVAL will increase by you have defined INCREMENT BY value,and
return the increased value.
1.
drop sequence SEQ_ID;
create sequence SEQ_ID
minvalue 1
maxvalue 5
start with 1
increment by 1
nocache
order;
SELECT SEQ_ID.nextval FROM dual;
NEXTVAL
----------
1
2.
begin
for i in 1..5 loop
insert into stu(id)values(SEQ_ID.Nextval);
end loop;
end;
/
select * from stu;
ID
----------
1
2
3
4
5
we can use sequence in the following situations:
1.SELECT with no subquery ,snapshot,VIEW.
2.INSERT with subquery.
3.INSERT with values.
4.UPDATE with set.