在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;
創建Sequence 序列 Create Sequence 你首先要有CREATE SEQUENCE或者CREATE ANY := next_id; end; 這樣你在插入數據時,不用指定你要自增的列的值 ..
id自動增長,程序裡直接賦值成null即可