Newer
Older
Set the referencing column values to NULL.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>SET DEFAULT</term>
<listitem>
<para>
Set the referencing column values to their default value.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>ON UPDATE <replaceable class="parameter">action</replaceable></term>
<listitem>
<para>
The action to do when a referenced column in the referenced
table is being updated to a new value. If the row is updated,
but the referenced column is not changed, no action is done.
There are the following actions.
<variablelist>
<varlistentry>
<term>NO ACTION</term>
<listitem>
<para>
Produce error if foreign key violated. This is the default.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>RESTRICT</term>
<listitem>
<para>
Same as NO ACTION.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>CASCADE</term>
<listitem>
<para>
Update the value of the referencing column to the new value of the
referenced column.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>SET NULL</term>
<listitem>
<para>
Set the referencing column values to NULL.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>SET DEFAULT</term>
<listitem>
<para>
Set the referencing column values to their default value.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term> [ NOT ] DEFERRABLE </term>
<listitem>
<para>
This controls whether the constraint can be deferred to the end
of the transaction. If DEFERRABLE, SET CONSTRAINTS ALL DEFERRED
will cause the foreign key to be checked only at the end of the
transaction. NOT DEFERRABLE is the default.
</para>
</listitem>
</varlistentry>
<term>INITIALLY <replaceable class="parameter">checktime</replaceable></term>
<listitem>
<para>
<replaceable class="parameter">checktime</replaceable> has two possible values
which specify the default time to check the constraint.
<variablelist>
<varlistentry>
<term>DEFERRED</term>
Check constraint only at the end of the transaction.
</varlistentry>
<varlistentry>
<term>IMMEDIATE</term>
Check constraint after each statement. This is the default.
</para>
</listitem>
</varlistentry>
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
</variablelist>
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect3>
<refsect3 id="R3-SQL-REFERENCES-2">
<refsect3info>
<date>2000-02-04</date>
</refsect3info>
<title>
Outputs
</title>
<para>
<variablelist>
<varlistentry>
<term><replaceable>status</replaceable></term>
<listitem>
<para>
<variablelist>
<varlistentry>
<term><computeroutput>
ERROR: <replaceable class="parameter">name</replaceable> referential integrity violation - key referenced from
<replaceable class="parameter">table</replaceable> not found in <replaceable class="parameter">reftable</replaceable>
</computeroutput></term>
<listitem>
<para>
This error occurs at runtime if one tries to insert a value
into a column which does not have a matching column in the
referenced table.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect3>
<refsect3 id="R3-SQL-REFERENCES-3">
<title>Description</title>
<para>
The REFERENCES column constraint specifies that a
column of a table must only contain values which match against
values in a referenced column of a referenced table.
</para>
<para>
A value added to this column is matched against the values of the
referenced table and referenced column using the given match type.
In addition, when the referenced column data is changed, actions
are run upon this column's matching data.
</para>
</refsect3>
<refsect3 id="R3-SQL-REFERENCES-4">
<refsect3info>
<date>1998-09-11</date>
</refsect3info>
<title>
Notes
</title>
<para>
Currently <productname>Postgres</productname> only supports MATCH
columns are supposed to be the columns of a UNIQUE constraint in
the referenced table, however <productname>Postgres</productname>
does not enforce this.
</para>
</refsect3>
</refsect1>
<refsect1 id="R1-SQL-TABLECONSTRAINT-1">
<title>
Table Constraints
<para>
[ CONSTRAINT name ] { PRIMARY KEY | UNIQUE } ( <replaceable class="parameter">column</replaceable> [, ... ] )
[ CONSTRAINT name ] CHECK ( <replaceable>constraint</replaceable> )
[ CONSTRAINT name ] FOREIGN KEY ( <replaceable class="parameter">column</replaceable> [, ... ] )
REFERENCES <replaceable class="parameter">reftable</replaceable> [ ( <replaceable class="parameter">refcolumn</replaceable> [, ... ] ) ]
[ MATCH <replaceable class="parameter">matchtype</replaceable> ]
[ ON DELETE <replaceable class="parameter">action</replaceable> ]
[ ON UPDATE <replaceable class="parameter">action</replaceable> ]
[ [ NOT ] DEFERRABLE ]
[ INITIALLY <replaceable class="parameter">checktime</replaceable> ]
</para>
<refsect2 id="R2-SQL-TABLECONSTRAINT-1">
<title>
Inputs
</title>
<para>
<term><replaceable class="parameter">constraint_name</replaceable></term>
An arbitrary name given to a constraint clause.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">column</replaceable> [, ... ]</term>
The column name(s) for which to define a unique index
and, for PRIMARY KEY, a NOT NULL constraint.
</varlistentry>
<term>CHECK ( <replaceable class="parameter">constraint</replaceable> )</term>
A boolean expression to be evaluated as the constraint.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect2>
<refsect2 id="R2-SQL-TABLECONSTRAINT-2">
<title>
Outputs
</title>
<para>
The possible outputs for the table constraint clause are the same
as for the corresponding portions of the column constraint clause.
</para>
</refsect2>
<refsect2 id="R2-SQL-TABLECONSTRAINT-3">
<title>
Description
</title>
<para>
A table constraint is an integrity constraint defined on one or
more columns of a table. The four variations of <quote>Table
Constraint</quote> are:
<simplelist columns="1">
<member>UNIQUE</member>
<member>CHECK</member>
<member>PRIMARY KEY</member>
<member>FOREIGN KEY</member>
</simplelist>
</para>
</refsect2>
<refsect2 id="R2-SQL-UNIQUECLAUSE-4">
<title>
<synopsis>
[ CONSTRAINT <replaceable class="parameter">constraint_name</replaceable> ] UNIQUE ( <replaceable class="parameter">column</replaceable> [, ... ] )
</para>
<variablelist>
<varlistentry>
<term><replaceable class="parameter">constraint_name</replaceable></term>
<listitem>
<para>
An arbitrary name given to a constraint clause.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">column</replaceable></term>
<listitem>
<para>
A name of a column in a table.
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect3>
<refsect3>
<title>Outputs</title>
<para>
<variablelist>
<varlistentry>
<term><replaceable>status</replaceable></term>
<listitem>
<para>
<variablelist>
<varlistentry>
<term>ERROR: Cannot insert a duplicate key into a unique index</term>
<listitem>
<para>
This error occurs at runtime if one tries to insert a
duplicate value into a column.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
Description
The UNIQUE constraint specifies a rule that a group of one or more
distinct columns of a table may contain only unique values. The
behavior of the UNIQUE table constraint is the same as that for
column constraints, with the additional capability to span multiple
columns.
See the section on the UNIQUE column constraint for more details.
</para>
</refsect3>
<refsect3 id="R3-SQL-UNIQUECLAUSE-4">
<title>
Usage
</title>
Prevent duplicate rows in the table distributors:
CREATE TABLE distributors (
UNIQUE(did,name)
</para>
</refsect3>
<refsect2 id="R2-SQL-PRIMARYKEY-4">
<title>
<para>
[ CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> ] PRIMARY KEY ( <replaceable class="PARAMETER">column</replaceable> [, ... ] )
</para>
<term><replaceable class="PARAMETER">constraint_name</replaceable></term>
An arbitrary name given to a constraint clause.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="PARAMETER">column</replaceable> [, ... ]</term>
The names of one or more columns in the table.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect3>
<refsect3>
<title>Outputs</title>
<term><replaceable>status</replaceable></term>
<listitem>
<para>
<variablelist>
<varlistentry>
<term>ERROR: Cannot insert a duplicate key into a unique index.</term>
<listitem>
<para>
This occurs at run-time if one tries to insert a duplicate
value into a column subject to a PRIMARY KEY constraint.
</listitem>
</varlistentry>
</variablelist>
</para>
</variablelist>
</refsect3>
<refsect3>
<title>Description</title>
The PRIMARY KEY constraint specifies a rule that a group of one
or more distinct columns of a table may contain only unique
(nonduplicate), non-null values. The column definitions of
the specified columns do not have to include a NOT NULL
constraint to be included in a PRIMARY KEY constraint.
</para>
<para>
The PRIMARY KEY table constraint is similar to that for column constraints,
with the additional capability of encompassing multiple columns.
Refer to the section on the PRIMARY KEY column constraint for more
information.
</para>
<refsect2 id="R2-SQL-REFERENCES-2">
<refsect2info>
<date>2000-02-04</date>
</refsect2info>
<title>
REFERENCES Constraint
</title>
<synopsis>
[ CONSTRAINT <replaceable class="parameter">constraint_name</replaceable> ] FOREIGN KEY ( <replaceable class="parameter">column</replaceable> [, ... ] )
REFERENCES <replaceable class="parameter">reftable</replaceable> [ ( <replaceable class="parameter">refcolumn</replaceable> [, ... ] ) ]
[ MATCH <replaceable class="parameter">matchtype</replaceable> ]
[ ON DELETE <replaceable class="parameter">action</replaceable> ]
[ ON UPDATE <replaceable class="parameter">action</replaceable> ]
[ [ NOT ] DEFERRABLE ]
[ INITIALLY <replaceable class="parameter">checktime</replaceable> ]
</synopsis>
<para>
The REFERENCES constraint specifies a rule that a column value or set
of column values is
checked against the values in another table.
<refsect3 id="R3-SQL-REFERENCES-5">
<title>Inputs</title>
<para>
<variablelist>
<varlistentry>
<term><replaceable class="PARAMETER">constraint_name</replaceable></term>
<listitem>
<para>
An arbitrary name given to a constraint clause.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="PARAMETER">column</replaceable> [, ... ]</term>
<listitem>
<para>
The names of one or more columns in the table.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">reftable</replaceable></term>
<listitem>
<para>
The table that contains the data to check against.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><replaceable class="parameter">referenced column</replaceable> [, ... ]</term>
<listitem>
<para>
One or more columns in the <replaceable class="parameter">reftable</replaceable>
to check the data against. If this is not specified, the PRIMARY KEY of the
<replaceable class="parameter">reftable</replaceable> is used.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>MATCH <replaceable class="parameter">matchtype</replaceable></term>
<listitem>
<para>
There are three match types: MATCH FULL, MATCH PARTIAL, and a
default match type if none is specified. MATCH FULL will not
allow one column of a multi-column foreign key to be NULL
unless all foreign key columns are NULL. The default MATCH type
allows some foreign key columns to be NULL while other parts
of the foreign key are not NULL. MATCH PARTIAL is currently not
supported.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>ON DELETE <replaceable class="parameter">action</replaceable></term>
<listitem>
<para>
The action to do when a referenced row in the referenced table is being
deleted. There are the following actions.
<variablelist>
<varlistentry>
<term>NO ACTION</term>
<listitem>
<para>
Produce error if foreign key violated. This is the default.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>RESTRICT</term>
<listitem>
<para>
Same as NO ACTION.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>CASCADE</term>
<listitem>
<para>
Delete any rows referencing the deleted row.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>SET NULL</term>
<listitem>
<para>
Set the referencing column values to NULL.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>SET DEFAULT</term>
<listitem>
<para>
Set the referencing column values to their default value.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>ON UPDATE <replaceable class="parameter">action</replaceable></term>
<listitem>
<para>
The action to do when a referenced column in the referenced
table is being updated to a new value. If the row is updated,
but the referenced column is not changed, no action is done.
There are the following actions.
<variablelist>
<varlistentry>
<term>NO ACTION</term>
<listitem>
<para>
Produce error if foreign key violated. This is the default.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>RESTRICT</term>
<listitem>
<para>
Disallow update of row being referenced.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>CASCADE</term>
<listitem>
<para>
Update the value of the referencing column to the new value
of the referenced column.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>SET NULL</term>
<listitem>
<para>
Set the referencing column values to NULL.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>SET DEFAULT</term>
<listitem>
<para>
Set the referencing column values to their default value.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term> [ NOT ] DEFERRABLE </term>
<listitem>
<para>
This controls whether the constraint can be deferred to the end
of the transaction. If DEFERRABLE, SET CONSTRAINTS ALL DEFERRED
will cause the foreign key to be checked only at the end of the
transaction. NOT DEFERRABLE is the default.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>INITIALLY <replaceable class="parameter">checktime</replaceable></term>
<listitem>
<para>
<replaceable class="parameter">checktime</replaceable> has two
possible values which specify the default time to check the
constraint.
<variablelist>
<varlistentry>
<term>IMMEDIATE</term>
Check constraint after each statement. This is the default.
</varlistentry>
<varlistentry>
<term>DEFERRED</term>
Check constraint only at the end of the transaction.
</varlistentry>
</variablelist>
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect3>
<refsect3 id="R3-SQL-REFERENCES-6">
<refsect3info>
<date>2000-02-04</date>
</refsect3info>
<title>
Outputs
</title>
<para>
<variablelist>
<varlistentry>
<term><replaceable>status</replaceable></term>
<listitem>
<para>
<variablelist>
<varlistentry>
<term><computeroutput>
ERROR: <replaceable class="parameter">name</replaceable> referential integrity violation - key referenced from
<replaceable class="parameter">table</replaceable> not found in <replaceable class="parameter">reftable</replaceable>
</computeroutput></term>
<listitem>
<para>
This error occurs at runtime if one tries to insert a value
into a column which does not have a matching column in the
referenced table.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect3>
<refsect3>
<title>Description</title>
<para>
The FOREIGN KEY constraint specifies a rule that a group of one
or more distinct columns of a table is related to a group
of distinct columns in the referenced table.
</para>
<para>
The FOREIGN KEY table constraint is similar to that for column
constraints, with the additional capability of encompassing
multiple columns.
</para>
<para>
Refer to the section on the FOREIGN KEY column constraint for more
information.
</para>
</refsect3>
<refsect1 id="R1-SQL-CREATETABLE-2">
<title>
Create table films and table distributors:
<programlisting>
CREATE TABLE films (
code CHARACTER(5) CONSTRAINT firstkey PRIMARY KEY,
title CHARACTER VARYING(40) NOT NULL,
did DECIMAL(3) NOT NULL,
date_prod DATE,
kind CHAR(10),
len INTERVAL HOUR TO MINUTE
);
</programlisting>
<programlisting>
CREATE TABLE distributors (
name VARCHAR(40) NOT NULL CHECK (name <> '')
);
</programlisting>
<para>
Create a table with a 2-dimensional array:
<programlisting>
CREATE TABLE array (
vector INT[][]
);
</programlisting>
</para>
Define a UNIQUE table constraint for the table films.
UNIQUE table constraints can be defined on one or more
columns of the table:
<programlisting>
CREATE TABLE films (
code CHAR(5),
title VARCHAR(40),
date_prod DATE,
kind CHAR(10),
len INTERVAL HOUR TO MINUTE,
CONSTRAINT production UNIQUE(date_prod)
);
</programlisting>
Define a CHECK column constraint:
<programlisting>
CREATE TABLE distributors (
did DECIMAL(3) CHECK (did > 100),
name VARCHAR(40)
);
</programlisting>
Define a CHECK table constraint:
<programlisting>
CREATE TABLE distributors (
did DECIMAL(3),
name VARCHAR(40)
CONSTRAINT con1 CHECK (did > 100 AND name > '')
);
</programlisting>
Define a PRIMARY KEY table constraint for the table films.
PRIMARY KEY table constraints can be defined on one or more
columns of the table:
<programlisting>
CREATE TABLE films (
title VARCHAR(40),
date_prod DATE,
kind CHAR(10),
len INTERVAL HOUR TO MINUTE,
CONSTRAINT code_title PRIMARY KEY(code,title)
);
</programlisting>
Defines a PRIMARY KEY column constraint for table distributors.
PRIMARY KEY column constraints can only be defined on one column
of the table (the following two examples are equivalent):
<programlisting>
CREATE TABLE distributors (
name CHAR VARYING(40),
PRIMARY KEY(did)
);
</programlisting>
<programlisting>
CREATE TABLE distributors (
name VARCHAR(40)
);
</programlisting>
<refsect1 id="R1-SQL-CREATETABLE-3">
<title>
<refsect2 id="R2-SQL-CREATETABLE-4">
<title>
In addition to the locally visible temporary table, SQL92 also defines a
CREATE GLOBAL TEMPORARY TABLE statement, and optionally an
ON COMMIT clause:
CREATE GLOBAL TEMPORARY TABLE <replaceable class="parameter">table</replaceable> ( <replaceable class="parameter">column</replaceable> <replaceable class="parameter">type</replaceable> [
DEFAULT <replaceable class="parameter">value</replaceable> ] [ CONSTRAINT <replaceable class="parameter">column_constraint</replaceable> ] [, ... ] )
[ CONSTRAINT <replaceable class="parameter">table_constraint</replaceable> ] [ ON COMMIT { DELETE | PRESERVE } ROWS ]
For temporary tables, the CREATE GLOBAL TEMPORARY TABLE statement
names a new table visible to other clients and defines the table's columns
and constraints.
The optional ON COMMIT clause of CREATE TEMPORARY TABLE specifies
whether or not the temporary table should be emptied of rows
whenever COMMIT is executed. If the ON COMMIT clause is omitted, SQL92
specifies that the default is ON COMMIT DELETE ROWS. However,
<productname>Postgres</productname>' behavior is always like
ON COMMIT PRESERVE ROWS.
<refsect3 id="R3-SQL-UNIQUECLAUSE-1">
<title>
SQL92 specifies some additional capabilities for UNIQUE:
</para>
Table Constraint definition:
<synopsis>
[ CONSTRAINT <replaceable>constraint_name</replaceable> ] UNIQUE ( <replaceable>column</replaceable> [, ... ] )
[ { INITIALLY DEFERRED | INITIALLY IMMEDIATE } ]
[ [ NOT ] DEFERRABLE ]
</synopsis>
Column Constraint definition:
<synopsis>
[ CONSTRAINT <replaceable>constraint_name</replaceable> ] UNIQUE
[ {INITIALLY DEFERRED | INITIALLY IMMEDIATE} ]
[ [ NOT ] DEFERRABLE ]
</synopsis>
</para>
<refsect3 id="R3-SQL-NULL-1">
<title>
NULL clause
The NULL <quote>constraint</quote> (actually a non-constraint) is a
<productname>Postgres</productname> extension to SQL92 that is
included for symmetry with the NOT NULL clause (and for compatibility
with some other RDBMSes). Since it is the
default for any column, its presence is simply noise.
[ CONSTRAINT <replaceable>constraint_name</replaceable> ] NULL
</synopsis>
</para>
<refsect3 id="R3-SQL-NOTNULL-4">
<title>
SQL92 specifies some additional capabilities for NOT NULL:
[ CONSTRAINT <replaceable>constraint_name</replaceable> ] NOT NULL
[ {INITIALLY DEFERRED | INITIALLY IMMEDIATE} ]
[ [ NOT ] DEFERRABLE ]
</synopsis>
Thomas G. Lockhart
committed
<!--
I can't figure out why DEFAULT clause is different from what we already have.
Perhaps because CURRENT_USER and CURRENT_DATE have specific types (currently
the <type>name</type> type), if you aren't careful then the types won't match up with
Thomas G. Lockhart
committed
the column. Not our problem...
- Thomas 1998-08-16
<REFSECT3 ID="R3-SQL-DEFAULTCLAUSE-1">
<TITLE>
DEFAULT clause
</TITLE>
<PARA>
SQL92 specifies some additional capabilities for the DEFAULT clause.
A DEFAULT clause is used to set the default value for a column
or a domain.
</para>
<synopsis>
DEFAULT niladic_user_function | niladic_datetime_function | NULL
</synopsis>
</refsect3>
Thomas G. Lockhart
committed
-->
<refsect3 id="R3-SQL-CONSTRAINT-3">
<title>
SQL92 specifies some additional capabilities for constraints,
Thomas G. Lockhart
committed
and also defines assertions and domain constraints.
<note>
<para>
<productname>Postgres</productname> does not yet support
either domains or assertions.
</para>
</note>
An assertion is a special type of integrity constraint and shares
the same namespace as other constraints. However, an assertion is
not necessarily dependent on one particular table as
constraints are, so SQL-92 provides the CREATE ASSERTION statement
as an alternate method for defining a constraint:
</para>
<synopsis>
CREATE ASSERTION <replaceable>name</replaceable> CHECK ( <replaceable>condition</replaceable> )
Domain constraints are defined by CREATE DOMAIN or ALTER DOMAIN
statements:
</para>
[ CONSTRAINT <replaceable>constraint_name</replaceable> ] CHECK <replaceable>constraint</replaceable>
[ {INITIALLY DEFERRED | INITIALLY IMMEDIATE} ]
[ [ NOT ] DEFERRABLE ]
</synopsis>
<para>
Table constraint definition:
[ CONSTRAINT <replaceable>constraint_name</replaceable> ] { PRIMARY KEY ( <replaceable class="parameter">column</replaceable>, ... ) | FOREIGN KEY <replaceable>constraint</replaceable> | UNIQUE <replaceable>constraint</replaceable> | CHECK <replaceable>constraint</replaceable> }
[ {INITIALLY DEFERRED | INITIALLY IMMEDIATE} ]
[ [ NOT ] DEFERRABLE ]
</synopsis>
<para>
Column constraint definition: