oracle怎么用触发器实现主键自增?
1个回答
展开全部
其实现步骤可参照如下例子。
drop table book;
--创建表
create table book(
bookId varchar2(4) primary key,
name varchar2(20)
);
--创建序列
create sequence book_seq start with 1 increment by 1;
--创建触发器
create or replace trigger book_trigger
before insert on book
for each row
begin
select book_seq.nextval into :new.bookId from dual;
end ;
--添加数据
insert into book(name) values ('cc');
insert into book(name) values ('dd');
commit;
查询数据:select * from book;
drop table book;
--创建表
create table book(
bookId varchar2(4) primary key,
name varchar2(20)
);
--创建序列
create sequence book_seq start with 1 increment by 1;
--创建触发器
create or replace trigger book_trigger
before insert on book
for each row
begin
select book_seq.nextval into :new.bookId from dual;
end ;
--添加数据
insert into book(name) values ('cc');
insert into book(name) values ('dd');
commit;
查询数据:select * from book;
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询