<REFENTRY ID="SQL-ALTERTABLE">
<REFMETA>
<REFENTRYTITLE>
ALTER TABLE
</REFENTRYTITLE>
<REFMISCINFO>SQL - Language Statements</REFMISCINFO>
</REFMETA>
<REFNAMEDIV>
<REFNAME>
ALTER TABLE
</REFNAME>
<REFPURPOSE>
Modifies table properties
</REFPURPOSE>
</refnamediv>
<REFSYNOPSISDIV>
<REFSYNOPSISDIVINFO>
<DATE>1998-04-15</DATE>
</REFSYNOPSISDIVINFO>
<SYNOPSIS>
ALTER TABLE <REPLACEABLE CLASS="PARAMETER">table</REPLACEABLE>
    [ * ] ADD [ COLUMN ] <REPLACEABLE CLASS="PARAMETER">column</REPLACEABLE> <REPLACEABLE CLASS="PARAMETER">type</REPLACEABLE>
ALTER TABLE <REPLACEABLE CLASS="PARAMETER">table</REPLACEABLE>
    [ * ] RENAME [ COLUMN ] <REPLACEABLE CLASS="PARAMETER">column</REPLACEABLE> TO <REPLACEABLE CLASS="PARAMETER">newcolumn</REPLACEABLE>
ALTER TABLE <REPLACEABLE CLASS="PARAMETER">table</REPLACEABLE>
    RENAME TO <REPLACEABLE CLASS="PARAMETER">newtable</REPLACEABLE>
</SYNOPSIS>

<REFSECT2 ID="R2-SQL-ALTERTABLE-1">
<REFSECT2INFO>
<DATE>1998-04-15</DATE>
</REFSECT2INFO>
<TITLE>
Inputs
</TITLE>
<PARA>
<VARIABLELIST>
<VARLISTENTRY>
<TERM>
<REPLACEABLE CLASS="PARAMETER"> table </REPLACEABLE>
</TERM>
<LISTITEM>
<PARA>
          The name of an existing table to alter.
</PARA>
</LISTITEM>
</VARLISTENTRY>

<VARLISTENTRY>
<TERM>
<REPLACEABLE CLASS="PARAMETER"> column </REPLACEABLE>
</TERM>
<LISTITEM>
<PARA>
          Name of a new or existing column.
</PARA>
</LISTITEM>
</VARLISTENTRY>

<VARLISTENTRY>
<TERM>
<REPLACEABLE CLASS="PARAMETER"> type </REPLACEABLE>
</TERM>
<LISTITEM>
<PARA>
          Type of the new column.
</PARA>
</LISTITEM>
</VARLISTENTRY>

<VARLISTENTRY>
<TERM>
<REPLACEABLE CLASS="PARAMETER"> newcolumn </REPLACEABLE>
</TERM>
<LISTITEM>
<PARA>
          New name for an existing column.
</PARA>
</LISTITEM>
</VARLISTENTRY>

<VARLISTENTRY>
<TERM>
<REPLACEABLE CLASS="PARAMETER"> newtable </REPLACEABLE>
</TERM>
<LISTITEM>
<PARA>
          New name for an existing column.
</PARA>
</LISTITEM>
</VARLISTENTRY>
</VARIABLELIST>
</para>
</REFSECT2>

<REFSECT2 ID="R2-SQL-ALTERTABLE-2">
<REFSECT2INFO>
<DATE>1998-04-15</DATE>
</REFSECT2INFO>
<TITLE>
Outputs
</TITLE>
<PARA>

<VARIABLELIST>
<VARLISTENTRY>
<TERM>
<returnvalue>ALTER</returnvalue>
</TERM>
<LISTITEM>
<PARA>
          Message returned from column or table renaming.
</PARA>
</LISTITEM>
</VARLISTENTRY>

<VARLISTENTRY>
<TERM>
<returnvalue>NEW</returnvalue>
</TERM>
<LISTITEM>
<PARA>
          Message returned from column addition.
</PARA>
</LISTITEM>
</VARLISTENTRY>

<VARLISTENTRY>
<TERM>
<ReturnValue>ERROR</ReturnValue>
</TERM>
<LISTITEM>
<PARA>
          Message returned if table or column is not available.
</para>
</listitem>
</VARLISTENTRY>
</VARIABLELIST>
</para>
</REFSECT2>
</REFSYNOPSISDIV>

<REFSECT1 ID="R1-SQL-ALTERTABLE-1">
<REFSECT1INFO>
<DATE>1998-04-15</DATE>
</REFSECT1INFO>
<TITLE>
Description
</TITLE>
<PARA>
   <command>ALTER TABLE</command> changes the definition of an existing table.
   The new columns and their types are specified in the same style
   and with the the same restrictions as in <command>CREATE TABLE</command>.
   The RENAME clause causes the name of a table or column
   to change without changing any of the data contained in
   the affected table. Thus, the table or column will
   remain of the same type and size after this command is
   executed.
</para>
<PARA>
   You must own the table in order to change its schema.
</PARA>

<REFSECT2 ID="R2-SQL-ALTERTABLE-3">
<REFSECT2INFO>
<DATE>1998-04-15</DATE>
</REFSECT2INFO>
<TITLE>
Notes
</TITLE>
<PARA>
The keyword COLUMN is noise and can be omitted.
</para>
<PARA>
<Quote>[*]</Quote> following a name of a table indicates that statement
     should be run over that table and all tables below it in the
     inheritance hierarchy.
    The <citetitle>PostgreSQL User's Guide</citetitle> has further
     information on inheritance.
</para>

<PARA>
   Refer to CREATE TABLE for a further description
     of valid arguments.
