Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit73ed2a5

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

File tree

2 files changed

+44
-42
lines changed

2 files changed

+44
-42
lines changed

‎doc/src/sgml/ddl.sgml

Lines changed: 31 additions & 29 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 uniquewith 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 uniqueamong 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-
Ifa unique constraintrefers toa group of columns,the columns
522-
are listed separated by commas:
521+
To definea unique constraintfora 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,10 +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.
550-
A uniqueness constraint on only some rows can be enforced by creating
551-
a <link linkend="indexes-partial">partial index</link>.
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>.
552553
</para>
553554

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

559560
<para>
560-
In general, a unique constraint is violatedwhen there is more than
561+
In general, a unique constraint is violatedif 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 arenot considered equal in this
564+
However, two null values arenever 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 canalso constrain more than one column; the syntax
610+
Primary keys canspan more than one column; the syntax
609611
is similar to unique constraints:
610612
<programlisting>
611613
CREATE TABLE example (
@@ -618,31 +620,31 @@ CREATE TABLE example (
618620
</para>
619621

620622
<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-
<para>
633-
Adding a primary key will automatically create a unique btree index
634-
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</>.
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 functionallythe same
640-
thing, but only one can be identified as the primary key.)
630+
of unique and not-null constraints, which are functionallyalmost the
631+
samething, 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

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

510506
<para>
511-
Only one primary key can be specified for a table, whether as a
512-
column constraint or a table constraint.
507+
The primary key constraint should name a set of columns that is
508+
different from the set of columns named by any unique
509+
constraint defined for the same table. (Otherwise, the unique
510+
constraint is redundant and will be discarded.)
513511
</para>
514512

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp