Skip to content
Advertisement

How to create id with AUTO_INCREMENT on Oracle?

It appears that there is no concept of AUTO_INCREMENT in Oracle, up until and including version 11g.

How can I create a column that behaves like auto increment in Oracle 11g?

Advertisement

Answer

There is no such thing as “auto_increment” or “identity” columns in Oracle as of Oracle 11g. However, you can model it easily with a sequence and a trigger:

Table definition:

Trigger definition:

UPDATE:

IDENTITY column is now available on Oracle 12c:

or specify starting and increment values, also preventing any insert into the identity column (GENERATED ALWAYS) (again, Oracle 12c+ only)

Alternatively, Oracle 12 also allows to use a sequence as a default value:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement