Sophie

Sophie

distrib > Mandriva > 2006.0 > x86_64 > by-pkgid > b8f4049de69feba5041d49ed4382e582 > files > 478

postgresql-docs-8.0.11-0.1.20060mdk.x86_64.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>ALTER TABLE</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 8.0.11 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="SQL Commands"
HREF="sql-commands.html"><LINK
REL="PREVIOUS"
TITLE="ALTER SEQUENCE"
HREF="sql-altersequence.html"><LINK
REL="NEXT"
TITLE="ALTER TABLESPACE"
HREF="sql-altertablespace.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2007-02-02T03:57:22"></HEAD
><BODY
CLASS="REFENTRY"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
>PostgreSQL 8.0.11 Documentation</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-altersequence.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-altersequence.html"
>Fast Backward</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="sql-altertablespace.html"
>Fast Forward</A
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="sql-altertablespace.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="SQL-ALTERTABLE"
></A
>ALTER TABLE</H1
><DIV
CLASS="REFNAMEDIV"
><A
NAME="AEN37121"
></A
><H2
>Name</H2
>ALTER TABLE&nbsp;--&nbsp;change the definition of a table</DIV
><A
NAME="AEN37124"
></A
><DIV
CLASS="REFSYNOPSISDIV"
><A
NAME="AEN37126"
></A
><H2
>Synopsis</H2
><PRE
CLASS="SYNOPSIS"
>ALTER TABLE [ ONLY ] <TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
> [ * ]
    <TT
CLASS="REPLACEABLE"
><I
>action</I
></TT
> [, ... ]
ALTER TABLE [ ONLY ] <TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
> [ * ]
    RENAME [ COLUMN ] <TT
CLASS="REPLACEABLE"
><I
>column</I
></TT
> TO <TT
CLASS="REPLACEABLE"
><I
>new_column</I
></TT
>
ALTER TABLE <TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
>
    RENAME TO <TT
CLASS="REPLACEABLE"
><I
>new_name</I
></TT
>

where <TT
CLASS="REPLACEABLE"
><I
>action</I
></TT
> is one of:

    ADD [ COLUMN ] <TT
CLASS="REPLACEABLE"
><I
>column</I
></TT
> <TT
CLASS="REPLACEABLE"
><I
>type</I
></TT
> [ <TT
CLASS="REPLACEABLE"
><I
>column_constraint</I
></TT
> [ ... ] ]
    DROP [ COLUMN ] <TT
CLASS="REPLACEABLE"
><I
>column</I
></TT
> [ RESTRICT | CASCADE ]
    ALTER [ COLUMN ] <TT
CLASS="REPLACEABLE"
><I
>column</I
></TT
> TYPE <TT
CLASS="REPLACEABLE"
><I
>type</I
></TT
> [ USING <TT
CLASS="REPLACEABLE"
><I
>expression</I
></TT
> ]
    ALTER [ COLUMN ] <TT
CLASS="REPLACEABLE"
><I
>column</I
></TT
> SET DEFAULT <TT
CLASS="REPLACEABLE"
><I
>expression</I
></TT
>
    ALTER [ COLUMN ] <TT
CLASS="REPLACEABLE"
><I
>column</I
></TT
> DROP DEFAULT
    ALTER [ COLUMN ] <TT
CLASS="REPLACEABLE"
><I
>column</I
></TT
> { SET | DROP } NOT NULL
    ALTER [ COLUMN ] <TT
CLASS="REPLACEABLE"
><I
>column</I
></TT
> SET STATISTICS <TT
CLASS="REPLACEABLE"
><I
>integer</I
></TT
>
    ALTER [ COLUMN ] <TT
CLASS="REPLACEABLE"
><I
>column</I
></TT
> SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
    ADD <TT
CLASS="REPLACEABLE"
><I
>table_constraint</I
></TT
>
    DROP CONSTRAINT <TT
CLASS="REPLACEABLE"
><I
>constraint_name</I
></TT
> [ RESTRICT | CASCADE ]
    CLUSTER ON <TT
CLASS="REPLACEABLE"
><I
>index_name</I
></TT
>
    SET WITHOUT CLUSTER
    SET WITHOUT OIDS
    OWNER TO <TT
CLASS="REPLACEABLE"
><I
>new_owner</I
></TT
>
    SET TABLESPACE <TT
CLASS="REPLACEABLE"
><I
>tablespace_name</I
></TT
></PRE
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN37155"
></A
><H2
>Description</H2
><P
>   <TT
CLASS="COMMAND"
>ALTER TABLE</TT
> changes the definition of an existing table.
   There are several subforms:

  <P
></P
></P><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="LITERAL"
>ADD COLUMN</TT
></DT
><DD
><P
>      This form adds a new column to the table using the same syntax as
      <A
HREF="sql-createtable.html"
><I
>CREATE TABLE</I
></A
>.
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>DROP COLUMN</TT
></DT
><DD
><P
>      This form drops a column from a table.  Indexes and
      table constraints involving the column will be automatically
      dropped as well.  You will need to say <TT
CLASS="LITERAL"
>CASCADE</TT
> if
      anything outside the table depends on the column, for example,
      foreign key references or views.
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>ALTER COLUMN TYPE</TT
></DT
><DD
><P
>      This form changes the type of a column of a table. Indexes and
      simple table constraints involving the column will be automatically
      converted to use the new column type by reparsing the originally
      supplied expression.  The optional <TT
CLASS="LITERAL"
>USING</TT
>
      clause specifies how to compute the new column value from the old;
      if omitted, the default conversion is the same as an assignment
      cast from old data type to new.  A  <TT
CLASS="LITERAL"
>USING</TT
>
      clause must be provided if there is no implicit or assignment
      cast from old to new type.
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>SET</TT
>/<TT
CLASS="LITERAL"
>DROP DEFAULT</TT
></DT
><DD
><P
>      These forms set or remove the default value for a column.
      The default values only apply to subsequent <TT
CLASS="COMMAND"
>INSERT</TT
>
      commands; they do not cause rows already in the table to change.
      Defaults may also be created for views, in which case they are
      inserted into <TT
CLASS="COMMAND"
>INSERT</TT
> statements on the view before
      the view's <TT
CLASS="LITERAL"
>ON INSERT</TT
> rule is applied.
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>SET</TT
>/<TT
CLASS="LITERAL"
>DROP NOT NULL</TT
></DT
><DD
><P
>      These forms change whether a column is marked to allow null
      values or to reject null values.  You can only use <TT
CLASS="LITERAL"
>SET
      NOT NULL</TT
> when the column contains no null values.
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>SET STATISTICS</TT
></DT
><DD
><P
>      This form
      sets the per-column statistics-gathering target for subsequent
      <A
HREF="sql-analyze.html"
><I
>ANALYZE</I
></A
> operations.
      The target can be set in the range 0 to 1000; alternatively, set it
      to -1 to revert to using the system default statistics
      target (<A
HREF="runtime-config.html#GUC-DEFAULT-STATISTICS-TARGET"
>default_statistics_target</A
>).
      For more information on the use of statistics by the
      <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> query planner, refer to
      <A
HREF="planner-stats.html"
>Section 13.2</A
>.
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>SET STORAGE</TT
></DT
><DD
><P
>      This form sets the storage mode for a column. This controls whether this
      column is held inline or in a supplementary table, and whether the data
      should be compressed or not. <TT
CLASS="LITERAL"
>PLAIN</TT
> must be used
      for fixed-length values such as <TT
CLASS="TYPE"
>integer</TT
> and is
      inline, uncompressed. <TT
CLASS="LITERAL"
>MAIN</TT
> is for inline,
      compressible data. <TT
CLASS="LITERAL"
>EXTERNAL</TT
> is for external,
      uncompressed data, and <TT
CLASS="LITERAL"
>EXTENDED</TT
> is for external,
      compressed data.  <TT
CLASS="LITERAL"
>EXTENDED</TT
> is the default for most
      data types that support non-<TT
CLASS="LITERAL"
>PLAIN</TT
> storage.
      Use of <TT
CLASS="LITERAL"
>EXTERNAL</TT
> will
      make substring operations on <TT
CLASS="TYPE"
>text</TT
> and <TT
CLASS="TYPE"
>bytea</TT
>
      columns faster, at the penalty of increased storage space.  Note that
      <TT
CLASS="LITERAL"
>SET STORAGE</TT
> doesn't itself change anything in the table,
      it just sets the strategy to be pursued during future table updates.
      See <A
HREF="storage-toast.html"
>Section 49.2</A
> for more information.
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>ADD <TT
CLASS="REPLACEABLE"
><I
>table_constraint</I
></TT
></TT
></DT
><DD
><P
>      This form adds a new constraint to a table using the same syntax as
      <A
HREF="sql-createtable.html"
><I
>CREATE TABLE</I
></A
>. 
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>DROP CONSTRAINT</TT
></DT
><DD
><P
>      This form drops constraints on a table.
      Currently, constraints on tables are not required to have unique
      names, so there may be more than one constraint matching the specified
      name.  All matching constraints will be dropped.
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>CLUSTER</TT
></DT
><DD
><P
>      This form selects the default index for future 
      <A
HREF="sql-cluster.html"
><I
>CLUSTER</I
></A
>
      operations.  It does not actually re-cluster the table.
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>SET WITHOUT CLUSTER</TT
></DT
><DD
><P
>      This form removes the most recently used
      <A
HREF="sql-cluster.html"
><I
>CLUSTER</I
></A
>
      index specification from the table.  This affects
      future cluster operations that don't specify an index.
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>SET WITHOUT OIDS</TT
></DT
><DD
><P
>      This form removes the <TT
CLASS="LITERAL"
>oid</TT
> system column from the
      table.  This is exactly equivalent to
      <TT
CLASS="LITERAL"
>DROP COLUMN oid RESTRICT</TT
>,
      except that it will not complain if there is already no
      <TT
CLASS="LITERAL"
>oid</TT
> column.
     </P
><P
>      Note that there is no variant of <TT
CLASS="COMMAND"
>ALTER TABLE</TT
>
      that allows OIDs to be restored to a table once they have been
      removed.
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>OWNER</TT
></DT
><DD
><P
>      This form changes the owner of the table, index, sequence, or view to the
      specified user.
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>SET TABLESPACE</TT
></DT
><DD
><P
>      This form changes the table's tablespace to the specified tablespace and
      moves the data file(s) associated with the table to the new tablespace.
      Indexes on the table, if any, are not moved; but they can be moved
      separately with additional <TT
CLASS="LITERAL"
>SET TABLESPACE</TT
> commands.
      See also 
      <A
HREF="sql-createtablespace.html"
><I
>CREATE TABLESPACE</I
></A
>.
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>RENAME</TT
></DT
><DD
><P
>      The <TT
CLASS="LITERAL"
>RENAME</TT
> forms change the name of a table
      (or an index, sequence, or view) or the name of an individual column in
      a table. There is no effect on the stored data.
     </P
></DD
></DL
></DIV
><P>
  </P
><P
>   All the actions except <TT
CLASS="LITERAL"
>RENAME</TT
> can be combined into
   a list of multiple alterations to apply in parallel.  For example, it
   is possible to add several columns and/or alter the type of several
   columns in a single command.  This is particularly useful with large
   tables, since only one pass over the table need be made.
  </P
><P
>   You must own the table to use <TT
CLASS="COMMAND"
>ALTER TABLE</TT
>; except for
   <TT
CLASS="COMMAND"
>ALTER TABLE OWNER</TT
>, which may only be executed by a superuser.
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN37281"
></A
><H2
>Parameters</H2
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
></DT
><DD
><P
>        The name (possibly schema-qualified) of an existing table to
        alter. If <TT
CLASS="LITERAL"
>ONLY</TT
> is specified, only that table is
        altered. If <TT
CLASS="LITERAL"
>ONLY</TT
> is not specified, the table and all
        its descendant tables (if any) are updated. <TT
CLASS="LITERAL"
>*</TT
> can be
        appended to the table name to indicate that descendant tables are
        to be altered, but in the current version, this is the default
        behavior.  (In releases before 7.1, <TT
CLASS="LITERAL"
>ONLY</TT
> was the
        default behavior.  The default can be altered by changing the
        configuration parameter <A
HREF="runtime-config.html#GUC-SQL-INHERITANCE"
>sql_inheritance</A
>.)
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>column</I
></TT
></DT
><DD
><P
>        Name of a new or existing column.
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>new_column</I
></TT
></DT
><DD
><P
>        New name for an existing column.
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>new_name</I
></TT
></DT
><DD
><P
>        New name for the table.
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>type</I
></TT
></DT
><DD
><P
>        Data type of the new column, or new data type for an existing
        column.
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>table_constraint</I
></TT
></DT
><DD
><P
>        New table constraint for the table.
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>constraint_name</I
></TT
></DT
><DD
><P
>        Name of an existing constraint to drop.
       </P
></DD
><DT
><TT
CLASS="LITERAL"
>CASCADE</TT
></DT
><DD
><P
>        Automatically drop objects that depend on the dropped column
        or constraint (for example, views referencing the column).
       </P
></DD
><DT
><TT
CLASS="LITERAL"
>RESTRICT</TT
></DT
><DD
><P
>        Refuse to drop the column or constraint if there are any dependent
        objects. This is the default behavior.
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>index_name</I
></TT
></DT
><DD
><P
>        The index name on which the table should be marked for clustering.
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>new_owner</I
></TT
></DT
><DD
><P
>        The user name of the new owner of the table.
       </P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>tablespace_name</I
