Oracle数据库实现ID主键自增

1.创建自增序列

CREATE SEQUENCE "***"."DEMO_ID_ADD"
 INCREMENT BY 1
 MINVALUE 1
 MAXVALUE 9999
 START WITH 1;

2.创建触发器

CREATE OR REPLACE TRIGGER "***"."DEMO_ID_Tigger" 
BEFORE INSERT ON "***"."DEMO" FOR EACH ROW ENABLE
BEGIN
  SELECT DEMO_ID_ADD.nextval INTO :new."DEMO_ID" from dual;
END DEMO_ID_Tigger;

注意:在使用PL/SQL或者Navicat创建触发器的时候,new."DEMO_ID"的DEMO_ID必须加上 “” ,否则会提示错误的赋值变量new.DEMO_ID.

你可能感兴趣的:(Oracle数据库实现ID主键自增)