]> begriffs open source - ai-pg/blob - full-docs/txt/ddl-identity-columns.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / ddl-identity-columns.txt
1
2 5.3. Identity Columns #
3
4    An identity column is a special column that is generated automatically
5    from an implicit sequence. It can be used to generate key values.
6
7    To create an identity column, use the GENERATED ... AS IDENTITY clause
8    in CREATE TABLE, for example:
9 CREATE TABLE people (
10     id bigint GENERATED ALWAYS AS IDENTITY,
11     ...,
12 );
13
14    or alternatively
15 CREATE TABLE people (
16     id bigint GENERATED BY DEFAULT AS IDENTITY,
17     ...,
18 );
19
20    See CREATE TABLE for more details.
21
22    If an INSERT command is executed on the table with the identity column
23    and no value is explicitly specified for the identity column, then a
24    value generated by the implicit sequence is inserted. For example, with
25    the above definitions and assuming additional appropriate columns,
26    writing
27 INSERT INTO people (name, address) VALUES ('A', 'foo');
28 INSERT INTO people (name, address) VALUES ('B', 'bar');
29
30    would generate values for the id column starting at 1 and result in the
31    following table data:
32  id | name | address
33 ----+------+---------
34   1 | A    | foo
35   2 | B    | bar
36
37    Alternatively, the keyword DEFAULT can be specified in place of a value
38    to explicitly request the sequence-generated value, like
39 INSERT INTO people (id, name, address) VALUES (DEFAULT, 'C', 'baz');
40
41    Similarly, the keyword DEFAULT can be used in UPDATE commands.
42
43    Thus, in many ways, an identity column behaves like a column with a
44    default value.
45
46    The clauses ALWAYS and BY DEFAULT in the column definition determine
47    how explicitly user-specified values are handled in INSERT and UPDATE
48    commands. In an INSERT command, if ALWAYS is selected, a user-specified
49    value is only accepted if the INSERT statement specifies OVERRIDING
50    SYSTEM VALUE. If BY DEFAULT is selected, then the user-specified value
51    takes precedence. Thus, using BY DEFAULT results in a behavior more
52    similar to default values, where the default value can be overridden by
53    an explicit value, whereas ALWAYS provides some more protection against
54    accidentally inserting an explicit value.
55
56    The data type of an identity column must be one of the data types
57    supported by sequences. (See CREATE SEQUENCE.) The properties of the
58    associated sequence may be specified when creating an identity column
59    (see CREATE TABLE) or changed afterwards (see ALTER TABLE).
60
61    An identity column is automatically marked as NOT NULL. An identity
62    column, however, does not guarantee uniqueness. (A sequence normally
63    returns unique values, but a sequence could be reset, or values could
64    be inserted manually into the identity column, as discussed above.)
65    Uniqueness would need to be enforced using a PRIMARY KEY or UNIQUE
66    constraint.
67
68    In table inheritance hierarchies, identity columns and their properties
69    in a child table are independent of those in its parent tables. A child
70    table does not inherit identity columns or their properties
71    automatically from the parent. During INSERT or UPDATE, a column is
72    treated as an identity column if that column is an identity column in
73    the table named in the statement, and the corresponding identity
74    properties are applied.
75
76    Partitions inherit identity columns from the partitioned table. They
77    cannot have their own identity columns. The properties of a given
78    identity column are consistent across all the partitions in the
79    partition hierarchy.