Sub topic 8
Generating Keys with Identity Columns
This chapter assumes that you know basic INSERT statements and understand primary keys. It also assumes Oracle Database 12c or later, because identity columns were introduced in Oracle 12c.
An identity column is a numeric column whose value Oracle generates automatically. It is commonly used for surrogate keys, which are keys created only to identify rows. The generated value comes from a sequence managed by Oracle.
Creating an identity column
Add an identity clause to a numeric column definition:
CREATE TABLE departments (
department_id NUMBER GENERATED ALWAYS AS IDENTITY,
department_name VARCHAR2(100) NOT NULL,
CONSTRAINT departments_pk
PRIMARY KEY (department_id),
CONSTRAINT departments_name_uk
UNIQUE (department_name)
);The department_id column is both an identity column and the table's primary key. An insert normally omits that column:
INSERT INTO departments (department_name)
VALUES ('Sales');
INSERT INTO departments (department_name)
VALUES ('Support');
SELECT department_id, department_name
FROM departments
ORDER BY department_id;Oracle generates a different key for each inserted row. You do not need to create or call a separate sequence.
Choosing an identity generation mode
Oracle provides three generation modes. The mode controls whether an insert may provide its own value.
| Mode | Omitted value | Explicit non-NULL value | Explicit NULL |
|---|---|---|---|
| `GENERATED ALWAYS` | Generated | Rejected | Rejected |
| `GENERATED BY DEFAULT` | Generated | Accepted | Usually rejected |
| `GENERATED BY DEFAULT ON NULL` | Generated | Accepted | Generated |
GENERATED ALWAYS gives Oracle complete control. This is a good default when application code should never assign identifiers.
INSERT INTO departments (department_id, department_name)
VALUES (99, 'Finance');The insert fails because department_id is GENERATED ALWAYS. Oracle reports an error such as ORA-32795.
BY DEFAULT permits an explicit value, which can help with controlled data loads:
CREATE TABLE employees (
employee_id NUMBER
GENERATED BY DEFAULT ON NULL AS IDENTITY,
email VARCHAR2(255) NOT NULL,
CONSTRAINT employees_pk
PRIMARY KEY (employee_id)
);
INSERT INTO employees (employee_id, email)
VALUES (5000, '[email protected]');
INSERT INTO employees (employee_id, email)
VALUES (NULL, '[email protected]');The first row receives 5000. The second row receives a generated value because ON NULL treats the supplied NULL as a request to generate one. This mode is often more tolerant of application code that includes the identity column in its insert list.
How the pieces fit
Identity generation does not replace the primary key constraint. The identity clause generates a candidate value; the primary key enforces uniqueness and disallows null values. Define both when the column identifies rows.
Inspecting identity columns
Oracle exposes identity metadata through a data dictionary view:
SELECT table_name,
column_name,
generation_type,
sequence_name
FROM user_tab_identity_cols
WHERE table_name IN ('DEPARTMENTS', 'EMPLOYEES')
ORDER BY table_name, column_name;GENERATION_TYPE shows whether the column uses ALWAYS, BY DEFAULT, or BY DEFAULT ON NULL. Oracle also records the sequence associated with the identity column.
Exercise
Which identity mode should you choose when application code must never supply its own key?
Show answer
Choose GENERATED ALWAYS AS IDENTITY. Inserts that provide an explicit identity value are rejected.