Skip to content
Snippets Groups Projects
create_table.sgml 64.2 KiB
Newer Older
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_table.sgml,v 1.47 2001/10/09 18:46:00 petere Exp $
Postgres documentation
-->

<refentry id="SQL-CREATETABLE">
 <refmeta>
  <refentrytitle id="sql-createtable-title">
  </refentrytitle>
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 </refmeta>
 <refnamediv>
  <refname>
  </refname>
  <refpurpose>
  </refpurpose>
 <refsynopsisdiv>
  <refsynopsisdivinfo>
  </refsynopsisdivinfo>
  <synopsis>
CREATE [ TEMPORARY | TEMP ] TABLE <replaceable class="PARAMETER">table_name</replaceable> (
    { <replaceable class="PARAMETER">column_name</replaceable> <replaceable class="PARAMETER">type</replaceable> [ <replaceable class="PARAMETER">column_constraint</replaceable> [ ... ] ]
      | <replaceable>table_constraint</replaceable> }  [, ... ] )
    [ INHERITS ( <replaceable>parent_table</replaceable> [, ... ] ) ]
    [ WITH OIDS | WITHOUT OIDS ]

where <replaceable class="PARAMETER">column_constraint</replaceable> can be:
[ CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> ]
{ NOT NULL | NULL | UNIQUE | PRIMARY KEY | DEFAULT <replaceable class="PARAMETER">value</replaceable> | CHECK (<replaceable class="PARAMETER">condition</replaceable>) |
  REFERENCES <replaceable class="PARAMETER">table</replaceable> [ ( <replaceable class="PARAMETER">column</replaceable> ) ] [ MATCH FULL | MATCH PARTIAL ]
   [ ON DELETE <replaceable class="parameter">action</replaceable> ] [ ON UPDATE <replaceable class="parameter">action</replaceable> ]
   [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
}

and <replaceable class="PARAMETER">table_constraint</replaceable> can be:
[ CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> ]
{ UNIQUE ( <replaceable class="PARAMETER">column_name</replaceable> [, ... ] ) |
  PRIMARY KEY ( <replaceable class="PARAMETER">column_name</replaceable> [, ... ] ) |
  CHECK ( <replaceable class="PARAMETER">condition</replaceable> ) |
  FOREIGN KEY ( <replaceable class="PARAMETER">column_name</replaceable> [, ... ] ) REFERENCES <replaceable class="PARAMETER">table</replaceable> [ ( <replaceable class="PARAMETER">column</replaceable> [, ... ] ) ]
   [ MATCH FULL | MATCH PARTIAL ] [ ON DELETE <replaceable class="parameter">action</replaceable> ] [ ON UPDATE <replaceable class="parameter">action</replaceable> ]
   [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
}
  </synopsis>
  <refsect2 id="R2-SQL-CREATETABLE-1">
   <title>
   </title>

    <variablelist>
     <varlistentry>
      <term>TEMPORARY or TEMP</term>
	If specified, the table is created only for this session, and is
	automatically dropped on session exit.
	Existing permanent tables with the same name are not visible
	(in this session) while the temporary table exists.
	Any indexes created on a temporary table are automatically
	temporary as well.
       </para>
      </listitem>
     </varlistentry>
     <varlistentry>
      <term><replaceable class="PARAMETER">table_name</replaceable></term>
Peter Eisentraut's avatar
Peter Eisentraut committed
	The name of the new table to be created.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><replaceable class="PARAMETER">column_name</replaceable></term>
	The name of a column to be created in the new table.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><replaceable class="PARAMETER">type</replaceable></term>
      <listitem>
       <para>
	The type of the column. This may include array specifiers.
	Refer to the <citetitle>PostgreSQL User's Guide</citetitle> for
	further information about data types and arrays.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><replaceable class="PARAMETER">parent_table</replaceable></term>
      <listitem>
       <para>
	The optional INHERITS clause specifies a list of table
	names from which this table automatically inherits all fields.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term>WITH OIDS or WITHOUT OIDS</term>
      <listitem>
       <para>
        This optional clause specifies whether rows of the new table should
	have OIDs (object identifiers) assigned to them.  The default is
	WITH OIDS.  (If the new table inherits from any tables that have OIDs,
	then WITH OIDS is forced even if the command says WITHOUT OIDS.)
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><replaceable class="PARAMETER">constraint_name</replaceable></term>
      <listitem>
       <para>
        An optional name for a column or table constraint.  If not specified,
	the system generates a name.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><replaceable class="PARAMETER">value</replaceable></term>
      <listitem>
       <para>
	A default value for a column.
	See the DEFAULT clause for more information.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><replaceable class="PARAMETER">condition</replaceable></term>
	CHECK clauses specify integrity constraints or tests which new or
	updated rows must satisfy for an insert or update operation to
	succeed. Each constraint must be an expression producing
	a boolean result.
	A condition appearing within a column definition should reference
	that column's value only, while a condition appearing as a table
	constraint may reference multiple columns.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><replaceable class="PARAMETER">table</replaceable></term>
        The name of an existing table to be referenced by a foreign
	key constraint.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><replaceable class="PARAMETER">column</replaceable></term>
      <listitem>
       <para>
        The name of a column in an existing table to be referenced by a
	foreign key constraint.  If not specified, the primary key of
	the existing table is assumed.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><replaceable class="PARAMETER">action</replaceable></term>
      <listitem>
       <para>
        A keyword indicating the action to take when a foreign key
	constraint is violated.
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
  </refsect2>
  <refsect2 id="R2-SQL-CREATETABLE-2">
   <title>
   </title>
   <para>
    <variablelist>
     <varlistentry>
      <term><computeroutput>
CREATE
       </computeroutput></term>
      <listitem>
       <para>
	Message returned if table is successfully created.
       </para>
      </listitem>
     </varlistentry>
     <varlistentry>
      <term><computeroutput>
ERROR
       </computeroutput></term>
      <listitem>
       <para>
	Message returned if table creation failed.
	This is usually accompanied by some descriptive text, such as:
	<computeroutput>
ERROR:  Relation '<replaceable class="parameter">table</replaceable>' already exists
	</computeroutput>
	, which occurs at runtime if the table specified already exists
	in the database.
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
  </refsect2>
 </refsynopsisdiv>
 <refsect1 id="R1-SQL-CREATETABLE-1">
  <title>
  </title>
   <command>CREATE TABLE</command> will enter a new, initially empty table
   into the current database. The table will be owned by the user issuing the

  <para>
   Each <replaceable class="PARAMETER">type</replaceable>
   may be a simple type, a complex type (set) or an array type.
   Each attribute may be specified to be non-null and
   each may have a default value, specified by the
   <xref linkend="R1-SQL-DEFAULTCLAUSE-1" endterm="R1-SQL-DEFAULTCLAUSE-1-TITLE">.
Bruce Momjian's avatar
Bruce Momjian committed
     Consistent array dimensions within an
     attribute are not enforced. This will likely change in a future
     release.
    </para>
   </note>
  </para>

  <para>
   <command>CREATE TABLE</command> also automatically creates a data type
   that represents the tuple type (structure type) corresponding to one
   row of the table.  Therefore, tables can't have the same name as any
   existing datatype.
  </para>

  <para>
   A table can have no more than 1600 columns (in practice, the
   effective limit is lower because of tuple-length constraints).
   A table cannot have the same name as a system catalog table.
  </para>
 </refsect1>

 <refsect1 id="R1-SQL-INHERITSCLAUSE-1">
  <title id="R1-SQL-INHERITSCLAUSE-1-TITLE">
   INHERITS Clause
  </title>
  <para>
   <synopsis>
INHERITS ( <replaceable class="PARAMETER">parent_table</replaceable> [, ... ] )
   </synopsis>
  </para>

   clause specifies a list of table names from which the new table
   automatically inherits all fields.  If the same field name appears in
   more than one parent table, Postgres reports an error unless the field
   definitions match in each of the parent tables.  If there is no
   definition conflict, then the duplicate fields are merged to form a single
   field of the new table.  If the new table's own field list contains a
   field name that is also inherited, this declaration must likewise match
   the inherited field(s), and the field definitions are merged into one.
   Inherited and new field declarations of the same name must specify exactly
   the same data type to avoid an error.  They need not specify identical
   constraints --- all constraints provided from any declaration are merged
   together and all are applied to the new table.  If the new table explicitly
   specifies a default value for the field, this default overrides any
   defaults from inherited declarations of the field.  Otherwise, any parents
   that specify default values for the field must all specify the same
   default, or an error will be reported.
  </para>

  <para>
   Postgres automatically allows the created table to inherit functions on
   tables above it in the inheritance hierarchy; that is, if we create table
   <literal>foo</literal> inheriting from <literal>bar</literal>, then
   functions that accept the tuple type <literal>bar</literal> can also be
   applied to instances of <literal>foo</literal>.  (Currently, this works
   reliably for functions on the first or only parent table, but not so well
   for functions on additional parents.)
  </para>
 <refsect1 id="R1-SQL-OIDSCLAUSE-1">
  <title id="R1-SQL-OIDSCLAUSE-1-TITLE">
   OIDS Clause
  </title>
  <para>
   <synopsis>
    WITH OIDS | WITHOUT OIDS
   </synopsis>
  </para>

  <para>
   This clause controls whether an OID (object ID) is generated and assigned
   to each row inserted into the table.  The default is WITH OIDS.
   Specifying WITHOUT OIDS allows the user to suppress generation of
   OIDs for rows of a table.  This may be worthwhile for large
   tables, since it will reduce OID consumption and thereby postpone
   wraparound of the 32-bit OID counter.  Once the counter wraps around,
   uniqueness of OIDs can no longer be assumed, which considerably reduces
   their usefulness.
  </para>

  <para>
   Whenever an application makes use of OIDs to identify specific rows of
   a table, it is recommended that you create a unique index on OID for
   that table, to ensure that OIDs in the table will indeed uniquely
   identify rows even after counter wraparound.  (An index on OID is needed
   anyway for fast lookup of rows by OID.)  Avoid assuming that OIDs are
   unique across tables --- if you need a database-wide unique identifier,
   use the combination of tableoid and row OID for the purpose.  (It is
   likely that future Postgres releases will use a separate OID counter
   for each table, so that it will be <emphasis>necessary</> not optional
   to include tableoid to have a unique identifier database-wide.)
  </para>

  <tip>
   <para>
    WITHOUT OIDS is not recommended for tables with no primary key, since
    without either an OID or a unique data key, it is difficult to identify
    specific rows.
   </para>
  </tip>
 </refsect1>

 <refsect1 id="R1-SQL-DEFAULTCLAUSE-1">
  <title id="R1-SQL-DEFAULTCLAUSE-1-TITLE">
   DEFAULT Clause
  </title>
  <para>
   <synopsis>
DEFAULT <replaceable class="PARAMETER">value</replaceable>
  <para>
   The DEFAULT clause assigns a default data value for the column whose
   column definition it appears within.  The value is any variable-free
   expression (note that sub-selects and cross-references to other
   columns in the current table are not supported).
   The data type of a default value must match the column definition's
   data type.
  </para>
  <para>
   The DEFAULT expression will be used in any INSERT operation that does
   not specify a value for the column.  If there is no DEFAULT clause,
   then the default is NULL.
  </para>
  <refsect2 id="R2-SQL-DEFAULTCLAUSE-2">
   <title>
   </title>
CREATE TABLE distributors (
    name     VARCHAR(40) DEFAULT 'luso films',
    did      INTEGER  DEFAULT NEXTVAL('distributors_serial'),
    modtime  TIMESTAMP DEFAULT now()
    The above assigns a literal constant default value for the column
    <literal>name</literal>, and arranges for the default value of column
    <literal>did</literal> to be generated by selecting the next value of a
    sequence object.  The default value of <literal>modtime</literal> will
    be the time at which the row is inserted.
   <para>
    It is worth remarking that
    modtime  TIMESTAMP DEFAULT 'now'
    would produce a result that is probably not the intended one: the
    string <literal>'now'</literal> will be coerced to a timestamp value
    immediately, and so the default value of <literal>modtime</literal> will
    always be the time of table creation.  This difficulty is avoided by
    specifying the default value as a function call.
 </refsect1>

 <refsect1 id="R1-SQL-COLUMNCONSTRAINT-1">
  <title id="R1-SQL-COLUMNCONSTRAINT-1-TITLE">
   <synopsis>
[ CONSTRAINT <replaceable class="parameter">constraint_name</replaceable> ] {
    NULL | NOT NULL | UNIQUE | PRIMARY KEY | CHECK <replaceable
     class="parameter">condition</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>
  <refsect2 id="R2-SQL-COLUMNCONSTRAINT-1">
   <title>
   </title>
   <para>
    <variablelist>
     <varlistentry>
      <term><replaceable class="parameter">constraint_name</replaceable></term>
	An arbitrary name given to a constraint clause.
       </para>
      </listitem>
     </varlistentry>
     <varlistentry>
      <term>NULL</term>
      <listitem>
       <para>
	The column is allowed to contain NULL values. This is the default.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term>NOT NULL</term>
      <listitem>
       <para>
	The column is not allowed to contain NULL values.
	This is equivalent to the column constraint
	CHECK (<replaceable class="PARAMETER">column</replaceable> NOT NULL).
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term>UNIQUE</term>
      <listitem>
       <para>
	The column must have unique values. In <productname>Postgres</productname>
	this is enforced by automatic creation of a unique index on the column.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term>PRIMARY KEY</term>
      <listitem>
       <para>
	This column is a primary key, which implies that other tables may rely
	on this column as a unique identifier for rows.  Both UNIQUE and
	NOT NULL are implied by PRIMARY KEY.  See PRIMARY KEY for more
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term>
<replaceable class="parameter">condition</replaceable>
	An arbitrary boolean-valued constraint condition.
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
   </para>
  <refsect2 id="R2-SQL-COLUMNCONSTRAINT-2">
   <title>
    Description
   </title>
    The optional constraint clauses specify constraints or tests which
    new or updated rows must satisfy for an insert or update
    operation to succeed.
   </para>

   <para>
    A constraint is a named rule: an SQL object which helps define
    valid sets of values by putting limits on the results of INSERT,
    UPDATE or DELETE operations performed on a table. 
   <para>
    There are two ways to define integrity constraints:
    table constraints, covered later, and column constraints, covered here.
   </para>
    A column constraint is an integrity constraint defined as part of a
    column definition, and logically becomes a table constraint as soon
    as it is created. The column constraints available are:
    <simplelist columns="1">
     <member>PRIMARY KEY</member>
     <member>REFERENCES</member>
     <member>UNIQUE</member>
     <member>CHECK</member>
     <member>NOT NULL</member>
    </simplelist>
   </para>
  <refsect2 id="R2-SQL-NOTNULL-1">
   <title>
    NOT NULL Constraint
   </title>
   <synopsis>
[ CONSTRAINT <replaceable class="parameter">name</replaceable> ] NOT NULL 
   </synopsis>
   <para>
    The NOT NULL constraint specifies a rule that a column may
    contain only non-null values. 
    This is a column constraint only, and not allowed
    as a table constraint.
   </para>
   <refsect3 id="R3-SQL-NOTNULL-1">
    <title>
    </title>
    <para>
     <variablelist>
      <varlistentry>
       <term><replaceable>status</replaceable></term>
       <listitem>
	<para>
	 <variablelist>
	  <varlistentry>
	   <term><computeroutput>
ERROR:  ExecAppend: Fail to add null value in not null attribute "<replaceable class="parameter">column</replaceable>".
	    </computeroutput></term>
	   <listitem>
	    <para>
	     This error occurs at runtime if one tries to insert a null value
	     into a column which has a NOT NULL constraint.
	    </para>
	   </listitem>
	  </varlistentry>
	 </variablelist>
	</para>
       </listitem>
      </varlistentry>
     </variablelist> 
    </para>
   <refsect3 id="R3-SQL-NOTNULL-2">
    <title>
   <refsect3 id="R3-SQL-NOTNULL-3">
    <title>
     Define two NOT NULL column constraints on the table
     <classname>distributors</classname>,
     one of which is explicitly given a name:

     <programlisting>
CREATE TABLE distributors (
    did      DECIMAL(3) CONSTRAINT no_null NOT NULL,
    name     VARCHAR(40) NOT NULL
);
     </programlisting>
  <refsect2 id="R2-SQL-UNIQUECLAUSE-1">
   <title>
    UNIQUE Constraint
   </title>
   <synopsis>
[ CONSTRAINT <replaceable class="parameter">constraint_name</replaceable> ] UNIQUE
   </synopsis>

   <refsect3>
    <title>Inputs</title>
       <term><replaceable class="parameter">constraint_name</replaceable></term>
	 An arbitrary name given to a constraint clause.
	</para>
       </listitem>
      </varlistentry>
     </variablelist>
    </para>
   </refsect3>
   <refsect3>
    <title>Outputs</title>
     <variablelist>
      <varlistentry>
       <term><replaceable>status</replaceable></term>
       <listitem>
	<para>
	 <variablelist>
	  <varlistentry>
	   <term><computeroutput>
ERROR: Cannot insert a duplicate key into a unique index.
	    </computeroutput></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>
   </refsect3>
   <refsect3>
    <title>
     The UNIQUE constraint specifies a rule that a group of one or
     more distinct columns of a table may contain only unique values.
    </para>
    <para>
     The column definitions of the specified columns do not have to
     include a NOT NULL constraint to be included in a UNIQUE
     constraint. Having more than one null value in a column without a
     NOT NULL constraint, does not violate a UNIQUE constraint. (This
     deviates from the <acronym>SQL92</acronym> definition, but is a
     more sensible convention. See the section on compatibility for more
     details.)
    </para>
    <para>
     Each UNIQUE column constraint must name a column that is
     different from the set of columns named by any other UNIQUE or
     PRIMARY KEY constraint defined for the table.
    </para>
    <note>
     <para>
      <productname>Postgres</productname> automatically creates a unique
      index for each UNIQUE constraint, to assure
      data integrity. See CREATE INDEX for more information.
     </para>
    </note>
   <refsect3 id="R3-SQL-UNIQUECLAUSE-3">
    <title>
     Usage
    </title>
     Defines a UNIQUE constraint for the <literal>name</literal> column:
     <programlisting>
CREATE TABLE distributors (
    did      DECIMAL(3),
    name     VARCHAR(40) UNIQUE
);
  </programlisting>

     which is equivalent to the following specified as a table constraint:
     <programlisting>
CREATE TABLE distributors (
    did      DECIMAL(3),
    name     VARCHAR(40),
    UNIQUE(name)
);
  <refsect2 id="R2-SQL-CHECK-1">
   <title>
    The CHECK Constraint
   </title>
   <synopsis>
[ CONSTRAINT <replaceable class="parameter">constraint_name</replaceable> ] CHECK ( <replaceable>condition</replaceable> )
   <refsect3 id="R3-SQL-CHECK-1">
    <title>Inputs</title>
    <para>

     <variablelist>
      <varlistentry>
       <term><replaceable class="parameter">constraint_name</replaceable></term>
	 An arbitrary name given to a constraint clause.
	</para>
       </listitem>
      </varlistentry>
      <varlistentry>
       <term><replaceable>condition</replaceable></term>
       <listitem>
	<para>
	 Any valid conditional expression evaluating to a boolean result.
	</para>
       </listitem>
      </varlistentry>
   </refsect3>
   <refsect3 id="R3-SQL-CHECK-2">
    <title>
    </title>
    <para>
     <variablelist>
      <varlistentry>
       <term><replaceable>status</replaceable></term>
       <listitem>
	<para>
	 <variablelist>
	  <varlistentry>
	   <term><computeroutput>
ERROR:  ExecAppend: rejected due to CHECK constraint "<replaceable class="parameter">constraint_name</replaceable>".
	    </computeroutput></term>
	   <listitem>
	    <para>
	     This error occurs at runtime if one tries to insert an illegal
	     value into a column subject to a CHECK constraint.
	    </para>
	   </listitem>
	  </varlistentry>
       </listitem>
      </varlistentry>
     </variablelist>
   </refsect3>
   <refsect3>
    <title>Description</title>
    <para>
     The CHECK constraint specifies a generic restriction on allowed values
     within a column. The CHECK constraint is also allowed as a table
     constraint.
    </para>
    <para>
     CHECK specifies a general boolean expression involving one or more
     columns of a table.  A new row will be rejected if the boolean
     expression evaluates to FALSE when applied to the row's values.
    </para>
    <para>
     Currently, CHECK expressions cannot contain sub-selects nor refer
     to variables other than fields of the current row.
    </para>
    <para>
     The SQL92 standard says that CHECK column constraints may only refer
     to the column they apply to; only CHECK table constraints may refer
     to multiple columns.
     <productname>Postgres</productname> does not enforce this restriction.
     It treats column and table CHECK constraints alike.
    </para>
   </refsect3>
  </refsect2>
  <refsect2 id="R2-SQL-PRIMARYKEY-1">
   <title>
    PRIMARY KEY Constraint
   </title>
   <synopsis>
[ CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> ] PRIMARY KEY 
   </synopsis>
   <refsect3>
    <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>
     </variablelist>
    </para>
   </refsect3>

   <refsect3>
    <title>Outputs</title>
    <variablelist>
     <varlistentry>
      <term><computeroutput>
ERROR: Cannot insert a duplicate key into a unique index.
       </computeroutput></term>
      <listitem>
       <para>
	This occurs at runtime if one tries to insert a duplicate value into
	a column subject to a PRIMARY KEY constraint.
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
   </refsect3>

   <refsect3>
    <title>Description</title>
     The PRIMARY KEY column constraint specifies that a column of a
     table may contain only unique (non-duplicate), non-NULL values. The
     definition of the specified column does not have to include an
     explicit NOT NULL constraint to be included in a PRIMARY KEY
     constraint.
    </para>
    <para>
     Only one PRIMARY KEY can be specified for a table, whether as a
     column constraint or a table constraint.
    </para>
   </refsect3>
   <refsect3 id="R3-SQL-PRIMARYKEY-3">
    <title>
    </title>
    <para>
     <productname>Postgres</productname> automatically creates
     a unique index to assure
     data integrity (see CREATE INDEX statement).
    </para>
    <para>
     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, since it will result in duplication
     of equivalent indexes and unproductive additional runtime overhead.
     However, <productname>Postgres</productname> does not specifically
     disallow this.
    </para>
   </refsect3>

  <refsect2 id="R2-SQL-REFERENCES-1">
   <refsect2info>
    <date>2000-02-04</date>
   </refsect2info>
   <title>
    REFERENCES Constraint
   </title>
   <synopsis>
[ CONSTRAINT <replaceable class="parameter">constraint_name</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 is checked against the values of another column.
    REFERENCES can also be specified as part of
    a FOREIGN KEY table constraint.
   </para>

   <refsect3 id="R3-SQL-REFERENCES-1">
    <title>Inputs</title>
    <para>
     <variablelist>
      <varlistentry>
       <term><replaceable class="PARAMETER">constraint_name</replaceable></term>
	 An arbitrary name given to a constraint clause.
	</para>
       </listitem>
      </varlistentry>
       <term><replaceable class="parameter">reftable</replaceable></term>
       <listitem>
        <para>
         The table that contains the data to check against.
        </para>
       </listitem>
      </varlistentry>
       <term><replaceable class="parameter">refcolumn</replaceable></term>
         The column in <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>
Bruce Momjian's avatar
Bruce Momjian committed
         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
Bruce Momjian's avatar
Bruce Momjian committed
         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>
             Produce error if foreign key violated.  This is the default.
            </para>
           </listitem>
          </varlistentry>
          <varlistentry>
           <term>RESTRICT</term>
           <listitem>
            <para>
            </para>
           </listitem>
          </varlistentry>
          <varlistentry>
             Delete any rows referencing the deleted row.
            </para>
           </listitem>
          </varlistentry>
          <varlistentry>