@@ -495,8 +495,8 @@ CREATE TABLE products (
495
495
</indexterm>
496
496
497
497
<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
500
500
table. The syntax is:
501
501
<programlisting>
502
502
CREATE TABLE products (
@@ -518,8 +518,8 @@ CREATE TABLE products (
518
518
</para>
519
519
520
520
<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:
523
523
<programlisting>
524
524
CREATE TABLE example (
525
525
a integer,
@@ -546,9 +546,10 @@ CREATE TABLE products (
546
546
547
547
<para>
548
548
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>.
552
553
</para>
553
554
554
555
<indexterm>
@@ -557,10 +558,10 @@ CREATE TABLE products (
557
558
</indexterm>
558
559
559
560
<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
561
562
one row in the table where the values of all of the
562
563
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
564
565
comparison. That means even in the presence of a
565
566
unique constraint it is possible to store duplicate
566
567
rows that contain a null value in at least one of the constrained
@@ -584,8 +585,9 @@ CREATE TABLE products (
584
585
</indexterm>
585
586
586
587
<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
589
591
two table definitions accept the same data:
590
592
<programlisting>
591
593
CREATE TABLE products (
@@ -605,7 +607,7 @@ CREATE TABLE products (
605
607
</para>
606
608
607
609
<para>
608
- Primary keys can also constrain more than one column; the syntax
610
+ Primary keys can span more than one column; the syntax
609
611
is similar to unique constraints:
610
612
<programlisting>
611
613
CREATE TABLE example (
@@ -617,32 +619,32 @@ CREATE TABLE example (
617
619
</programlisting>
618
620
</para>
619
621
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
-
632
622
<para>
633
623
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</>.
635
626
</para>
636
627
637
628
<para>
638
629
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.)
641
632
Relational database theory
642
633
dictates that every table must have a primary key. This rule is
643
634
not enforced by <productname>PostgreSQL</productname>, but it is
644
635
usually best to follow it.
645
636
</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>
646
648
</sect2>
647
649
648
650
<sect2 id="ddl-constraints-fk">
0 commit comments