1.
新建表 TEST_INCREASE;
字段为 USERID NUMBER 10
USERNAME VARCHAR2 20
2.
新建自动增长序列 SEQ_TEST_AUTOINCREASE
CREATE SEQUENCE TEST_INCREASE_SEQUENCE
INCREMENT BY 1 --每次自动增长1
START WITH 1 --从1开始计数
NOMAXVALUE --无最大值
NOCYCLE --无循环累加
CACHE 20;--缓存10个序列
3.
新建触发器TRI_TEST_INCREASE
SQL语句如下:
CREATE OR REPLACE TRIGGER "TSPTEST"."TRI_TEST_INCREASE" BEFORE INSERT ON "tsptest"."" REFERENCING OLD AS "OLD" NEW AS "NEW" FOR EACH ROW
BEGIN
SELECT SEQ_TEST_AUTOINCREASE.NEXTVAL INTO:NEW.USERID FROM DUAL;
END;
4.
测试:
反复执行
INSERT INTO TEST_INCREASE(USERNAME) VALUES('aaaaa');
然后执行
SELECT * FROM TEST_INCREASE
查看结果,结果如下: