清空資料表truncate table TABLE_NAME;
設定Auto Increment
參考資料:
http://www.lifeaftercoffee.com/2006/02/17/how-to-create-auto-increment-columns-in-oracle/
先建立一個sequence
create sequence SEQUENCE_NAME再建立一個trigger
start with 1
increment by 1
nocache;
create or replace trigger TRIGGER_NAME這樣就完成了
before insert
on TABLE_NAME
referencing NEW as NEW
for each row
begin
select SEQUENCE_NAME.nextval into :NEW.AUTO_INCREMENT_COLUMN from dual;
end;
/
SEQUENCE有下列參數:MINVALUE、MAXVALUE、START WITH、INCREMENT BY、CACHE、NOCACHE、CYCLE
要加nocache,sequence才不會亂跳
SEQUENCE到達最大值之前不能重新計數,只能用DROP刪除後再重新CREATE建立。
可以用ALTER來修改SEQUENCE的參數
ALTER SEQUENCE seq01用NEXTVAL取得下一個值
INCREMENT BY 100;
SELECT seq01.NEXTVAL FROM DUAL;
用CURRVAL取的目前值
SELECT seq01.CURRVAL FROM DUAL;
沒有留言:
張貼留言