Skip to content

Commit 5303706

Browse files
committed
Doc: clarify that CREATE TABLE discards redundant unique constraints.
The SQL standard says that redundant unique constraints are disallowed, but we long ago decided that throwing an error would be too user-unfriendly, so we just drop redundant ones. The docs weren't very clear about that though, as this behavior was only explained for PRIMARY KEY vs UNIQUE, not UNIQUE vs UNIQUE. While here, I couldn't resist doing some copy-editing and markup-fixing on the adjacent text about INCLUDE options. Per bug #16767 from Matthias vd Meent. Discussion: https://postgr.es/m/16767-1714a2056ca516d0@postgresql.org
1 parent 10c6015 commit 5303706

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

doc/src/sgml/ref/create_table.sgml

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -776,15 +776,17 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
776776
<varlistentry>
777777
<term><literal>UNIQUE</literal> (column constraint)</term>
778778
<term><literal>UNIQUE ( <replaceable class="parameter">column_name</replaceable> [, ... ] )</literal>
779-
<optional> INCLUDE ( <replaceable class="parameter">column_name</replaceable> [, ...]) </optional> (table constraint)</term>
779+
<optional> <literal>INCLUDE ( <replaceable class="parameter">column_name</replaceable> [, ...])</literal> </optional> (table constraint)</term>
780780

781781
<listitem>
782782
<para>
783783
The <literal>UNIQUE</literal> constraint specifies that a
784784
group of one or more columns of a table can contain
785-
only unique values. The behavior of the unique table constraint
786-
is the same as that for column constraints, with the additional
787-
capability to span multiple columns.
785+
only unique values. The behavior of a unique table constraint
786+
is the same as that of a unique column constraint, with the
787+
additional capability to span multiple columns. The constraint
788+
therefore enforces that any two rows must differ in at least one
789+
of these columns.
788790
</para>
789791

790792
<para>
@@ -793,10 +795,10 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
793795
</para>
794796

795797
<para>
796-
Each unique table constraint must name a set of columns that is
798+
Each unique constraint should name a set of columns that is
797799
different from the set of columns named by any other unique or
798-
primary key constraint defined for the table. (Otherwise it
799-
would just be the same constraint listed twice.)
800+
primary key constraint defined for the table. (Otherwise, redundant
801+
unique constraints will be discarded.)
800802
</para>
801803

802804
<para>
@@ -809,10 +811,15 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
809811
<para>
810812
Adding a unique constraint will automatically create a unique btree
811813
index on the column or group of columns used in the constraint.
812-
The optional clause <literal>INCLUDE</literal> adds to that index
813-
one or more columns on which the uniqueness is not enforced.
814-
Note that although the constraint is not enforced on the included columns,
815-
it still depends on them. Consequently, some operations on these columns
814+
</para>
815+
816+
<para>
817+
The optional <literal>INCLUDE</literal> clause adds to that index
818+
one or more columns that are simply <quote>payload</quote>: uniqueness
819+
is not enforced on them, and the index cannot be searched on the basis
820+
of those columns. However they can be retrieved by an index-only scan.
821+
Note that although the constraint is not enforced on included columns,
822+
it still depends on them. Consequently, some operations on such columns
816823
(e.g., <literal>DROP COLUMN</literal>) can cause cascaded constraint and
817824
index deletion.
818825
</para>
@@ -822,7 +829,7 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
822829
<varlistentry>
823830
<term><literal>PRIMARY KEY</literal> (column constraint)</term>
824831
<term><literal>PRIMARY KEY ( <replaceable class="parameter">column_name</replaceable> [, ... ] )</literal>
825-
<optional> INCLUDE ( <replaceable class="parameter">column_name</replaceable> [, ...]) </optional> (table constraint)</term>
832+
<optional> <literal>INCLUDE ( <replaceable class="parameter">column_name</replaceable> [, ...])</literal> </optional> (table constraint)</term>
826833
<listitem>
827834
<para>
828835
The <literal>PRIMARY KEY</literal> constraint specifies that a column or
@@ -840,27 +847,34 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
840847

841848
<para>
842849
<literal>PRIMARY KEY</literal> enforces the same data constraints as
843-
a combination of <literal>UNIQUE</literal> and <literal>NOT NULL</literal>, but
850+
a combination of <literal>UNIQUE</literal> and <literal>NOT
851+
NULL</literal>. However,
844852
identifying a set of columns as the primary key also provides metadata
845853
about the design of the schema, since a primary key implies that other
846854
tables can rely on this set of columns as a unique identifier for rows.
847855
</para>
848856

849857
<para>
850-
<literal>PRIMARY KEY</literal> constraints share the restrictions that
851-
<literal>UNIQUE</literal> constraints have when placed on partitioned
852-
tables.
858+
When placed on a partitioned table, <literal>PRIMARY KEY</literal>
859+
constraints share the restrictions previously decribed
860+
for <literal>UNIQUE</literal> constraints.
853861
</para>
854862

855863
<para>
856864
Adding a <literal>PRIMARY KEY</literal> constraint will automatically
857865
create a unique btree index on the column or group of columns used in the
858-
constraint. The optional <literal>INCLUDE</literal> clause allows a list
859-
of columns to be specified which will be included in the non-key portion
860-
of the index. Although uniqueness is not enforced on the included columns,
861-
the constraint still depends on them. Consequently, some operations on the
862-
included columns (e.g., <literal>DROP COLUMN</literal>) can cause cascaded
863-
constraint and index deletion.
866+
constraint.
867+
</para>
868+
869+
<para>
870+
The optional <literal>INCLUDE</literal> clause adds to that index
871+
one or more columns that are simply <quote>payload</quote>: uniqueness
872+
is not enforced on them, and the index cannot be searched on the basis
873+
of those columns. However they can be retrieved by an index-only scan.
874+
Note that although the constraint is not enforced on included columns,
875+
it still depends on them. Consequently, some operations on such columns
876+
(e.g., <literal>DROP COLUMN</literal>) can cause cascaded constraint and
877+
index deletion.
864878
</para>
865879
</listitem>
866880
</varlistentry>

0 commit comments

Comments
 (0)