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

比如有一个表 test 两个字段 id和name 其中id是number型自增的 使用插入语句insert into test values(1,‘sss’);自然可以实现插入 但是我想values中不用写id字段 直接写name字段 比如values(‘ssss’) 就向test表中插入了数据 sql语句该怎么写呢?

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
);
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-09-04
使用触发器
第2个回答  2013-09-03
insert into test(name) values(‘sss’)
相似回答