本文简单介绍Oracle自增字段创建的一种方法。
描述:将测试记录表pipe_test的字段id设置为自增字段。
- pipe_test的表结构
SQL> desc pipe_test;
Name Type Nullable Default Comments
-------- ------------- -------- ------- --------
ID NUMBER(10)
CONTENT VARCHAR2(256) '_'
DATETIME DATE sysdate - 创建序列
create sequence seq_inc_pipe
start with 1
increment by 1
cache 20;
start with 1
increment by 1
cache 20;
- 创建触发器
create or replace trigger trg_pipe_test_id
before insert on pipe_test
for each row
begin
select seq_inc_pipe.nextval into :new.id from dual;
end trg_pipe_test_id;
before insert on pipe_test
for each row
begin
select seq_inc_pipe.nextval into :new.id from dual;
end trg_pipe_test_id;
- 测试
insert into pipe_test values(1,'test',sysdate);
本文介绍了一种在Oracle数据库中实现自增字段的方法,通过创建序列和触发器的方式,使得pipe_test表的id字段能够在每次插入新记录时自动递增。
3万+

被折叠的 条评论
为什么被折叠?



