oracle表主键id依靠触发器 sequence实现自增长
- 创建表:
create table SEND_COUNT
(
I_ID NUMBER(10) not null,
C_INDEX_CODE VARCHAR2(64) not null,
send_count number(10),
I_STATUS NUMBER(10) not null,
I_ORG_ID NUMBER(10) not null,
I_RES_TYPE INTEGER not null,
c_update_time date
)
- 创建主键及索引:
如果不创建主键,在连续插入数据时,会产生I_ID取值重复的情况
alter table SEND_COUNT
add constraint PK_SEND_COUNT primary key (I_ID)
using index
- 创建sequence
cache指定序列按组分配,每次分配后放内存中,可以加快访问速度。
create sequence SEQ_SEND_COUNT
increment by 1
start with 1
maxvalue 99999999999999999999
cache 10;
- 创建触发器自增长
create or replace trigger tr_send_count
before insert on send_count
for each row
when (new.i_id is null)
begin
select seq_send_count.nextval into :new.i_id from dual;
end;
本文详细介绍如何在Oracle数据库中使用触发器和sequence实现表ID的自增长,包括创建表、主键、sequence和触发器的具体步骤。
1875

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



