Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Please help with no-gap autoincrement field

Please help with no-gap autoincrement field

From: <g_chime_at_yahoo.com>
Date: 13 Jun 2006 09:54:41 -0700
Message-ID: <1150217681.391590.197490@y43g2000cwc.googlegroups.com>


I am converting from MySQL to ORACLE and having problems with an autoincrement field.
MySQL has a built-in autoincrement feature ORACLE doesn't seem to.

I am using a sequence and a "before insert" trigger (see code below).

The problem is that when an insert fails the sequence still increments, so there will be gaps.
This is a data warehouse and rows are never deleted, and the autoincrement field should have no gaps.

I have tried to change "before insert" to "after insert" but that will result in an error (ORA-04084: cannot change NEW values for this trigger type.)

I am new to ORACLE and this MySQL -> ORACLE conversion takes a lot longer that expected.

Any help is appreciated.

Here is the code:


create table test_table
{
nrow number not null primary key,
...
};

create sequence nrow_sequence start with 1 increment by 1 nomaxvalue;

create trigger autoincrement_trigger
before insert
on test_table
referencing NEW as NEW
for each row
begin

        select nrow_sequency.nextval into :new.nrow from dual; end;


As I said, when I change "before insert" to "after insert" an ORA-04084 is generated. Received on Tue Jun 13 2006 - 11:54:41 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US