2 5.3. Identity Columns #
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.
7 To create an identity column, use the GENERATED ... AS IDENTITY clause
8 in CREATE TABLE, for example:
10 id bigint GENERATED ALWAYS AS IDENTITY,
16 id bigint GENERATED BY DEFAULT AS IDENTITY,
20 See CREATE TABLE for more details.
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,
27 INSERT INTO people (name, address) VALUES ('A', 'foo');
28 INSERT INTO people (name, address) VALUES ('B', 'bar');
30 would generate values for the id column starting at 1 and result in the
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');
41 Similarly, the keyword DEFAULT can be used in UPDATE commands.
43 Thus, in many ways, an identity column behaves like a column with a
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.
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).
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
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.
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