Situsnya Wordpress

while scratching my head…

How to Create Auto Increment Columns in Oracle September 2, 2008

Filed under: Oracle — Remo @ 4:13 am
Tags:

After pointing out how not to create auto increment columns in Oracle, I suppose I should point out how to create auto increment columns in oracle.

Many will gripe about this not being a standard feature in Oracle, but when it’s as easy as two more commands after your CREATE TABLE command I can’t see any good reason to use fancy SQL on every insert.

First let’s create a simple table to play with.

SQL> CREATE TABLE test
(id NUMBER PRIMARY KEY,
name VARCHAR2(30));

Table created.

Now we’ll assume we want ID to be an auto increment field. First we need a sequence to grab values from.

SQL> CREATE SEQUENCE test_sequence
START WITH 1
INCREMENT BY 1;

Sequence created.

Now we can use that sequence in an BEFORE INSERT trigger on the table.

CREATE OR REPLACE TRIGGER test_trigger
BEFORE INSERT
ON test
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
SELECT test_sequence.nextval INTO :NEW.ID FROM dual;
END;
/

Trigger created.

This trigger will automatically grab the next value from the sequence we just created and substitute it into the ID column before the insert is completed.

Now we’ll do some inserts:

SQL> INSERT INTO test (name) VALUES ('Jon');

1 row created.

SQL> INSERT INTO test (name) VALUES (’Bork’);

1 row created.

SQL> INSERT INTO test (name) VALUES (’Matt’);

1 row created.

SQL> SELECT * FROM test;

ID NAME
———- ——————————
1 Jon
2 Bork
3 Matt

Because the sequence is updated independent of the rows being committed there will be no conflict if multiple users are inserting into the same table simultaneously.

If you need to capture the value of the auto increment column you’ll want to check out my other article Oracle Auto increment Columns – Part 2

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.

Source:

http://www.lifeaftercoffee.com/2006/02/17/how-to-create-auto-increment-columns-in-oracle/

About these ads
 

16 Responses to “How to Create Auto Increment Columns in Oracle”

  1. keshab Says:

    hi
    i m keshab , from last few month i was searching it .today i got .so thanks…………..

  2. remoharsono Says:

    Hi keshab, glad to know it’s useful for you. I’ve put the original url of this article so that people know who should get credit…yes, not me…thanks for posting the comment anyway :)

  3. Satish Says:

    Hi folks,

    Is there any way to create auto increment column in oracle the same way like in mysql.

    CREATE TABLE test (
    TEST1 Number(10) NOT NULL auto_increment,
    TEST2 varchar2(50) default NULL,
    PRIMARY KEY (TEST1)
    );

    Is it necessary to create a seq. and a trigger in case of oracle??

    Cheers

    Satish

  4. Rakesh Says:

    can we define auto increment when we create a table. acutally i do not want to create trigger for it. i wants that when i create a table with create table statement then i can define autoincrement

  5. Remo Says:

    rakesh, unfortunately oracle doesn’t support autoincrement so you have to create trigger to implement autoincrement…at least for rel 8i. I’m not quite sure about other versions…

  6. swathi Says:

    this ans helped a lot..thanq u vry much..

  7. swathi Says:

    how can we see the sequence we have created for the table

  8. Thank you man, this was very usefull…

  9. vaibhav Says:

    Thank you very much….
    its really very helpful……

  10. Suresh verma Says:

    Hi,
    This is very useful for us….
    Thanks a lot

  11. hirondas Says:

    Thanks a lot, really its a very impotent note for me…

  12. Subrata Dey Pappu Says:

    if i want to write a trigger on a particular attribute of a table i.e. if and only if a column is updated in table 1 then table 2 should be updated. how can i write this code?
    is it valid?
    create or replace trigger mytrigger
    after insert on table1.column name…..

  13. cyber_dy Says:

    thanks for this post…

  14. [...] : http://situsnya.wordpress.com/2008/09/02/how-to-create-auto-increment-columns-in-oracle/ Share this:TwitterFacebookLike this:LikeBe the first to like [...]

  15. rituparna bhattacharyya Says:

    thanx bro…


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.