Alex Rivera | Logout

Auto-increment in Oracle without using a trigger

Asked 2008-11-25T11:04:26.577
23

What are the other ways of achieving auto-increment in oracle other than use of triggers?

Edit
Report

2 Answers

11

A trigger to obtain the next value from a sequence is the most common way to achieve an equivalent to AUTOINCREMENT:

create trigger mytable_trg
before insert on mytable
for each row
when (new.id is null)
begin
    select myseq.nextval into :new.id from dual;
end;

You don't need the trigger if you control the inserts - just use the sequence in the insert statement:

insert into mytable (id, data) values (myseq.nextval, 'x');

This could be hidden inside an API package, so that the caller doesn't need to reference the sequence:

mytable_pkg.insert_row (p_data => 'x');

But using the trigger is more "transparent".

answered 2008-11-25T13:09:56.323
-8
SELECT max (id) + 1 
FROM   table
answered 2008-12-01T12:42:10.280

Your Answer