Identity Columns (Autoincrement)
Identity columns are defined using the GENERATED {ALWAYS | BY DEFAULT} AS IDENTITY
clause.The identity column is a column associated with an internal sequence.Its value is set automatically every time it is not specified in the INSERT
statement, or when the column value is specified as DEFAULT
.
Rules
-
The data type of an identity column must be an exact number type with zero scale.Allowed types are
SMALLINT
,INTEGER
,BIGINT
,NUMERIC(p[,0])
andDECIMAL(p[,0])
with p <= 18.-
The
INT128
type and numeric types with a precision higher than 18 are not supported.
-
-
An identity column cannot have a
DEFAULT
orCOMPUTED
value. -
An identity column can be altered to become a regular column.
-
A regular column cannot be altered to become an identity column.
-
Identity columns are implicitly
NOT NULL
(non-nullable), and cannot be made nullable. -
Uniqueness is not enforced automatically.A
UNIQUE
orPRIMARY KEY
constraint is required to guarantee uniqueness. -
The use of other methods of generating key values for identity columns, e.g. by trigger-generator code or by allowing users to change or add them, is discouraged to avoid unexpected key violations.
-
The
INCREMENT
value cannot be zero (0
).