oracle中有一个表的id是number型自增的 insert时如何不用插入id 只插入其他字段就实现数据的插入

2024-12-22 20:20:19
推荐回答(3个)
回答1:

oracle 12c之前必须用sequence :

create table test_tab
(
id number primary key
);

create sequence test_seq start with 1 increment by 1 nocycle;

create or replace trigger test_trg
before insert on test_tab
for each row
begin
select test_seq.nextval into :new.id
from dual;
end;
/

12c之后可以直接定义 字段的默认值为一个自增序列

create sequence test_seq start with 1 increment by 1 nocycle;

create table test_tab
(
id number default test_seq.nextval primary key
);

回答2:

使用触发器

回答3:

insert into test(name) values(‘sss’)