oracle数据库设置主键自增:

--创建表
create table blog(
id integer primary key,
title varchar2(200),
content varchar2(20000),
user_id varchar2(45),
pub_date date);

--创建sequence:
create sequence blog_id_sequence
increment by 1
start with 1
nomaxvalue
nocache;

--创建触发器:
create trigger blog_id_autoincrement before 
insert on blog for each row 
when (new.id is null)
begin 
select blog_id_sequence.nextval into:new.id from dual;
end;


注意,如果 select 。。。into后面少了:,会导致触发器失效且无法通过验证
posted @ 2024-09-23 18:09  未来的羁绊  阅读(147)  评论(0编辑  收藏  举报