2015年11月10日 星期二

[ORACLE] 11g建立sequence

簡單來說三個步驟
1. 建立TABLE
2. 建立sequence
3. 建立trigger

以下為初階範本
create table T_Owner.t_Table(
  emp_id number primary key,   --重點一定要為PK,TYPE我會用number ,比較不會怕爆掉,在SQL Server用int就有爆過的經驗
  emp_name varchar2(100)
  );

create sequence emp_id_seq
minvalue  1
maxvalue  999999999999999999999999999
start  with 1
increment  by  1
nocache; --這邊還有另外一種為cache模式,主要差別在於會不會發生跳號問題


create trigger trg_emp_id
   before insert on T_Owner.t_Table
   for each row
   begin
   select emp_id_seq.nextval
    into :new.emp_id
   from dual;
  end;
   /

進階版 http://proxy.gtn.com.tw/forum/index.php?topic=25.0

沒有留言:

張貼留言