Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Mailing Lists -> Oracle-L -> RE: Table autonumber field
> -----Original Message-----
> From: Mike T [mailto:mtmind_at_the-beach.net]
>
> I need to create an autonumber field in an Oracle Table (from
> a SQL server table identity field) , I was looking for a
> default value that I could assign (select max(ID) + 1) or
> some way to create an identity field
> or use a trigger after insert but nothing seems to work for
> me. Any help would be appreciated, This field also has to be
> the primary Key .
Read the Oracle SQL manual about the use of sequences and triggers. Here's an example: SQL> create sequence s ;
Sequence created.
SQL> create table t (id number primary key, name varchar2 (30)) ;
Table created.
SQL> create trigger b4it
2 before insert on t 3 for each row 4 begin 5 select s.nextval into :new.id from dual ; 6 end ; 7 /
Trigger created.
SQL> insert into t (name) values ('Mao Chu') ;
1 row created.
SQL> insert into t (name) values ('Mohandas Gandhi') ;
1 row created.
SQL> commit ;
Commit complete.
SQL> select * from t ;
ID NAME
---------- ------------------------------1 Mao Chu