oracle 使用触发器更新插入列的id

来源:互联网 发布:深圳壹基金知乎 编辑:程序博客网 时间:2024/06/06 09:42

创建表book

create table book(

 id int primary key,
 name varchar2(50) not null
);


创建序列

create sequence seq_book
start with 1

increment by 1;


创建触发器

create or replace trigger tri_book_id
before insert on book
for each row 
  declare 
  --local variables here
  begin
    select seq_book.nextval into:NEW.Id from dual;
    end tri_book_id;
    

     

激活book表上所有的触发器 

ALTER TABLE book ENABLE ALL TRIGGERS; 或 重新编译触发器   ALTER TRIGGER tri_book_id COMPILE;commit;

    

插入两列
insert into book(name) values ('jake');
insert into book(name) values ('kate');




select * from dd;
0 0