></TT
></DT
><DD
><P
>        The tablespace name to which the table will be moved.
       </P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN37349"
></A
><H2
>Notes</H2
><P
>    The key word <TT
CLASS="LITERAL"
>COLUMN</TT
> is noise and can be omitted.
   </P
><P
>    When a column is added with <TT
CLASS="LITERAL"
>ADD COLUMN</TT
>, all existing
    rows in the table are initialized with the column's default value
    (NULL if no <TT
CLASS="LITERAL"
>DEFAULT</TT
> clause is specified).
   </P
><P
>    Adding a column with a non-null default or changing the type of an
    existing column will require the entire table to be rewritten.  This
    may take a significant amount of time for a large table; and it will
    temporarily require double the disk space.
   </P
><P
>    Adding a <TT
CLASS="LITERAL"
>CHECK</TT
> or <TT
CLASS="LITERAL"
>NOT NULL</TT
> constraint requires
    scanning the table to verify that existing rows meet the constraint.
   </P
><P
>    The main reason for providing the option to specify multiple changes
    in a single <TT
CLASS="COMMAND"
>ALTER TABLE</TT
> is that multiple table scans or
    rewrites can thereby be combined into a single pass over the table.
   </P
><P
>    The <TT
CLASS="LITERAL"
>DROP COLUMN</TT
> form does not physically remove
    the column, but simply makes it invisible to SQL operations.  Subsequent
    insert and update operations in the table will store a null value for the
    column. Thus, dropping a column is quick but it will not immediately
    reduce the on-disk size of your table, as the space occupied 
    by the dropped column is not reclaimed.  The space will be
    reclaimed over time as existing rows are updated.
   </P
><P
>    The fact that <TT
CLASS="LITERAL"
>ALTER TYPE</TT
> requires rewriting the whole table
    is sometimes an advantage, because the rewriting process eliminates
    any dead space in the table.  For example, to reclaim the space occupied
    by a dropped column immediately, the fastest way is
</P><PRE
CLASS="PROGRAMLISTING"
>ALTER TABLE table ALTER COLUMN anycol TYPE anytype;</PRE
><P>
    where <TT
CLASS="LITERAL"
>anycol</TT
> is any remaining table column and
    <TT
CLASS="LITERAL"
>anytype</TT
> is the same type that column already has.
    This results in no semantically-visible change in the table,
    but the command forces rewriting, which gets rid of no-longer-useful
    data.
   </P
><P
>    The <TT
CLASS="LITERAL"
>USING</TT
> option of <TT
CLASS="LITERAL"
>ALTER TYPE</TT
> can actually
    specify any expression involving the old values of the row; that is, it
    can refer to other columns as well as the one being converted.  This allows
    very general conversions to be done with the <TT
CLASS="LITERAL"
>ALTER TYPE</TT
>
    syntax.  Because of this flexibility, the <TT
CLASS="LITERAL"
>USING</TT
>
    expression is not applied to the column's default value (if any); the
    result might not be a constant expression as required for a default.
    This means that when there is no implicit or assignment cast from old to
    new type, <TT
CLASS="LITERAL"
>ALTER TYPE</TT
> may fail to convert the default even
    though a <TT
CLASS="LITERAL"
>USING</TT
> clause is supplied.  In such cases,
    drop the default with <TT
CLASS="LITERAL"
>DROP DEFAULT</TT
>, perform the <TT
CLASS="LITERAL"
>ALTER
    TYPE</TT
>, and then use <TT
CLASS="LITERAL"
>SET DEFAULT</TT
> to add a suitable new
    default.  Similar considerations apply to indexes and constraints involving
    the column.
   </P
><P
>    If a table has any descendant tables, it is not permitted to add,
    rename, or change the type of a column in the parent table without doing
    the same to the descendants.  That is, <TT
CLASS="COMMAND"
>ALTER TABLE ONLY</TT
>
    will be rejected.  This ensures that the descendants always have
    columns matching the parent.
   </P
><P
>    A recursive <TT
CLASS="LITERAL"
>DROP COLUMN</TT
> operation will remove a
    descendant table's column only if the descendant does not inherit
    that column from any other parents and never had an independent
    definition of the column.  A nonrecursive <TT
CLASS="LITERAL"
>DROP
    COLUMN</TT
> (i.e., <TT
CLASS="COMMAND"
>ALTER TABLE ONLY ... DROP
    COLUMN</TT
>) never removes any descendant columns, but
    instead marks them as independently defined rather than inherited.
   </P
