diff --git a/doc/src/sgml/ddl.sgml b/doc/src/sgml/ddl.sgml index 2adeeff3eb..6a3b622098 100644 --- a/doc/src/sgml/ddl.sgml +++ b/doc/src/sgml/ddl.sgml @@ -495,8 +495,8 @@ CREATE TABLE products ( - Unique constraints ensure that the data contained in a column or a - group of columns is unique with respect to all the rows in the + Unique constraints ensure that the data contained in a column, or a + group of columns, is unique among all the rows in the table. The syntax is: CREATE TABLE products ( @@ -518,8 +518,8 @@ CREATE TABLE products ( - If a unique constraint refers to a group of columns, the columns - are listed separated by commas: + To define a unique constraint for a group of columns, write it as a + table constraint with the column names separated by commas: CREATE TABLE example ( a integer, @@ -546,9 +546,10 @@ CREATE TABLE products ( Adding a unique constraint will automatically create a unique B-tree - index on the column or group of columns used in the constraint. - A uniqueness constraint on only some rows can be enforced by creating - a partial index. + index on the column or group of columns listed in the constraint. + A uniqueness restriction covering only some rows cannot be written as + a unique constraint, but it is possible to enforce such a restriction by + creating a unique partial index. @@ -557,10 +558,10 @@ CREATE TABLE products ( - In general, a unique constraint is violated when there is more than + In general, a unique constraint is violated if there is more than one row in the table where the values of all of the columns included in the constraint are equal. - However, two null values are not considered equal in this + However, two null values are never considered equal in this comparison. That means even in the presence of a unique constraint it is possible to store duplicate rows that contain a null value in at least one of the constrained @@ -584,8 +585,9 @@ CREATE TABLE products ( - Technically, a primary key constraint is simply a combination of a - unique constraint and a not-null constraint. So, the following + A primary key constraint indicates that a column, or group of columns, + can be used as a unique identifier for rows in the table. This + requires that the values be both unique and not null. So, the following two table definitions accept the same data: CREATE TABLE products ( @@ -605,7 +607,7 @@ CREATE TABLE products ( - Primary keys can also constrain more than one column; the syntax + Primary keys can span more than one column; the syntax is similar to unique constraints: CREATE TABLE example ( @@ -617,32 +619,32 @@ CREATE TABLE example ( - - A primary key indicates that a column or group of columns can be - used as a unique identifier for rows in the table. (This is a - direct consequence of the definition of a primary key. Note that - a unique constraint does not, by itself, provide a unique identifier - because it does not exclude null values.) This is useful both for - documentation purposes and for client applications. For example, - a GUI application that allows modifying row values probably needs - to know the primary key of a table to be able to identify rows - uniquely. - - Adding a primary key will automatically create a unique B-tree index - on the column or group of columns used in the primary key. + on the column or group of columns listed in the primary key, and will + force the column(s) to be marked NOT NULL. A table can have at most one primary key. (There can be any number - of unique and not-null constraints, which are functionally the same - thing, but only one can be identified as the primary key.) + of unique and not-null constraints, which are functionally almost the + same thing, but only one can be identified as the primary key.) Relational database theory dictates that every table must have a primary key. This rule is not enforced by PostgreSQL, but it is usually best to follow it. + + + Primary keys are useful both for + documentation purposes and for client applications. For example, + a GUI application that allows modifying row values probably needs + to know the primary key of a table to be able to identify rows + uniquely. There are also various ways in which the database system + makes use of a primary key if one has been declared; for example, + the primary key defines the default target column(s) for foreign keys + referencing its table. + diff --git a/doc/src/sgml/ref/create_table.sgml b/doc/src/sgml/ref/create_table.sgml index f0c94d591e..cd234dbf4e 100644 --- a/doc/src/sgml/ref/create_table.sgml +++ b/doc/src/sgml/ref/create_table.sgml @@ -506,25 +506,25 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI PRIMARY KEY ( column_name [, ... ] ) (table constraint) - The primary key constraint specifies that a column or columns of a table - can contain only unique (non-duplicate), nonnull values. - Technically, PRIMARY KEY is merely a - combination of UNIQUE and NOT NULL, but - identifying a set of columns as primary key also provides - metadata about the design of the schema, as a primary key - implies that other tables - can rely on this set of columns as a unique identifier for rows. - - - - Only one primary key can be specified for a table, whether as a + The PRIMARY KEY constraint specifies that a column or + columns of a table can contain only unique (non-duplicate), nonnull + values. Only one primary key can be specified for a table, whether as a column constraint or a table constraint. The primary key constraint should name a set of columns that is - different from other sets of columns named by any unique - constraint defined for the same table. + different from the set of columns named by any unique + constraint defined for the same table. (Otherwise, the unique + constraint is redundant and will be discarded.) + + + + PRIMARY KEY enforces the same data constraints as + a combination of UNIQUE and NOT NULL, but + identifying a set of columns as the primary key also provides metadata + about the design of the schema, since a primary key implies that other + tables can rely on this set of columns as a unique identifier for rows.