How to make column autoincrement in Oracle 9i

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • didebuli
    New Member
    • May 2007
    • 5

    How to make column autoincrement in Oracle 9i

    Hi all,
    I have table test with fields (ID, Name, Lastname)
    ID (number)
    Name varchar (15)
    Lastname varchar (30)

    When I insert record to table I want to be incremented field ID automatically.

    Please give me your opinion how can I make ID field autoincrement.

    Thanks in advance.
  • kpenneti
    New Member
    • May 2007
    • 1

    #2
    Hi,

    Better you write after insert trigger. Why i am telling is once you insert the value in to the table the after insert trigger will be fired and id no will be increment.

    Originally posted by didebuli
    Hi all,
    I have table test with fields (ID, Name, Lastname)
    ID (number)
    Name varchar (15)
    Lastname varchar (30)

    When I insert record to table I want to be incremented field ID automatically.

    Please give me your opinion how can I make ID field autoincrement.

    Thanks in advance.

    Comment

    • didebuli
      New Member
      • May 2007
      • 5

      #3
      Can you write me step-by-step how can I do it.

      Thanks in advance.

      Comment

      • chandu031
        Recognized Expert New Member
        • Mar 2007
        • 77

        #4
        Originally posted by didebuli
        Hi all,
        I have table test with fields (ID, Name, Lastname)
        ID (number)
        Name varchar (15)
        Lastname varchar (30)

        When I insert record to table I want to be incremented field ID automatically.

        Please give me your opinion how can I make ID field autoincrement.

        Thanks in advance.
        Hi,

        The only way of implementing an Identity column in Oracle would be to have a
        before insert trigger on the table. Here's the syntax for the same:
        Code:
        CREATE OR REPLACE TRIGGER 
        TRIG
        BEFORE INSERT ON TEST
        FOR EACH ROW
        BEGIN
        SELECT SEQ.NEXTVAL INTO :NEW.ID FROM DUAL;
        
        EXCEPTION 
        WHEN OTHERS THEN
        RAISE_APPLICATION_ERROR(-20001,SQLERRM());
        END;
        To use this you will have to create a sequence first.

        Hope this is helpful.

        Comment

        • didebuli
          New Member
          • May 2007
          • 5

          #5
          Thank you very much

          Comment

          Working...