oracle自增id小记
--content
oracle 自增ID的实现 需要 序列加触发器
--step1 Create Table
Create Table Person
(
id int NOT NULL PRIMARY KEY,
uname varchar(50) NOT NULL,
upass varchar(30) NOT NULL
);
--step2 CREATE SEQUENCE
--drop SEQUENCE person_id_sequence;
CREATE SEQUENCE person_id_sequence
INCREMENT BY 1 -- 每次加几个
START WITH 1 -- 从1开始计数
NOMAXVALUE -- 不设置最大值
NOCYCLE -- 一直累加,不循环
NOCACHE -- 不建缓冲区
--step3 CREATE TRIGGER
CREATE OR REPLACE TRIGGER person_id_tri BEFORE
INSERT ON person FOR EACH ROW when (new.id is null)
begin
select person_id_sequence.nextval into :new.id from dual;
end;
--step4 test
INSERT INTO Person(uname,upass) values('qqq','wrt');
OK!