Sub topic 9
ALWAYS vs BY DEFAULT vs BY DEFAULT ON NULL
Assume you know basic CREATE TABLE and INSERT statements, primary keys, and foreign keys. The examples require Oracle Database 12c or later.
An identity column is a numeric column whose value Oracle generates from a system-managed sequence. Identity columns are often used for surrogate keys, which are keys created only to identify rows.
The generation mode controls whether an insert may provide its own value.
The three generation modes
| Mode | Omitted column | Explicit non-NULL value | Explicit `NULL` |
|---|---|---|---|
| `GENERATED ALWAYS` | Oracle generates a value | Rejected | Oracle generates a value |
| `GENERATED BY DEFAULT` | Oracle generates a value | Accepted | Stored as `NULL`, subject to constraints |
| `GENERATED BY DEFAULT ON NULL` | Oracle generates a value | Accepted | Oracle generates a value |
GENERATED ALWAYS gives Oracle complete control. Use it when applications must never choose key values.
GENERATED BY DEFAULT allows an application or data migration to provide a value. If the column is omitted, Oracle generates one. An explicit NULL is treated as a supplied value, so a NOT NULL or primary key constraint can reject it.
GENERATED BY DEFAULT ON NULL also accepts explicit values, but treats NULL as a request to generate a value. This mode is useful when an application always includes the column in an insert but sometimes passes NULL.
A working example
The following table demonstrates all three modes. The identity columns are also primary keys, so they cannot contain NULL.
CREATE TABLE identity_modes_demo (
always_id NUMBER GENERATED ALWAYS AS IDENTITY,
default_id NUMBER GENERATED BY DEFAULT AS IDENTITY,
default_null_id NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY,
description VARCHAR2(100) NOT NULL,
CONSTRAINT identity_modes_demo_pk
PRIMARY KEY (always_id)
);If an identity column is omitted, Oracle generates a value for every mode:
INSERT INTO identity_modes_demo (description)
VALUES ('Generated values');
SELECT always_id, default_id, default_null_id, description
FROM identity_modes_demo;The explicit value 500 is accepted for default_id and default_null_id:
INSERT INTO identity_modes_demo
(default_id, default_null_id, description)
VALUES (500, 600, 'Application supplied values');An explicit value for always_id is rejected:
-- Fails with ORA-32795 because ALWAYS does not allow a supplied value.
INSERT INTO identity_modes_demo (always_id, description)
VALUES (900, 'Not allowed');For default_null_id, NULL activates the generator:
INSERT INTO identity_modes_demo
(default_id, default_null_id, description)
VALUES (700, NULL, 'NULL triggers generation');For default_id, an explicit NULL is still a supplied value. Because default_id is not declared as a primary key in this example, its behavior can be observed separately. In production, define key columns with a primary key or NOT NULL constraint so that missing identifiers cannot be stored.
Choosing a mode
Choose GENERATED ALWAYS for ordinary application-created rows when the database owns key generation.
Choose BY DEFAULT when controlled imports, replication, or data migrations must preserve known identifiers.
Choose BY DEFAULT ON NULL when callers may send the identity column and use NULL to mean “generate this value.”
The mode does not make values consecutive. Rollbacks, deletes, caching, and concurrent sessions can create gaps. Do not use an identity column as a row count or as a timestamp.
How inserts flow
Exercise
Which mode should you choose if an import must preserve explicit identifiers, but ordinary inserts should generate identifiers when the column is omitted?
Show answer
Choose GENERATED BY DEFAULT AS IDENTITY. It accepts explicit non-NULL identifiers and generates a value when the column is omitted.