Oracle ID 自增

1.创建表
-- Create table
create table USERS
(
  ID       NUMBER not null,
  USERNAME VARCHAR2(25),
  PASSWORD VARCHAR2(25),
  EMAIL    VARCHAR2(50)
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );
-- Create/Recreate primary, unique and foreign key constraints 
alter table USERS
  add constraint ID primary key (ID)
  using index 
  tablespace USERS
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );


2.创建序列

CREATE SEQUENCE SEQ_USERS_ID 
INCREMENT BY 1 -- 每次加几个 
START WITH 1 -- 从1开始计数 
NOMAXVALUE -- 不设置最大值 
NOCYCLE -- 一直累加,不循环 
CACHE 10; 


3.创建触发器
create or replace trigger TRI_USERS_ID  
   before insert on users    
   for each row  
declare  
   -- local variables here  
begin  
   SELECT SEQ_USERS_ID.NEXTVAL  
   INTO :NEW.ID  
   FROM DUAL;  
end TRI_USERS_ID;
 

你可能感兴趣的:(oracle,sql,cache)