在oracle数据库里创建自增ID字段的步骤
在oracle里使用自增ID字段的步骤比较繁琐,总结如下:
-- 创建表
CREATE TABLE ADVICE ( ID INT NOT NULL, ACTIVE INT DEFAULT 1 NOT NULL, TYPE INT NOT NULL, MSG VARCHAR2(512) NOT NULL, ADVICE VARCHAR2(4000) NOT NULL, PRIMARY KEY(ID), CONSTRAINT ADVICE_UNI UNIQUE(TYPE,MSG) )TABLESPACE MYDB; --创建自增ID,名称为:表名_字段名_SEQ CREATE SEQUENCE ADVICE_ID_SEQ MINVALUE 1 NOMAXVALUE INCREMENT BY 1 START WITH 1 NOCACHE; -- 为Insert操作创建触发器,无需在SQL语句里写NEXTVAL,名称为表名_INS_TRG CREATE OR REPLACE TRIGGER ADVICE_INS_TRG BEFORE INSERT ON ADVICE FOR EACH ROW WHEN(NEW.ID IS NULL) BEGIN SELECT ADVICE_ID_SEQ.NEXTVAL INTO :NEW.ID FROM DUAL; END;
相关推荐
lklong 2020-11-22
oraclemch 2020-11-06
shilukun 2020-10-10
duanqingfeng 2020-09-29
周嘉笙 2020-11-09
zhuzhufxz 2020-09-16
bfcady 2020-08-16
Hody 2020-08-16
FightFourEggs 2020-08-16
数据库设计 2020-08-16
yanghuatong 2020-08-16
dbasunny 2020-08-16
罗罗 2020-08-16
ihuaqiang 2020-08-16
choice0 2020-07-30
娜娜 2020-07-28
solarspot 2020-07-28
踩风火轮的乌龟 2020-07-26