mercoledì 23 gennaio 2013

Tip: Oracle SQL Developer Data Modeler Create Sequence for an auto incrementing ID

Recently I have started using this tool from Oracle and I must admit it is brilliant for designing Star Schemas and generating DDL code.
For my project I need to define an auto incrementing ID field for several tables of my model. I obviously needed a sequence for each and I was looking for a way to define it in the Data Modeler.
Oracle Data Modeler allows you to mark any column field as auto-incrementing. Select the table in your model and double click on the column. In the example below, I want the primary key field to be auto-incrementing.


Select the option Auto-Incrementing in the Column Property panel.



If you check the DDL you will see the code for the sequence and the trigger that is fired whenever a new line is inserted into the table.

CREATE SEQUENCE CNE_CNEIDE_SEQ
    NOCACHE
    ORDER ;

CREATE OR REPLACE TRIGGER CNE_CNEIDE_TRG
BEFORE INSERT ON T_CANAL_ENTRADA
FOR EACH ROW
WHEN (NEW.CNEIDE IS NULL)
BEGIN
    SELECT CNE_CNEIDE_SEQ.NEXTVAL INTO :NEW.CNEIDE FROM DUAL;
END;
/


If you don't need the trigger, just untick the option in the Auto Increment panel.


1 commento:

  1. Hey! That was really helpfull!
    This tool sure is amazing.
    Regards!

    RispondiElimina