| Oracle FAQ | Your Portal to the Oracle Knowledge Grid | |
|  |  | |||
Home -> Community -> Mailing Lists -> Oracle-L -> Re: Autoincrement functionality
On Tue, Aug 01, 2000 at 10:56:14AM -0800, Dave Morgan wrote:
> Hi All,
> 	I am porting a schema to Oracle from mysql that uses
> 	AUTOINCREMENT to produce the primary key.
> 
> 	In order to keep the application code the same 	I have
> 	to get a number from a sequence and insert into the primary key
> 	column at the database level. 
> 	
> 	The only way I have figured out is to use a before insert trigger
> 	something like:
> 
> 	WARNING: THIS IS UNTESTED PSUEDO CODE
> 
> 	CREATE TRIGGER AUTOINCREMENT 
>     	BEFORE  INSERT ON TABLE TEST 
>     	FOR EACH ROW 
>     	BEGIN
> 		:new.PRIMARY_KEY_ROW = TEST_NUM.nextval;
> 	END;
You should create a SEQUENCE. You can obtain a unique 
auto incrementing value by using SEQNAME.NEXTVAL:
create sequence myseq;
insert into mytab (myprimkey,...) values(myseq.nextval,....)
Received on Tue Aug 01 2000 - 23:50:34 CDT
|  |  |