</para>
</REFSECT2>
</REFSECT1>

<REFSECT1 ID="R1-SQL-ALTERTABLE-2">
<TITLE>
Usage
</TITLE>
<PARA>
   To add a column of type VARCHAR to a table:
<ProgramListing>
    ALTER TABLE distributors ADD COLUMN address VARCHAR(30);
</ProgramListing>
</para>

<PARA>
   To rename an existing column:
<ProgramListing>
    ALTER TABLE distributors RENAME COLUMN address TO city;
</ProgramListing>
</para>

<PARA>
   To rename an existing table:
<ProgramListing>
    ALTER TABLE distributors RENAME TO suppliers;
</ProgramListing>
</para>

</REFSECT1>

<REFSECT1 ID="R1-SQL-ALTERTABLE-3">
<TITLE>
Compatibility
</TITLE>

<REFSECT2 ID="R2-SQL-ALTERTABLE-4">
<REFSECT2INFO>
<DATE>1998-04-15</DATE>
</REFSECT2INFO>
<TITLE>
SQL92
</TITLE>
<PARA>
<command>ALTER TABLE/RENAME</command>
 is a <productname>Postgres</productname> language extension.
</para>

<PARA>
   SQL92 specifies some additional capabilities for <command>ALTER TABLE</command>
   statement which are not yet directly supported by
 <ProductName>Postgres</ProductName>:
</para>

<VARIABLELIST>
<VARLISTENTRY>
<TERM>
<Synopsis>
ALTER TABLE <REPLACEABLE CLASS="PARAMETER">table</REPLACEABLE> ALTER [ COLUMN ] <REPLACEABLE CLASS="PARAMETER">column</REPLACEABLE>
    SET DEFAULT <REPLACEABLE CLASS="PARAMETER">default</REPLACEABLE>

ALTER TABLE <REPLACEABLE CLASS="PARAMETER">table</REPLACEABLE> ALTER [ COLUMN ] <REPLACEABLE CLASS="PARAMETER">column</REPLACEABLE>
    ADD [ CONSTRAINT <REPLACEABLE CLASS="PARAMETER">constraint</REPLACEABLE> ] <REPLACEABLE CLASS="PARAMETER">table-constraint</REPLACEABLE>
</Synopsis>
</TERM>
<LISTITEM>
<PARA>
          Puts the default value or constraint specified into the
          definition of column in the table.
 See <command>CREATE TABLE</command> for the
          syntax of the default and table-constraint clauses.
          If a default clause already exists, it will be replaced by
          the new definition. If any constraints on this column already
          exist, they will be retained using a boolean AND with the new
          constraint.
</para>

<PARA>
Currently, to set new default constraints on an existing column
          the table must be recreated and reloaded:

<ProgramListing>
CREATE TABLE temp AS SELECT * FROM distributors;
DROP TABLE distributors;
CREATE TABLE distributors (
    did      DECIMAL(3) DEFAULT 1,
    name     VARCHAR(40) NOT NULL,
    city     VARCHAR(30)
);
INSERT INTO distributors SELECT * FROM temp;
DROP TABLE temp;
</ProgramListing>
</PARA>
</LISTITEM>
</VARLISTENTRY>

<VARLISTENTRY>
<TERM>
<Synopsis>
ALTER TABLE <REPLACEABLE CLASS="PARAMETER">table</REPLACEABLE>
    DROP DEFAULT <REPLACEABLE CLASS="PARAMETER">default</REPLACEABLE>
ALTER TABLE <REPLACEABLE CLASS="PARAMETER">table</REPLACEABLE>
    DROP CONSTRAINT <REPLACEABLE CLASS="PARAMETER">constraint</REPLACEABLE> { RESTRICT | CASCADE }
</Synopsis>
</TERM>
<LISTITEM>
<PARA>
          Removes the default value specified by default or the rule
          specified by constraint from the definition of a table. 
          If RESTRICT is specified only a constraint with no dependent
          constraints can be destroyed.
          If CASCADE is specified, Any constraints that are dependent on
          this constraint are also dropped.
</para>

<PARA>
Currently, to remove a default value or constraints on an
          existing column the table must be recreated and reloaded:

<ProgramListing>
CREATE TABLE temp AS SELECT * FROM distributors;
DROP TABLE distributors;
CREATE TABLE distributors AS SELECT * FROM temp;
DROP TABLE temp;
</ProgramListing>
</para>
</listitem>
</varlistentry> 
  
<VARLISTENTRY>
<TERM>
<Synopsis>
ALTER TABLE <REPLACEABLE CLASS="PARAMETER">table</REPLACEABLE>
    DROP [ COLUMN ] <REPLACEABLE CLASS="PARAMETER">column</REPLACEABLE> { RESTRICT | CASCADE }
</Synopsis>
</TERM>
<LISTITEM>
<PARA>
          Removes a column from a table.
          If RESTRICT is specified only a column with no dependent
          objects can be destroyed.
          If CASCADE is specified, all objects that are dependent on
          this column are also dropped.
</para>

<PARA>
Currently, to remove an existing column the table must be
          recreated and reloaded:

<ProgramListing>
CREATE TABLE temp AS SELECT did, city FROM distributors;    
DROP TABLE distributors;
CREATE TABLE distributors (
    did      DECIMAL(3)  DEFAULT 1,
    name     VARCHAR(40) NOT NULL,
);
INSERT INTO distributors SELECT * FROM temp;
DROP TABLE temp;
</ProgramListing>
</PARA>
</listitem>
</varlistentry>
</VARIABLELIST>
</refsect2>
</refsect1>
</REFENTRY>
