如何在Oracle 11G数据库中为主键列设置自动增量属性
如何在oracle 11g数据库中为主键列设置自动增量属性
i使用了类似的代码
create table test_tab
(
id号主键
);
创建序列test_seq以1个增量1个nocycle开始;
在test_tab上插入之前创建或替换触发器test_trg
每一行
开始
:new.id:= test_seq.nextval;
结束;
是否有任何其他方法可以做到这一点....就像sql database。在sql中我们直接将主键列标识属性设置为是。
谢谢
121ss
how can i set auto increment property in oracle 11g database for primary key column
i have used a code like that
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
:new.id := test_seq.nextval;
end;
is there is any other method for do this....like sql database .In sql we directly set the primary key column identity property to yes .
thanks
121ss
根据您使用的Oracle DB的版本,您的方式是正确的这样做的方式。在12C之前,Oracle没有自动递增标识列的真正概念,但这非常有效。但是,从12C开始的Oracle版本中,引入了新功能。一种方法是这样的:
Depending on the version of Oracle DB you are using, the way you have done it is the correct way to do it. Prior to 12C, Oracle has no real concept of an automatically incrementing identity column, but this works perfectly well. In versions of Oracle from 12C on, though, new features were introduced. One way to do this is like this:
CREATE TABLE test_tab (
id NUMBER GENERATED ALWAYS AS IDENTITY,
something VARCHAR2(30)
);
insert into test_tab('something') values('Hi de hi');
You can get more details here[^].