Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c477e84

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 cc2ca93 commit c477e84

File tree

2 files changed

+42
-40
lines changed

2 files changed

+42
-40
lines changed

doc/src/sgml/ddl.sgml

+29-27
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,
@@ -546,9 +546,10 @@ CREATE TABLE products (
546546

547547
<para>
548548
Adding a unique constraint will automatically create a unique B-tree
549-
index on the column or group of columns used in the constraint.
550-
A uniqueness constraint on only some rows can be enforced by creating
551-
a <link linkend="indexes-partial">partial index</link>.
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>.
552553
</para>
553554

554555
<indexterm>
@@ -557,10 +558,10 @@ CREATE TABLE products (
557558
</indexterm>
558559

559560
<para>
560-
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
561562
one row in the table where the values of all of the
562563
columns included in the constraint are equal.
563-
However, two null values are not considered equal in this
564+
However, two null values are never considered equal in this
564565
comparison. That means even in the presence of a
565566
unique constraint it is possible to store duplicate
566567
rows that contain a null value in at least one of the constrained
@@ -584,8 +585,9 @@ CREATE TABLE products (
584585
</indexterm>
585586

586587
<para>
587-
Technically, a primary key constraint is simply a combination of a
588-
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
589591
two table definitions accept the same data:
590592
<programlisting>
591593
CREATE TABLE products (
@@ -605,7 +607,7 @@ CREATE TABLE products (
605607
</para>
606608

607609
<para>
608-
Primary keys can also constrain more than one column; the syntax
610+
Primary keys can span more than one column; the syntax
609611
is similar to unique constraints:
610612
<programlisting>
611613
CREATE TABLE example (
@@ -617,32 +619,32 @@ CREATE TABLE example (
617619
</programlisting>
618620
</para>
619621

620-
<para>
621-
A primary key indicates that a column or group of columns can be
622-
used as a unique identifier for rows in the table. (This is a
623-
direct consequence of the definition of a primary key. Note that
624-
a unique constraint does not, by itself, provide a unique identifier
625-
because it does not exclude null values.) This is useful both for
626-
documentation purposes and for client applications. For example,
627-
a GUI application that allows modifying row values probably needs
628-
to know the primary key of a table to be able to identify rows
629-
uniquely.
630-
</para>
631-
632622
<para>
633623
Adding a primary key will automatically create a unique B-tree index
634-
on the column or group of columns used in the primary key.
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</>.
635626
</para>
636627

637628
<para>
638629
A table can have at most one primary key. (There can be any number
639-
of unique and not-null constraints, which are functionally the same
640-
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.)
641632
Relational database theory
642633
dictates that every table must have a primary key. This rule is
643634
not enforced by <productname>PostgreSQL</productname>, but it is
644635
usually best to follow it.
645636
</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>
646648
</sect2>
647649

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

doc/src/sgml/ref/create_table.sgml

+13-13
Original file line numberDiff line numberDiff line change
@@ -506,25 +506,25 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI
506506
<term><literal>PRIMARY KEY ( <replaceable class="PARAMETER">column_name</replaceable> [, ... ] )</> (table constraint)</term>
507507
<listitem>
508508
<para>
509-
The primary key constraint specifies that a column or columns of a table
510-
can contain only unique (non-duplicate), nonnull values.
511-
Technically, <literal>PRIMARY KEY</literal> is merely a
512-
combination of <literal>UNIQUE</> and <literal>NOT NULL</>, but
513-
identifying a set of columns as primary key also provides
514-
metadata about the design of the schema, as a primary key
515-
implies that other tables
516-
can rely on this set of columns as a unique identifier for rows.
509+
The <literal>PRIMARY KEY</> constraint specifies that a column or
510+
columns of a table can contain only unique (non-duplicate), nonnull
511+
values. Only one primary key can be specified for a table, whether as a
512+
column constraint or a table constraint.
517513
</para>
518514

519515
<para>
520-
Only one primary key can be specified for a table, whether as a
521-
column constraint or a table constraint.
516+
The primary key constraint should name a set of columns that is
517+
different from the set of columns named by any unique
518+
constraint defined for the same table. (Otherwise, the unique
519+
constraint is redundant and will be discarded.)
522520
</para>
523521

524522
<para>
525-
The primary key constraint should name a set of columns that is
526-
different from other sets of columns named by any unique
527-
constraint defined for the same table.
523+
<literal>PRIMARY KEY</literal> enforces the same data constraints as
524+
a combination of <literal>UNIQUE</> and <literal>NOT NULL</>, but
525+
identifying a set of columns as the primary key also provides metadata
526+
about the design of the schema, since a primary key implies that other
527+
tables can rely on this set of columns as a unique identifier for rows.
528528
</para>
529529
</listitem>
530530
</varlistentry>

0 commit comments

Comments
 (0)