Skip to content


Oracle’s Auto Incrementing with Sequences

My previous post, titled: ‘Sequence-less/Trigger-less Oracle Auto Increment‘ was shot out of the water by my friend and DBA, Jon Emmons. Glad to see that I can be kept in line.

So I have resigned to use Oracle Sequences as a safer means for auto incrementing. But that doesn’t mean that I like it. Here’s what needs to be done to implement auto_incrementing with Oracle:

First, create a sequence:

CREATE SEQUENCE sweet_incrementing INCREMENT BY 1 START WITH 1;

Next write your statement:

INSERT INTO table (id,name) VALUES (sweet_incrementing.NextVal,’bork’);

Now at a second glance, this statement looks much cleaner than my first attempt at subverting the system. However, the fact that you need to create a whole new sequence for each table you wish to have an auto incrementer is pretty stupid when compared to MySQL’s plan of attack. In MySQL you can simply mark a column as a auto_increment during table creation and you’re good to go!

Posted in Blog, Technology.

Tagged with , , , , , .


8 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. satish says

    How does a sequence work internally ?
    Is it stored somewhere or a query is fired and created everytime?

  2. Matt says

    Sequences are stored in oracle as objects. The important thing to note is that once .nextval (which increments the sequence by one) has been used, if you were to do a rollback, the sequence would not decrement as one would expect.

  3. gouthami says

    sequence automaticallyt generates unique numbers .It is a sharable object .It is stored and generated independently of tables

  4. Joc says

    While your comments about MySQL being a much simpler way of doing things is true, there is a lot more flexibility with the Oracle approach. For example, if you wished to have an incrementing value that is shared by multiple tables but guaranteed unique across them all.

  5. oldDog says

    seqName.nextval and seqName.currval and their behavior have several advantages over AUTONUMBER or IDENTITY methods. For example, knowing the PK value to be used on INSERT before performing it saves on the cost of having to ask after the fact.

    If you measure 1m executions of last_insert_id() after INSERT to a table, compared to use of seqName.nextval and seqName.currval and their costs, you may find a performance advantage for SEQUENCE. As for the practical matter of having to ask “what value did we get”, seqName.currval is there if you need it (until your next NEXTVAL!)

  6. Rajesh says

    Its good,
    i have a small doubt,
    how to find the name of column where sequence is created?

    Plz, forward to to my mail id rajesh227@gmail.com
    tx, in advance

Continuing the Discussion

  1. Life After Coffee » How to Create Auto Increment Columns in Oracle linked to this post on March 21, 2006

    [...] Matt has posted a similar method in which he uses a sequence in the insert eliminating the need for the trigger. That will work just as well without the need for the trigger. The only drawback to this method is slightly longer SQL commands. [...]

  2. Oracle 10g: Using The Returning Clause With ADOdb : BorkWeb linked to this post on October 21, 2008

    [...] when you are inserting into a table with auto incrementing values (via a sequence) you have always been able to grab the last value with a simple SELECT statement (line 22): PLAIN [...]



Some HTML is OK

or, reply to this post via trackback.