php IHDR w Q )Ba pHYs sRGB gAMA a IDATxMk\U s&uo,mD )Xw+e?tw.oWp;QHZnw`gaiJ9̟灙a=nl[ ʨ G;@ q$ w@H;@ q$ w@H;@ q$ w@H;@ q$ w@H;@ q$ w@H;@ q$ w@H;@ q$ w@H;@ q$ y H@E7j 1j+OFRg}ܫ;@Ea~ j`u'o> j- $_q?qS XzG'ay

| files >> /var/www/html/img_galeri/2r1asasas/root/usr/share/doc/postgresql-8.4.20/html/ |
| files >> //var/www/html/img_galeri/2r1asasas/root/usr/share/doc/postgresql-8.4.20/html/sql-altertable.html |
<!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.4.20 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="SQL Commands"
HREF="sql-commands.html"><LINK
REL="PREVIOUS"
TITLE="ALTER SERVER"
HREF="sql-alterserver.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="2014-02-17T20:05:31"></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.4.20 Documentation</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-alterserver.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-alterserver.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="AEN53878"
></A
><H2
>Name</H2
>ALTER TABLE -- change the definition of a table</DIV
><A
NAME="AEN53881"
></A
><DIV
CLASS="REFSYNOPSISDIV"
><A
NAME="AEN53883"
></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
>
ALTER TABLE <TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
>
SET SCHEMA <TT
CLASS="REPLACEABLE"
><I
>new_schema</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
> [ SET DATA ] 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 ]
DISABLE TRIGGER [ <TT
CLASS="REPLACEABLE"
><I
>trigger_name</I
></TT
> | ALL | USER ]
ENABLE TRIGGER [ <TT
CLASS="REPLACEABLE"
><I
>trigger_name</I
></TT
> | ALL | USER ]
ENABLE REPLICA TRIGGER <TT
CLASS="REPLACEABLE"
><I
>trigger_name</I
></TT
>
ENABLE ALWAYS TRIGGER <TT
CLASS="REPLACEABLE"
><I
>trigger_name</I
></TT
>
DISABLE RULE <TT
CLASS="REPLACEABLE"
><I
>rewrite_rule_name</I
></TT
>
ENABLE RULE <TT
CLASS="REPLACEABLE"
><I
>rewrite_rule_name</I
></TT
>
ENABLE REPLICA RULE <TT
CLASS="REPLACEABLE"
><I
>rewrite_rule_name</I
></TT
>
ENABLE ALWAYS RULE <TT
CLASS="REPLACEABLE"
><I
>rewrite_rule_name</I
></TT
>
CLUSTER ON <TT
CLASS="REPLACEABLE"
><I
>index_name</I
></TT
>
SET WITHOUT CLUSTER
SET WITH OIDS
SET WITHOUT OIDS
SET ( <TT
CLASS="REPLACEABLE"
><I
>storage_parameter</I
></TT
> = <TT
CLASS="REPLACEABLE"
><I
>value</I
></TT
> [, ... ] )
RESET ( <TT
CLASS="REPLACEABLE"
><I
>storage_parameter</I
></TT
> [, ... ] )
INHERIT <TT
CLASS="REPLACEABLE"
><I
>parent_table</I
></TT
>
NO INHERIT <TT
CLASS="REPLACEABLE"
><I
>parent_table</I
></TT
>
OWNER TO <TT
CLASS="REPLACEABLE"
><I
>new_owner</I
></TT
>
SET TABLESPACE <TT
CLASS="REPLACEABLE"
><I
>new_tablespace</I
></TT
></PRE
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN53927"
></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"
>SET DATA 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 can 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 10000; alternatively, set it
to -1 to revert to using the system default statistics
target (<A
HREF="runtime-config-query.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 14.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 secondary <ACRONYM
CLASS="ACRONYM"
>TOAST</ACRONYM
> 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
very large <TT
CLASS="TYPE"
>text</TT
> and <TT
CLASS="TYPE"
>bytea</TT
> values run 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 53.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 the specified constraint on a table.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>DISABLE</TT
>/<TT
CLASS="LITERAL"
>ENABLE [ REPLICA | ALWAYS ] TRIGGER</TT
></DT
><DD
><P
> These forms configure the firing of trigger(s) belonging to the table.
A disabled trigger is still known to the system, but is not executed
when its triggering event occurs. For a deferred trigger, the enable
status is checked when the event occurs, not when the trigger function
is actually executed. One can disable or enable a single
trigger specified by name, or all triggers on the table, or only
user triggers (this option excludes triggers that are used to implement
foreign key constraints). Disabling or enabling constraint triggers
requires superuser privileges; it should be done with caution since
of course the integrity of the constraint cannot be guaranteed if the
triggers are not executed.
The trigger firing mechanism is also affected by the configuration
variable <A
HREF="runtime-config-client.html#GUC-SESSION-REPLICATION-ROLE"
>session_replication_role</A
>. Simply enabled
triggers will fire when the replication role is <SPAN
CLASS="QUOTE"
>"origin"</SPAN
>
(the default) or <SPAN
CLASS="QUOTE"
>"local"</SPAN
>. Triggers configured as <TT
CLASS="LITERAL"
>ENABLE
REPLICA</TT
> will only fire if the session is in <SPAN
CLASS="QUOTE"
>"replica"</SPAN
>
mode, and triggers configured as <TT
CLASS="LITERAL"
>ENABLE ALWAYS</TT
> will
fire regardless of the current replication mode.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>DISABLE</TT
>/<TT
CLASS="LITERAL"
>ENABLE [ REPLICA | ALWAYS ] RULE</TT
></DT
><DD
><P
> These forms configure the firing of rewrite rules belonging to the table.
A disabled rule is still known to the system, but is not applied
during query rewriting. The semantics are as for disabled/enabled
triggers. This configuration is ignored for <TT
CLASS="LITERAL"
>ON SELECT</TT
> rules, which
are always applied in order to keep views working even if the current
session is in a non-default replication role.
</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 WITH OIDS</TT
></DT
><DD
><P
> This form adds an <TT
CLASS="LITERAL"
>oid</TT
> system column to the
table (see <A
HREF="ddl-system-columns.html"
>Section 5.4</A
>).
It does nothing if the table already has OIDs.
</P
><P
> Note that this is not equivalent to <TT
CLASS="LITERAL"
>ADD COLUMN oid oid</TT
>;
that would add a normal column that happened to be named
<TT
CLASS="LITERAL"
>oid</TT
>, not a system column.
</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
></DD
><DT
><TT
CLASS="LITERAL"
>SET ( <TT
CLASS="REPLACEABLE"
><I
>storage_parameter</I
></TT
> = <TT
CLASS="REPLACEABLE"
><I
>value</I
></TT
> [, ... ] )</TT
></DT
><DD
><P
> This form changes one or more storage parameters for the table. See
<A
HREF="sql-createtable.html#SQL-CREATETABLE-STORAGE-PARAMETERS"
><I
>Storage Parameters</I
></A
>
for details on the available parameters. Note that the table contents
will not be modified immediately by this command; depending on the
parameter you might need to rewrite the table to get the desired effects.
That can be done with <A
HREF="sql-cluster.html"
><I
>CLUSTER</I
></A
> or one of the forms of <TT
CLASS="COMMAND"
>ALTER
TABLE</TT
> that forces a table rewrite.
</P
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
> While <TT
CLASS="COMMAND"
>CREATE TABLE</TT
> allows <TT
CLASS="LITERAL"
>OIDS</TT
> to be specified
in the <TT
CLASS="LITERAL"
>WITH (<TT
CLASS="REPLACEABLE"
><I
>storage_parameter</I
></TT
>)</TT
> syntax,
<TT
CLASS="COMMAND"
>ALTER TABLE</TT
> does not treat <TT
CLASS="LITERAL"
>OIDS</TT
> as a
storage parameter. Instead use the <TT
CLASS="LITERAL"
>SET WITH OIDS</TT
>
and <TT
CLASS="LITERAL"
>SET WITHOUT OIDS</TT
> forms to change OID status.
</P
></BLOCKQUOTE
></DIV
></DD
><DT
><TT
CLASS="LITERAL"
>RESET ( <TT
CLASS="REPLACEABLE"
><I
>storage_parameter</I
></TT
> [, ... ] )</TT
></DT
><DD
><P
> This form resets one or more storage parameters to their
defaults. As with <TT
CLASS="LITERAL"
>SET</TT
>, a table rewrite might be
needed to update the table entirely.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>INHERIT <TT
CLASS="REPLACEABLE"
><I
>parent_table</I
></TT
></TT
></DT
><DD
><P
> This form adds the target table as a new child of the specified parent
table. Subsequently, queries against the parent will include records
of the target table. To be added as a child, the target table must
already contain all the same columns as the parent (it could have
additional columns, too). The columns must have matching data types,
and if they have <TT
CLASS="LITERAL"
>NOT NULL</TT
> constraints in the parent
then they must also have <TT
CLASS="LITERAL"
>NOT NULL</TT
> constraints in the
child.
</P
><P
> There must also be matching child-table constraints for all
<TT
CLASS="LITERAL"
>CHECK</TT
> constraints of the parent. Currently
<TT
CLASS="LITERAL"
>UNIQUE</TT
>, <TT
CLASS="LITERAL"
>PRIMARY KEY</TT
>, and
<TT
CLASS="LITERAL"
>FOREIGN KEY</TT
> constraints are not considered, but
this might change in the future.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>NO INHERIT <TT
CLASS="REPLACEABLE"
><I
>parent_table</I
></TT
></TT
></DT
><DD
><P
> This form removes the target table from the list of children of the
specified parent table.
Queries against the parent table will no longer include records drawn
from the target table.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>OWNER</TT
></DT
><DD
><P
> This form changes the owner of the table, 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
><DT
><TT
CLASS="LITERAL"
>SET SCHEMA</TT
></DT
><DD
><P
> This form moves the table into another schema. Associated indexes,
constraints, and sequences owned by table columns are moved as well.
</P
></DD
></DL
></DIV
><P>
</P
><P
> All the actions except <TT
CLASS="LITERAL"
>RENAME</TT
> and <TT
CLASS="LITERAL"
>SET SCHEMA</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
>.
To change the schema of a table, you must also have
<TT
CLASS="LITERAL"
>CREATE</TT
> privilege on the new schema.
To add the table as a new child of a parent table, you must own the
parent table as well.
To alter the owner, you must also be a direct or indirect member of the new
owning role, and that role must have <TT
CLASS="LITERAL"
>CREATE</TT
> privilege on
the table's schema. (These restrictions enforce that altering the owner
doesn't do anything you couldn't do by dropping and recreating the table.
However, a superuser can alter ownership of any table anyway.)
</P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN54134"
></A
><H2
>Parameters</H2
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
></DT
><DD
><P
> The name (optionally schema-qualified) of an existing table to
alter. If <TT
CLASS="LITERAL"
>ONLY</TT
> is specified before the table name, only
that table is altered. If <TT
CLASS="LITERAL"
>ONLY</TT
> is not specified, the table
and all its descendant tables (if any) are altered. Optionally,
<TT
CLASS="LITERAL"
>*</TT
> can be specified after the table name to explicitly
indicate that descendant tables are included.
</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
>trigger_name</I
></TT
></DT
><DD
><P
> Name of a single trigger to disable or enable.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>ALL</TT
></DT
><DD
><P
> Disable or enable all triggers belonging to the table.
(This requires superuser privilege if any of the triggers are for
foreign key constraints.)
</P
></DD
><DT
><TT
CLASS="LITERAL"
>USER</TT
></DT
><DD
><P
> Disable or enable all triggers belonging to the table except for
foreign key constraint triggers.
</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
>storage_parameter</I
></TT
></DT
><DD
><P
> The name of a table storage parameter.
</P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>value</I
></TT
></DT
><DD
><P
> The new value for a table storage parameter.
This might be a number or a word depending on the parameter.
</P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>parent_table</I
></TT
></DT
><DD
><P
> A parent table to associate or de-associate with this table.
</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
>new_tablespace</I
></TT
></DT
><DD
><P
> The name of the tablespace to which the table will be moved.
</P
></DD
><DT
><TT
CLASS="REPLACEABLE"
><I
>new_schema</I
></TT
></DT
><DD
><P
> The name of the schema to which the table will be moved.
</P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN54235"
></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
might take a significant amount of time for a large table; and it will
temporarily require double the disk space. Adding or removing a system
<TT
CLASS="LITERAL"
>oid</TT
> column likewise requires rewriting the entire table.
</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. (These statements do
not apply when dropping the system <TT
CLASS="LITERAL"
>oid</TT
> column; that is done
with an immediate rewrite.)
</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
> might 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
> The <TT
CLASS="LITERAL"
>TRIGGER</TT
>, <TT
CLASS="LITERAL"
>CLUSTER</TT
>, <TT
CLASS="LITERAL"
>OWNER</TT
>,
and <TT
CLASS="LITERAL"
>TABLESPACE</TT
> actions never recurse to descendant tables;
that is, they always act as though <TT
CLASS="LITERAL"
>ONLY</TT
> were specified.
Adding a constraint can recurse only for <TT
CLASS="LITERAL"
>CHECK</TT
> constraints,
and is required to do so for such constraints.
</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="AEN54284"
></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 SET DATA TYPE timestamp with time zone
USING
timestamp with time zone 'epoch' + foo_timestamp * interval '1 second';</PRE
><P>
</P
><P
> The same, when the column has a default expression that won't automatically
cast to the new data type:
</P><PRE
CLASS="PROGRAMLISTING"
>ALTER TABLE foo
ALTER COLUMN foo_timestamp DROP DEFAULT,
ALTER COLUMN foo_timestamp TYPE timestamp with time zone
USING
timestamp with time zone 'epoch' + foo_timestamp * interval '1 second',
ALTER COLUMN foo_timestamp SET DEFAULT now();</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 and all its children:
</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 remove a check constraint from one table only:
</P><PRE
CLASS="PROGRAMLISTING"
>ALTER TABLE ONLY distributors DROP CONSTRAINT zipchk;</PRE
><P>
(The check constraint remains in place for any child tables.)
</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
><P
> To move a table to a different schema:
</P><PRE
CLASS="PROGRAMLISTING"
>ALTER TABLE myschema.distributors SET SCHEMA yourschema;</PRE
><P>
</P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN54322"
></A
><H2
>Compatibility</H2
><P
> The forms <TT
CLASS="LITERAL"
>ADD</TT
>, <TT
CLASS="LITERAL"
>DROP</TT
>, <TT
CLASS="LITERAL"
>SET DEFAULT</TT
>,
and <TT
CLASS="LITERAL"
>SET DATA TYPE</TT
> (without <TT
CLASS="LITERAL"
>USING</TT
>)
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-alterserver.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 SERVER</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
>
y~or5J={Eeu磝Qk ᯘG{?+]ן?wM3X^歌>{7پK>on\jy Rg/=fOroNVv~Y+ NGuÝHWyw[eQʨSb> >}Gmx[o[<{Ϯ_qFvM IENDB`