Oracle - Modify An Existing Table To Auto-increment A Column
I have a table with the following column: NOTEID NUMBER NOT NULL, For all intents and purposes, this column is the primary key. This table has a few thousand rows, each with
Solution 1:
You can't alter the table. Oracle doesn't support declarative auto-incrementing columns. You can create a sequence
CREATE SEQUENCE note_seq
STARTWITH800
INCREMENT BY1
CACHE 100;
Then, you can create a trigger
CREATEOR REPLACE TRIGGER populate_note_id
BEFORE INSERTON note
FOREACHROWBEGIN
:new.note_id := note_seq.nextval;
END;
or, if you want to allow callers to specify a non-default NOTE_ID
CREATEOR REPLACE TRIGGER populate_note_id
BEFORE INSERTON note
FOREACHROWBEGIN
IF( :new.note_id isnull )
THEN
:new.note_id := note_seq.nextval;
END IF;
END;
Solution 2:
If your MAX(noteid) is 799, then try:
CREATE SEQUENCE noteseq
STARTWITH800
INCREMENT BY1Then when inserting a new record, for the NOTEID column, you would do:
noteseq.nextval
Post a Comment for "Oracle - Modify An Existing Table To Auto-increment A Column"