在oracle中不像mysql可以id自增,所以要通过oracle的触发器实现主键的自增。

1.下面是表结构,原来是没有主键自增的

 

2.将QUESTION_ID设为主键

alter table CHK_T_QUESTION_DTL(表名) add constraint QUESTION_ID(字段) primary key(QUESTION_ID);

3.设值序列化

create sequence SEQ_QUESTION_ID(序列化名)
minvalue 1
maxvalue 99999999
start with 1
increment by 1
cache 50;

4.创建触发器

create or  replace trigger tr_question_dtl(触发器名字)

before insert on CHK_T_QUESTION_DTL(表名)

for each row

begin

select SEQ_QUESTION_ID(序列化名).nextval into :new.QUESTION_ID(自增的字段名) from dual;

end;

下面进行测试

 

posted on 2021-04-12 16:46  StephenZYH  阅读(3219)  评论(0)    收藏  举报