Skip to content

Commit abcb32d

Browse files
committed
Improve documentation about PRIMARY KEY constraints.
Get rid of the false implication that PRIMARY KEY is exactly equivalent to UNIQUE + NOT NULL. That was more-or-less true at one time in our implementation, but the standard doesn't say that, and we've grown various features (many of them required by spec) that treat a pkey differently from less-formal constraints. Per recent discussion on pgsql-general. I failed to resist the temptation to do some other wordsmithing in the same area.
1 parent dd48a39 commit abcb32d

File tree

2 files changed

+44
-40
lines changed

2 files changed

+44
-40
lines changed

doc/src/sgml/ddl.sgml

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -495,8 +495,8 @@ CREATE TABLE products (
495495
</indexterm>
496496

497497
<para>
498-
Unique constraints ensure that the data contained in a column or a
499-
group of columns is unique with respect to all the rows in the
498+
Unique constraints ensure that the data contained in a column, or a
499+
group of columns, is unique among all the rows in the
500500
table. The syntax is:
501501
<programlisting>
502502
CREATE TABLE products (
@@ -518,8 +518,8 @@ CREATE TABLE products (
518518
</para>
519519

520520
<para>
521-
If a unique constraint refers to a group of columns, the columns
522-
are listed separated by commas:
521+
To define a unique constraint for a group of columns, write it as a
522+
table constraint with the column names separated by commas:
523523
<programlisting>
524524
CREATE TABLE example (
525525
a integer,
@@ -545,8 +545,11 @@ CREATE TABLE products (
545545
</para>
546546

547547
<para>
548-
Adding a unique constraint will automatically create a unique btree
549-
index on the column or group of columns used in the constraint.
548+
Adding a unique constraint will automatically create a unique B-tree
549+
index on the column or group of columns listed in the constraint.
550+
A uniqueness restriction covering only some rows cannot be written as
551+
a unique constraint, but it is possible to enforce such a restriction by
552+
creating a unique <link linkend="indexes-partial">partial index</link>.
550553
</para>
551554

552555
<indexterm>
@@ -555,10 +558,10 @@ CREATE TABLE products (
555558
</indexterm>
556559

557560
<para>
558-
In general, a unique constraint is violated when there is more than
561+
In general, a unique constraint is violated if there is more than
559562
one row in the table where the values of all of the
560563
columns included in the constraint are equal.
561-
However, two null values are not considered equal in this
564+
However, two null values are never considered equal in this
562565
comparison. That means even in the presence of a
563566
unique constraint it is possible to store duplicate
564567
rows that contain a null value in at least one of the constrained
@@ -582,8 +585,9 @@ CREATE TABLE products (
582585
</indexterm>
583586

584587
<para>
585-
Technically, a primary key constraint is simply a combination of a
586-
unique constraint and a not-null constraint. So, the following
588+
A primary key constraint indicates that a column, or group of columns,
589+
can be used as a unique identifier for rows in the table. This
590+
requires that the values be both unique and not null. So, the following
587591
two table definitions accept the same data:
588592
<programlisting>
589593
CREATE TABLE products (
@@ -603,7 +607,7 @@ CREATE TABLE products (
603607
</para>
604608

605609
<para>
606-
Primary keys can also constrain more than one column; the syntax
610+
Primary keys can span more than one column; the syntax
607611
is similar to unique constraints:
608612
<programlisting>
609613
CREATE TABLE example (
@@ -616,31 +620,31 @@ CREATE TABLE example (
616620
</para>
617621

618622
<para>
619-
A primary key indicates that a column or group of columns can be
620-
used as a unique identifier for rows in the table. (This is a
621-
direct consequence of the definition of a primary key. Note that
622-
a unique constraint does not, by itself, provide a unique identifier
623-
because it does not exclude null values.) This is useful both for
624-
documentation purposes and for client applications. For example,
625-
a GUI application that allows modifying row values probably needs
626-
to know the primary key of a table to be able to identify rows
627-
uniquely.
628-
</para>
629-
630-
<para>
631-
Adding a primary key will automatically create a unique btree index
632-
on the column or group of columns used in the primary key.
623+
Adding a primary key will automatically create a unique B-tree index
624+
on the column or group of columns listed in the primary key, and will
625+
force the column(s) to be marked <literal>NOT NULL</>.
633626
</para>
634627

635628
<para>
636629
A table can have at most one primary key. (There can be any number
637-
of unique and not-null constraints, which are functionally the same
638-
thing, but only one can be identified as the primary key.)
630+
of unique and not-null constraints, which are functionally almost the
631+
same thing, but only one can be identified as the primary key.)
639632
Relational database theory
640633
dictates that every table must have a primary key. This rule is
641634
not enforced by <productname>PostgreSQL</productname>, but it is
642635
usually best to follow it.
643636
</para>
637+
638+
<para>
639+
Primary keys are useful both for
640+
documentation purposes and for client applications. For example,
641+
a GUI application that allows modifying row values probably needs
642+
to know the primary key of a table to be able to identify rows
643+
uniquely. There are also various ways in which the database system
644+
makes use of a primary key if one has been declared; for example,
645+
the primary key defines the default target column(s) for foreign keys
646+
referencing its table.
647+
</para>
644648
</sect2>
645649

646650
<sect2 id="ddl-constraints-fk">

doc/src/sgml/ref/create_table.sgml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -493,25 +493,25 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI
493493
<term><literal>PRIMARY KEY ( <replaceable class="PARAMETER">column_name</replaceable> [, ... ] )</> (table constraint)</term>
494494
<listitem>
495495
<para>
496-
The primary key constraint specifies that a column or columns of a table
497-
can contain only unique (non-duplicate), nonnull values.
498-
Technically, <literal>PRIMARY KEY</literal> is merely a
499-
combination of <literal>UNIQUE</> and <literal>NOT NULL</>, but
500-
identifying a set of columns as primary key also provides
501-
metadata about the design of the schema, as a primary key
502-
implies that other tables
503-
can rely on this set of columns as a unique identifier for rows.
496+
The <literal>PRIMARY KEY</> constraint specifies that a column or
497+
columns of a table can contain only unique (non-duplicate), nonnull
498+
values. Only one primary key can be specified for a table, whether as a
499+
column constraint or a table constraint.
504500
</para>
505501

506502
<para>
507-
Only one primary key can be specified for a table, whether as a
508-
column constraint or a table constraint.
503+
The primary key constraint should name a set of columns that is
504+
different from the set of columns named by any unique
505+
constraint defined for the same table. (Otherwise, the unique
506+
constraint is redundant and will be discarded.)
509507
</para>
510508

511509
<para>
512-
The primary key constraint should name a set of columns that is
513-
different from other sets of columns named by any unique
514-
constraint defined for the same table.
510+
<literal>PRIMARY KEY</literal> enforces the same data constraints as
511+
a combination of <literal>UNIQUE</> and <literal>NOT NULL</>, but
512+
identifying a set of columns as the primary key also provides metadata
513+
about the design of the schema, since a primary key implies that other
514+
tables can rely on this set of columns as a unique identifier for rows.
515515
</para>
516516
</listitem>
517517
</varlistentry>

0 commit comments

Comments
 (0)