您的位置:首页 > 数据库 > Oracle

oracle 触发器实现主键自增

2014-05-15 09:28 218 查看
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;

查询
select * from user_objects ubs where ubs.OBJECT_TYPE='SEQUENCE';
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: