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!

Discuss This Article


8 Responses to “Oracle’s Auto Incrementing with Sequences”

  1. pingback pingback:
    Life After Coffee » How to Create Auto Increment Columns in Oracle

    [...] 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. [...]

    Reply to this comment.
    1
  2. Avatarsatish

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

    Reply to this comment.
    2
  3. AvatarMatt
    Author Comment

    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.

    Reply to this comment.
    3
  4. Avatargouthami

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

    Reply to this comment.
    4
  5. AvatarJoc

    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.

    Reply to this comment.
    5
  6. AvataroldDog

    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!)

    Reply to this comment.
    6
  7. AvatarRajesh

    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

    Reply to this comment.
    7
  8. pingback pingback:
    Oracle 10g: Using The Returning Clause With ADOdb : BorkWeb

    [...] 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 [...]

    Reply to this comment.
    8

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Comment Preview:

 (4725) - oracle sequence (103) - oracle sequence id (27) - oracle sequence insert (23) - oracle create sequence (22) - oracle insert id (19) - oracle sequence column (18) - sequence oracle (17) - oracle auto increment (16) - oracle sequence rollback (16) - oracle sequence trigger (14) - create sequence Oracle (14) - oracle sequences (13) - oracle insert sequence (13) - Sequence Column Oracle (13) - oracle (12) - oracle autoid (12) - oracle table sequence (12) - CREATE SEQUENCE in oracle (12) - ORACLE AUTO INDEX (12) - sequence creation in oracle (11) - oracle auto id (10) - sequence in oracle (10) - mysql nextval (10) - oracle trigger sequence (9) - sequence table oracle (9) - oracle autoindex (9) - HOW TO WRITE SEQUENCE IN ORACLE (8) - oracle sequence syntax (8) - oracle sequence usage (8) - insert sequence oracle (7) - oracle insert nextval (7) - ORACLE sequence table (7) - oracle sequence PK (7) - SEQUENCE ID ORACLE (6) - auto id oracle (6) - oracle automatic id (6) - oracle sequence random (6) - next id in Oracle (6) - oracle autocounter (6) - trigger sequence oracle (5) - oracle insert id from sequence (5) - create oracle sequence (5) - oracle automatic sequence (5) - oracle auto generated id (5) - last_insert_id oracle (5) - oracle table auto sequence (5) - oracle auto indexing (5) - oracle insert pk (5) - sequences in oracle (4) - oracle autoincrement (4) - oracle insert sequence id (4) - oracle auto sequence (4) - How to write a sequence in Oracle (4) - trigger oracle sequence (4) - oracle auto insert (4) - create sequence oracle syntax (4) - mysql auto_increment in oracle (4) - creating a sequence in oracle (4) - SEQUENCES EN ORACLE (4) - Oracle pk auto increment (4) - oracle insert unique id (4) - oracle auto identity (4) - advantage of using oracle sequence (4) - oracle sequences trigger (3) - auto increment in oracle (3) - sequence trigger oracle (3) - sequence oracle trigger (3) - oracle sequence in trigger (3) - oracle insert ID sequence (3) - oracle id sequence (3) - oracle trigger sequence nextval (3) - mysql auto_increment oracle (3) - oracle rollback sequence (3) - CREATING SEQUENCE IN ORACLE (3) - automatic sequence in oracle (3) - Oracle insert auto id (3) - oracle auto generate (3) - creation of sequence in oracle (3) - oracle sequence stored (3) - sequence id in oracle (3) - oracle sequences in triggers (3) - oracle autoinc (3) - advantage of use oracle sequence (3) - oracle automatische id (3) - oracle sequence increment by (3) - oracle sequences advantages (3) - query for sequence in oracle (3) - TABLE SEQUENCE ID in oracle (3) - oracle table sequences (3) - oracle lite sequences (3) - alter sequence oracle (3) - oracle create seqence (3) - Oracle sequence example (3) - how squences in mysql different from oracle (3) - advantages of sequence in oracle (3) - increment oracle query (3) - sequence during table creation oracle (3) - Advantage of Sequence oracle (3) - oracel insert into (3) - oracle last sequence id (3) - oracle auto generated pk (3) - oracle create sequence syntax (3) - create sequence based columns for table in oracle (3) - how a sequence works in oracle (3) - Oracle insert NEXTVAL in table (3) - oracle sequence advantage (3) - automatic sequence oracle (3) - insert into oracle pk increment (3) - auto generate pk oracle (3) - automatic id in oracle (3) - auto generated sequence in oracle (3) - column sequence oracle (3) - oracle sequence und trigger für id (3) - insert command with automatic increment to a column in oracle (3) - oracle automatic id column (3) - create auto generated column in oracle (3) - oracle lite autoincrement trigger (3) - ORACLE column auto index (3) - oracle sequence without trigger (3) - how to write query for incrementing the value (3) - oracle sequence em (3) - ORACLE SEQUENCE TRIGGER EXAMPLE (3) - oracle auto (2) - oracle sequence autoincrement (2) - auto_increment in oracle (2) - oracle triggers sequences (2) - oracle auto increment trigger (2) - oracle auto_increment (2) - oracle autoincrement column (2) - Oracle sequences table (2) - sequence oracle table (2) - Oracle trigger sequences (2) - oracle table id sequence (2) - oracle trigger auto sequence (2) - oracles sequences (2) - oracle sequences methods (2) - Oracle autoincrement columns (2) - creating oracle sequence (2) - sequence in oracle table (2) - How Oracle sequence works (2) - sequence syntax in Oracle (2) - oracle auto increment without trigger (2) - oracle where are sequences stored (2) - where are oracle sequences stored (2) - insert sequence in a table (2) - oracle auto column (2) - insert sequence in Oracle (2) - oracle auto id column (2) - oracle autoincrement mysql (2) -