下面給出ORACLE的一種實現方式,要分2步走:
1. 建立 SEQUENCE CREATE [ OR REPLACE ] SEQUENCE sequence_identity START WITH initial seed INCREMENT BY step MAXVALUE upper bound [NOMAXVALUE] NOCYCLE [empty]
2. 建立 TRIGGER CREATE [ OR REPLACE ] TRIGGER trigger_identity BEFORE INSERT ON table_name FOR EACH ROW BEGIN SELECT sequence_identity.NEXTVAL INTO :new.column_name FROM DUAL; END;
說明:
為了盡量避免 sequence 同名沖突, sequence_identity 命名方式,建議采取
SEQ_UserName_TableName_SequenceName 的形式
trigger_identity 的命名方式,同上。