oracle 实现ID自增

时间:2022-04-23 02:09:47
CREATE TABLE testTable1
(
ID INT NOT NULL,
NAME VARCHAR2(4000) NOT NULL,
PRIMARY KEY(ID)
)
TABLESPACE MYDB; --创建自增ID,名称为:表名_字段名_SEQ
CREATE SEQUENCE testTable1_ID_SEQ MINVALUE 1 NOMAXVALUE INCREMENT BY 1 START WITH 1 NOCACHE; -- 为Insert操作创建触发器,无需在SQL语句里写NEXTVAL,名称为表名_INS_TRG
CREATE OR REPLACE TRIGGER testTable1_INS_TRG BEFORE INSERT ON testTable1 FOR EACH ROW WHEN(NEW.ID IS NULL)
BEGIN
SELECT testTable1_ID_SEQ.NEXTVAL INTO :NEW.ID FROM DUAL;
END; --创建自增ID,名称为:表名_字段名_SEQ
CREATE SEQUENCE lz_Cbenefititem_ID_SEQ MINVALUE 1 NOMAXVALUE INCREMENT BY 1 START WITH 1 NOCACHE; -- 为Insert操作创建触发器,无需在SQL语句里写NEXTVAL,名称为表名_INS_TRG
CREATE OR REPLACE TRIGGER lz_Cbenefititem_pro_INS_TRG BEFORE INSERT ON lz_claimbased_benefititem_pro FOR EACH ROW WHEN(NEW.benefititemno IS NULL)
BEGIN
SELECT lz_Cbenefititem_ID_SEQ.NEXTVAL INTO :NEW.benefititemno FROM DUAL;
END; --创建自增ID,名称为:表名_字段名_SEQ
CREATE SEQUENCE lz_Cclassify_ID_SEQ MINVALUE 1 NOMAXVALUE INCREMENT BY 1 START WITH 1 NOCACHE; -- 为Insert操作创建触发器,无需在SQL语句里写NEXTVAL,名称为表名_INS_TRG
CREATE OR REPLACE TRIGGER lz_CClassify_pro_INS_TRG BEFORE INSERT ON lz_claimbased_classify_pro FOR EACH ROW WHEN(NEW.benefitsclassifyno IS NULL)
BEGIN
SELECT lz_Cclassify_ID_SEQ.NEXTVAL INTO :NEW.benefitsclassifyno FROM DUAL;
END; --创建自增ID,名称为:表名_字段名_SEQ
CREATE SEQUENCE lz_Cdeductible_ID_SEQ MINVALUE 1 NOMAXVALUE INCREMENT BY 1 START WITH 1 NOCACHE; -- 为Insert操作创建触发器,无需在SQL语句里写NEXTVAL,名称为表名_INS_TRG
CREATE OR REPLACE TRIGGER lz_CDeductible_pro_INS_TRG BEFORE INSERT ON lz_claimbased_deductible_pro FOR EACH ROW WHEN(NEW.deductibleno IS NULL)
BEGIN
SELECT lz_Cdeductible_ID_SEQ.NEXTVAL INTO :NEW.deductibleno FROM DUAL;
END; --创建自增ID,名称为:表名_字段名_SEQ
CREATE SEQUENCE lz_Cmaximum_ID_SEQ MINVALUE 1 NOMAXVALUE INCREMENT BY 1 START WITH 1 NOCACHE; -- 为Insert操作创建触发器,无需在SQL语句里写NEXTVAL,名称为表名_INS_TRG
CREATE OR REPLACE TRIGGER lz_Cmaximum_pro_INS_TRG BEFORE INSERT ON lz_claimbased_maximum_pro FOR EACH ROW WHEN(NEW.outofpocketmaximumno IS NULL)
BEGIN
SELECT lz_Cmaximum_ID_SEQ.NEXTVAL INTO :NEW.outofpocketmaximumno FROM DUAL;
END; --创建自增ID,名称为:表名_字段名_SEQ
CREATE SEQUENCE lz_Cbenefits_ID_SEQ MINVALUE 1 NOMAXVALUE INCREMENT BY 1 START WITH 1 NOCACHE; -- 为Insert操作创建触发器,无需在SQL语句里写NEXTVAL,名称为表名_INS_TRG
CREATE OR REPLACE TRIGGER lz_Cbenefits_pro_INS_TRG BEFORE INSERT ON lz_claimbased_benefits_pro FOR EACH ROW WHEN(NEW.benefitsno IS NULL)
BEGIN
SELECT lz_Cbenefits_ID_SEQ.NEXTVAL INTO :NEW.benefitsno FROM DUAL;
END;