@@ -62,6 +62,7 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI
6262 NULL |
6363 CHECK ( <replaceable class="PARAMETER">expression</replaceable> ) [ NO INHERIT ] |
6464 DEFAULT <replaceable>default_expr</replaceable> |
65+ GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( <replaceable>sequence_options</replaceable> ) ] |
6566 UNIQUE <replaceable class="PARAMETER">index_parameters</replaceable> |
6667 PRIMARY KEY <replaceable class="PARAMETER">index_parameters</replaceable> |
6768 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
8182
8283<phrase>and <replaceable class="PARAMETER">like_option</replaceable> is:</phrase>
8384
84- { INCLUDING | EXCLUDING } { DEFAULTS | CONSTRAINTS | INDEXES | STORAGE | COMMENTS | ALL }
85+ { INCLUDING | EXCLUDING } { DEFAULTS | CONSTRAINTS | IDENTITY | INDEXES | STORAGE | COMMENTS | ALL }
8586
8687<phrase>and <replaceable class="PARAMETER">partition_bound_spec</replaceable> is:</phrase>
8788
@@ -412,6 +413,11 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI
412413 Column <literal>STORAGE</> settings are also copied from parent tables.
413414 </para>
414415
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>
415421 </listitem>
416422 </varlistentry>
417423
@@ -480,6 +486,12 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI
480486 such as <function>nextval</>, may create a functional linkage between
481487 the original and new tables.
482488 </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>
483495 <para>
484496 Not-null constraints are always copied to the new table.
485497 <literal>CHECK</literal> constraints will be copied only if
@@ -512,7 +524,7 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI
512524 </para>
513525 <para>
514526 <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>.
516528 </para>
517529 <para>
518530 Note that unlike <literal>INHERITS</literal>, columns and
@@ -626,6 +638,37 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI
626638 </listitem>
627639 </varlistentry>
628640
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+
629672 <varlistentry>
630673 <term><literal>UNIQUE</> (column constraint)</term>
631674 <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
12631306
12641307 <para>
12651308 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
12671310 generator as the table's primary key is preferred. However, if
12681311 your application does make use of OIDs to identify specific
12691312 rows of a table, it is recommended to create a unique constraint
@@ -1323,7 +1366,7 @@ CREATE TABLE films (
13231366);
13241367
13251368CREATE TABLE distributors (
1326- did integer PRIMARY KEY DEFAULT nextval('serial') ,
1369+ did integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY ,
13271370 name varchar(40) NOT NULL CHECK (name <> '')
13281371);
13291372</programlisting>
@@ -1737,6 +1780,20 @@ CREATE TABLE cities_ab_10000_to_100000
17371780 </para>
17381781 </refsect2>
17391782
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+
17401797 <refsect2>
17411798 <title><literal>LIKE</> Clause</title>
17421799
0 commit comments