A sequence:
Can automatically generate unique numbers
is a shareable object
Can be used to create a primary key value
Replaces application code
Speeds up the efficiency of accessing sequence values when cached in memory
CREATE SEQUENCE 序列名
[INCREMENT BY n]增长值
[START WITH n]第一个值
[(MAXVALUE n | NOMAXVALUE)]最大
[(MINVALUE n | NOMINVALUE)]是否包含最小
[(CYCLE | NOCYCLE) ]循环使用,不建议
[(CACHE n | NOCACHE) ]内存一次性装在几个,系统崩溃后出错
引用序列号NEXTVAL and CURRVAL Pseudocolun
· NEXTVAL returns the next available sequence value.It returns a unique value everytime it is referenced, even for different users.
· CURRVAL obtains the current sequence value.
· NEXTVAL must be issued for that sequence before CURRVAL contains a value.
Insert a new department named“Support”in location ID 2500:
View the current value for the DEPT_DEPTID_SEQ sequence:
Caching Sequence Values
· Caching sequence values in memory gives faster access to those values.
· Gaps in sequence values can occur when:序列号断层
—A rollback occurs
—The system crashes
—A sequence is used in another table
Guidelines for Modifying a Sequence
ALTER SEQUENCE 序列名
[INCREMENT BY n]
[START WITH n]不能修改初始值,必须先删除
[(MAXVALUE n | NOMAXVALUE)]
[(MINVALUE n | NOMINVALUE)]
[(CYCLE | NOCYCLE) ]
[(CACHE n | NOCACHE) ]
· You must be the owner or have the ALTER privilege for the sequence.
· Only future sequence numbers are affected.
· The sequence must be dropped and re-created to restart the sequence at a different number.
· Some validation is performed.
· To remove a sequence, use the DROP statement:
DROP SEQUENCE dept_dept id_seq;




