为实现表的主键自增长,创建序列和触发器

时间:2020-12-18 05:06:27

CREATE SEQUENCE INNERID 
MINVALUE 1 
MAXVALUE 99999999999999 
START WITH 1   INCREMENT BY 1 
CACHE 20 
ORDER; 

CREATE OR REPLACE TRIGGER EMP_TG 
BEFORE INSERT ON employee FOR EACH ROW 
BEGIN 
SELECT INNERID.NEXTVAL INTO :NEW.EMID FROM DUAL; 
END; 


insert into employee(emname,emage,emsex) values('dd',40,1)
若不创建触发器,使用序列进行自增长,sql语句应这么写
insert into employee(emid,emname,emage,emsex) values(INNERID.NEXTVAL,'aa',32,2)