Auto increment Integer Value on Oracle Database (10g)
Using databases it is often useful to have rows identified by a unique numeric value (mainly for ID columns), no matter what the value is. If, like me, you have a background of MySQL (where auto increment values are easy like saying AUTO_INCREMENT) but for some reasons you have to develop something on Oracle Database 10g you’ll be a little confused about the lack of a single auto increment function.
Here is a simple howto on how to have an auto incremented numeric column value on Oracle Database using a sequence and a trigger (if you find some errors please use the comments):
Tested on: Oracle Database 10g Enterprise Edition
Precondition: a numeric type (INTEGER is ok) column in a table (in this example the schema is PROVA01 and the table is UTENTI with ID as column).
- Log in Oracle Enterprise Manager (probably http://servername:1158/em/) as user with proper privileges (SYS as SYSDBA will work);
- Go to “Administration”;
- Under “Schemas > Database Objects” open “Sequence”;
- Select proper schema (in this how to is PROVA01, the table is UTENTI and the field is ID);
- Create a new sequence by filling:
- Return back to administration page and go to “Programs > Triggers” and create a new trigger:
- Name: TRIGGER_SEQ_UTENTI is a good choice;
- Schema: PROVA01;
- Select “Replace if exists”
- Trigger body (in bold values you may change):
BEGIN SELECT UTENTI_ID_SEQ.NEXTVAL INTO :NEW.ID FROM DUAL; END UTENTE_SEQ_TRIGGER;
- Go to “Event” tab and configure the trigger:
- “Execute the trigger” on Table;
- Table: in this case PROVA01.UTENTI;
- Start trigger: before;
- Event: INSERT;
- Go to “Advanced” and select “FOR EACH ROW”;
- Save the trigger and, if any, check out the errors.
Test the sequence and trigger by inserting a new table row, the ID value will be auto-inserted (and incremented).
thanks a lot.
i was getting frustrated ….
i have been working oon mysql and mam new to oracle..
but this helped me a lot…
thanks once again.
you are welcome
Thank’s so much
I newbi in oracle