Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 7-Jan-14 23:42pm
v2

1 solution

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:
SQL
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[^].
 
Share this answer
 
Comments
121ss 8-Jan-14 6:24am    
ok thanks pete O'Hanion
Jörgen Andersson 8-Jan-14 7:36am    
I'll stay with Sequences for now, but good to know.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900