1.如何查看sequence的DDL語句
SQL>SELECT DBMS_METADATA.GET_DDL('SEQUENCE','SEQ_SMECUSTPERIODINFO','CLMS01') FROM DUAL; --第一個是類型,第二個是sequence的名字,第三個是sequence的owner
CREATE SEQUENCE "CLMS01"."SEQ_SMECUSTPERIODINFO" MINVALUE 1 MAXVALUE 999999
9999 INCREMENT BY 1 START WITH 8798675575 CACHE 20 ORDER NOCYCLE --START WITH 8798675575 此值為dba_sequneces中的LAST_NUMBER,比較奇怪!!
2.dba_sequences相關字的定義
Column | Datatype | NULL | Description |
---|---|---|---|
SEQUENCE_OWNER |
VARCHAR2(30) |
NOT NULL |
Owner of the sequence |
SEQUENCE_NAME |
VARCHAR2(30) |
NOT NULL |
Sequence name |
MIN_VALUE |
NUMBER |
Minimum value of the sequence | |
MAX_VALUE |
NUMBER |
Maximum value of the sequence | |
INCREMENT_BY |
NUMBER |
NOT NULL |
Value by which sequence is incremented |
CYCLE_FLAG |
VARCHAR2(1) |
Indicates whether the sequence wraps around on reaching the limit (Y ) or not (N ) |
|
ORDER_FLAG |
VARCHAR2(1) |
Indicates whether sequence numbers are generated in order (Y ) or not (N ) |
|
CACHE_SIZE |
NUMBER |
NOT NULL |
Number of sequence numbers to cache |
LAST_NUMBER |
NUMBER |
NOT NULL |
Last sequence number written to disk. If a sequence uses caching, the number written to disk is the last number placed in the sequence cache. This number is likely to be greater than the last sequence number that was used. |
- CYCLE_FLAG:標志sequence是否循環使用(到最大值之后變為最小值)
- ORDER_FLAG:標志sequence是否是按照順序生成。
- CACHE_SIZE:標志內存中緩存的sequence個數(保存在shared pool中),設置較小時會出現row cache lock的等待事件。
- LAST_NUMBER:上一個寫入到disk的sequence number。
3.如何修改sequence
修改cache,連接到sequence的owner,執行以下語句,調整cache值,其他的修改類似。
SQL>alter sequence cache 1000;
修改maxvalue值:
SQL> alter sequence testeq maxvalue 9999999999;
Sequence altered.
修改order、noorder值:
SQL> alter sequence testeq order/noorder;
Sequence altered.
4.sequence的使用
情景一:直接調用
****************************
創建序列
****************************
create sequence testseq
increment by 1
start with 900000
maxvalue 999999999
cycle
cache 200;
****************************
創建存儲過程
****************************
create or replace procedure proc_arg
as
begin
insert into test
(object_id,object_name,owner)
values
(testseq.nextval,'dayu','dayu');
commit;
end;
情景二:創建trigger,當值為空時插入sequence
create sequence testseq
increment by 1
start with 900000
maxvalue 999999999
cycle
cache 200;
create or replace trigger test_trigger
before insert on test
for each row
declare
tempkey test.object_id%type;
begin
select testseq.NEXTVAL into tempkey from dual;
if :new.object_id is null then
:new.object_id := tempkey+1;
end if;
end;
DECLARE
i number := 0;
BEGIN
for i in 90000 .. 200000 loop
if mod(i,2)=0 then
insert into test
(object_name, owner)
values
('dayu','dayu');
commit;
end if;
end loop;
END;
DECLARE
i number := 0;
BEGIN
for i in 90000 .. 200000 loop
if mod(i,2)=1 then
insert into test
(object_name, owner)
values
('dayu', 'dayu');
commit;
end if;
end loop;
END;