Oracle实现主键自增

--建表MSHOW_ SYSUSER_INFO

create table MSHOW_ SYSUSER_INFO
(
ID int primary key,
ROLE_ID int not null,
LOGIN_NAME varchar2(50) not null,
USER_NAME varchar2(20),
USER_PHONE varchar2(20),
USER_DEPARTMENT varchar2(50),
USER_ADDR varchar2(200),
PASSWORD varchar2(50),
CREATE_DATE date,
UPDATE_DATE date,
STATUS int,
RMK varchar2(100)
)

 

--创建序列MSHOW_ SYSUSER_INFO_SEQ

CREATE SEQUENCE MSHOW_ SYSUSER_INFO_SEQ

      INCREMENT BY 1

          START WITH 1

      NOMAXVALUE

      NOCACHE

      NOCYCLE;

 

--创建触发器tri_MSHOW_ SYSUSER_INFO

create or replace trigger tri_MSHOW_ SYSUSER_INFO  
       before insert on MSHOW_ SYSUSER_INFO  
       for each row  
declare  
       myid number;  
begin  
       if 
       :new.ID is null or :new.ID='' or :new.ID=0 then  
       select MSHOW_ SYSUSER_INFO_SEQ.nextval into myid from dual;  
         
       :new.ID:=myid;  
         
       end if;  
end tri_MSHOW_ SYSUSER_INFO;

 

posted @ 2012-05-09 15:54  hibernate3例子  阅读(160)  评论(0编辑  收藏  举报