><P
>    Changing any part of a system catalog table is not permitted.
   </P
><P
>    Refer to <A
HREF="sql-createtable.html"
><I
>CREATE TABLE</I
></A
> for a further description of valid
    parameters. <A
HREF="ddl.html"
>Chapter 5</A
> has further information on
    inheritance.
   </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN37389"
></A
><H2
>Examples</H2
><P
>   To add a column of type <TT
CLASS="TYPE"
>varchar</TT
> to a table:
</P><PRE
CLASS="PROGRAMLISTING"
>ALTER TABLE distributors ADD COLUMN address varchar(30);</PRE
><P>
  </P
><P
>   To drop a column from a table:
</P><PRE
CLASS="PROGRAMLISTING"
>ALTER TABLE distributors DROP COLUMN address RESTRICT;</PRE
><P>
  </P
><P
>   To change the types of two existing columns in one operation:
</P><PRE
CLASS="PROGRAMLISTING"
>ALTER TABLE distributors
    ALTER COLUMN address TYPE varchar(80),
    ALTER COLUMN name TYPE varchar(100);</PRE
><P>
  </P
><P
>   To change an integer column containing UNIX timestamps to <TT
CLASS="TYPE"
>timestamp
   with time zone</TT
> via a <TT
CLASS="LITERAL"
>USING</TT
> clause:
</P><PRE
CLASS="PROGRAMLISTING"
>ALTER TABLE foo
    ALTER COLUMN foo_timestamp TYPE timestamp with time zone
    USING
        timestamp with time zone 'epoch' + foo_timestamp * interval '1 second';</PRE
><P>
  </P
><P
>   To rename an existing column:
</P><PRE
CLASS="PROGRAMLISTING"
>ALTER TABLE distributors RENAME COLUMN address TO city;</PRE
><P>
  </P
><P
>   To rename an existing table:
</P><PRE
CLASS="PROGRAMLISTING"
>ALTER TABLE distributors RENAME TO suppliers;</PRE
><P>
  </P
><P
>   To add a not-null constraint to a column:
</P><PRE
CLASS="PROGRAMLISTING"
>ALTER TABLE distributors ALTER COLUMN street SET NOT NULL;</PRE
><P>
   To remove a not-null constraint from a column:
</P><PRE
CLASS="PROGRAMLISTING"
>ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;</PRE
><P>
  </P
><P
> 
   To add a check constraint to a table:
</P><PRE
CLASS="PROGRAMLISTING"
>ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);</PRE
><P>
  </P
><P
> 
   To remove a check constraint from a table and all its children:
</P><PRE
CLASS="PROGRAMLISTING"
>ALTER TABLE distributors DROP CONSTRAINT zipchk;</PRE
><P>
  </P
><P
> 
   To add a foreign key constraint to a table:
</P><PRE
CLASS="PROGRAMLISTING"
>ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) MATCH FULL;</PRE
><P>
  </P
><P
> 
   To add a (multicolumn) unique constraint to a table:
</P><PRE
CLASS="PROGRAMLISTING"
>ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);</PRE
><P>
  </P
><P
> 
   To add an automatically named primary key constraint to a table, noting
   that a table can only ever have one primary key:
</P><PRE
CLASS="PROGRAMLISTING"
>ALTER TABLE distributors ADD PRIMARY KEY (dist_id);</PRE
><P>
  </P
><P
> 
        To move a table to a different tablespace:
</P><PRE
CLASS="PROGRAMLISTING"
>ALTER TABLE distributors SET TABLESPACE fasttablespace;</PRE
><P>
  </P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN37421"
></A
><H2
>Compatibility</H2
><P
>   The <TT
CLASS="LITERAL"
>ADD</TT
>, <TT
CLASS="LITERAL"
>DROP</TT
>, and <TT
CLASS="LITERAL"
>SET DEFAULT</TT
>
   forms conform with the SQL standard.  The other forms are
   <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> extensions of the SQL standard.
   Also, the ability to specify more than one manipulation in a single
   <TT
CLASS="COMMAND"
>ALTER TABLE</TT
> command is an extension.
  </P
><P
>   <TT
CLASS="COMMAND"
>ALTER TABLE DROP COLUMN</TT
> can be used to drop the only
   column of a table, leaving a zero-column table.  This is an
   extension of SQL, which disallows zero-column tables.
  </P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-altersequence.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="sql-altertablespace.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>ALTER SEQUENCE</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="sql-commands.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>ALTER TABLESPACE</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>