@@ -62,6 +62,7 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI
62
62
NULL |
63
63
CHECK ( <replaceable class="PARAMETER">expression</replaceable> ) [ NO INHERIT ] |
64
64
DEFAULT <replaceable>default_expr</replaceable> |
65
+ GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( <replaceable>sequence_options</replaceable> ) ] |
65
66
UNIQUE <replaceable class="PARAMETER">index_parameters</replaceable> |
66
67
PRIMARY KEY <replaceable class="PARAMETER">index_parameters</replaceable> |
67
68
REFERENCES <replaceable class="PARAMETER">reftable</replaceable> [ ( <replaceable class="PARAMETER">refcolumn</replaceable> ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]
@@ -81,7 +82,7 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI
81
82
82
83
<phrase>and <replaceable class="PARAMETER">like_option</replaceable> is:</phrase>
83
84
84
- { INCLUDING | EXCLUDING } { DEFAULTS | CONSTRAINTS | INDEXES | STORAGE | COMMENTS | ALL }
85
+ { INCLUDING | EXCLUDING } { DEFAULTS | CONSTRAINTS | IDENTITY | INDEXES | STORAGE | COMMENTS | ALL }
85
86
86
87
<phrase>and <replaceable class="PARAMETER">partition_bound_spec</replaceable> is:</phrase>
87
88
@@ -412,6 +413,11 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI
412
413
Column <literal>STORAGE</> settings are also copied from parent tables.
413
414
</para>
414
415
416
+ <para>
417
+ If a column in the parent table is an identity column, that property is
418
+ not inherited. A column in the child table can be declared identity
419
+ column if desired.
420
+ </para>
415
421
</listitem>
416
422
</varlistentry>
417
423
@@ -480,6 +486,12 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI
480
486
such as <function>nextval</>, may create a functional linkage between
481
487
the original and new tables.
482
488
</para>
489
+ <para>
490
+ Any identity specifications of copied column definitions will only be
491
+ copied if <literal>INCLUDING IDENTITY</literal> is specified. A new
492
+ sequence is created for each identity column of the new table, separate
493
+ from the sequences associated with the old table.
494
+ </para>
483
495
<para>
484
496
Not-null constraints are always copied to the new table.
485
497
<literal>CHECK</literal> constraints will be copied only if
@@ -512,7 +524,7 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI
512
524
</para>
513
525
<para>
514
526
<literal>INCLUDING ALL</literal> is an abbreviated form of
515
- <literal>INCLUDING DEFAULTS INCLUDING CONSTRAINTS INCLUDING INDEXES INCLUDING STORAGE INCLUDING COMMENTS</literal>.
527
+ <literal>INCLUDING DEFAULTS INCLUDING IDENTITY INCLUDING CONSTRAINTS INCLUDING INDEXES INCLUDING STORAGE INCLUDING COMMENTS</literal>.
516
528
</para>
517
529
<para>
518
530
Note that unlike <literal>INHERITS</literal>, columns and
@@ -626,6 +638,37 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI
626
638
</listitem>
627
639
</varlistentry>
628
640
641
+ <varlistentry>
642
+ <term><literal>GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( <replaceable>sequence_options</replaceable> ) ]</literal></term>
643
+ <listitem>
644
+ <para>
645
+ This clause creates the column as an <firstterm>identity
646
+ column</firstterm>. It will have an implicit sequence attached to it
647
+ and the column in new rows will automatically have values from the
648
+ sequence assigned to it.
649
+ </para>
650
+
651
+ <para>
652
+ The clauses <literal>ALWAYS</literal> and <literal>BY DEFAULT</literal>
653
+ determine how the sequence value is given precedence over a
654
+ user-specified value in an <command>INSERT</command> statement.
655
+ If <literal>ALWAYS</literal> is specified, a user-specified value is
656
+ only accepted if the <command>INSERT</command> statement
657
+ specifies <literal>OVERRIDING SYSTEM VALUE</literal>. If <literal>BY
658
+ DEFAULT</literal> is specified, then the user-specified value takes
659
+ precedence. See <xref linkend="sql-insert"> for details. (In
660
+ the <command>COPY</command> command, user-specified values are always
661
+ used regardless of this setting.)
662
+ </para>
663
+
664
+ <para>
665
+ The optional <replaceable>sequence_options</replaceable> clause can be
666
+ used to override the options of the sequence.
667
+ See <xref linkend="sql-createsequence"> for details.
668
+ </para>
669
+ </listitem>
670
+ </varlistentry>
671
+
629
672
<varlistentry>
630
673
<term><literal>UNIQUE</> (column constraint)</term>
631
674
<term><literal>UNIQUE ( <replaceable class="PARAMETER">column_name</replaceable> [, ... ] )</> (table constraint)</term>
@@ -1263,7 +1306,7 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI
1263
1306
1264
1307
<para>
1265
1308
Using OIDs in new applications is not recommended: where
1266
- possible, using a <literal>SERIAL</literal> or other sequence
1309
+ possible, using an identity column or other sequence
1267
1310
generator as the table's primary key is preferred. However, if
1268
1311
your application does make use of OIDs to identify specific
1269
1312
rows of a table, it is recommended to create a unique constraint
@@ -1323,7 +1366,7 @@ CREATE TABLE films (
1323
1366
);
1324
1367
1325
1368
CREATE TABLE distributors (
1326
- did integer PRIMARY KEY DEFAULT nextval('serial') ,
1369
+ did integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY ,
1327
1370
name varchar(40) NOT NULL CHECK (name <> '')
1328
1371
);
1329
1372
</programlisting>
@@ -1737,6 +1780,20 @@ CREATE TABLE cities_ab_10000_to_100000
1737
1780
</para>
1738
1781
</refsect2>
1739
1782
1783
+ <refsect2>
1784
+ <title>Multiple Identity Columns</title>
1785
+
1786
+ <para>
1787
+ <productname>PostgreSQL</productname> allows a table to have more than one
1788
+ identity column. The standard specifies that a table can have at most one
1789
+ identity column. This is relaxed mainly to give more flexibility for
1790
+ doing schema changes or migrations. Note that
1791
+ the <command>INSERT</command> command supports only one override clause
1792
+ that applies to the entire statement, so having multiple identity columns
1793
+ with different behaviors is not well supported.
1794
+ </para>
1795
+ </refsect2>
1796
+
1740
1797
<refsect2>
1741
1798
<title><literal>LIKE</> Clause</title>
1742
1799
0 commit comments