日期:2014-05-16  浏览次数:20771 次

DB2中的sequence定义和使用

--第一种方法(自己使用过的,绿色安全无污染):

创建sequence,名为SEQ_LDBG? ,自增1,

CREATE SEQUENCE? SEQ_LDBG? AS INT START WITH 1 INCREMENT BY 1 MINVALUE 1???
?????? NO MAXVALUE NO CYCLE NO CACHE ORDER;?

--在sql中nextval for SEQ_LDBG这样使用,codechange7为表名,

select? nextval for SEQ_LDBG, code,name from codechange7;

?

?

---第二种方法:

CREATE SEQUENCE? SEQ_LDBG? START WITH 1 INCREMENT BY 1 NOMAXVALUE NOCYCLE CACHE 24;
--获取下一个值

select nextval for SEQ_LDBG?? from codechange7;

--获取当前值

select prevval for SEQ_LDBG?? from codechange7;

--插入记录

?

insert into codechange7(id) values ( NEXTVAL FOR? id)

?

--删除sequence

?? DROP SEQUENCE?? SEQ_LDBG???;?--序列设置开始值
?????ALTER SEQUENCE 序列名? (SEQ_LDBG???) RESTART WITH 下一值(1)

--修改
?修改最大值:?? ALTER SEQUENCE <sequence_name> MAX VALUE <numeric-constant> | NO MAXVALUE
?修改最小值:?? ALTER SEQUENCE <sequence_name> MIN VALUE <numeric-constant> | NO MINVALUE

?

(此值需要比当前值小)
?修改步长:??? ALTER SEQUENCE <sequence_name> INCREMENT BY <numeric-constant>;
?修改CACHE值: ALTER SEQUENCE <sequence_name> CACHE <numeric-constant> | NO CACHE
?修改循环属性: ALTER SEQUENCE <sequence_name> <CYCLE | NO CYCLE>
?修改排序属性:ALTER SEQUENCE <sequence_name> <ORDER | NO ORDER>
?从新计数:??? ALTER SEQUENCE <sequence_name> RESTART | RESTART WITH <numeric-constant>

?

?

?

?

?