ok

Mini Shell

Direktori : /proc/thread-self/root/proc/self/root/opt/alt/postgresql11/usr/share/man/man7/
Upload File :
Current File : //proc/thread-self/root/proc/self/root/opt/alt/postgresql11/usr/share/man/man7/ALTER_TABLE.7

'\" t
.\"     Title: ALTER TABLE
.\"    Author: The PostgreSQL Global Development Group
.\" Generator: DocBook XSL Stylesheets v1.78.1 <http://docbook.sf.net/>
.\"      Date: 2017-11-06
.\"    Manual: PostgreSQL 9.2.24 Documentation
.\"    Source: PostgreSQL 9.2.24
.\"  Language: English
.\"
.TH "ALTER TABLE" "7" "2017-11-06" "PostgreSQL 9.2.24" "PostgreSQL 9.2.24 Documentation"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
ALTER_TABLE \- change the definition of a table
.\" ALTER TABLE
.SH "SYNOPSIS"
.sp
.nf
ALTER TABLE [ IF EXISTS ] [ ONLY ] \fIname\fR [ * ]
    \fIaction\fR [, \&.\&.\&. ]
ALTER TABLE [ IF EXISTS ] [ ONLY ] \fIname\fR [ * ]
    RENAME [ COLUMN ] \fIcolumn_name\fR TO \fInew_column_name\fR
ALTER TABLE [ IF EXISTS ] [ ONLY ] \fIname\fR [ * ]
    RENAME CONSTRAINT \fIconstraint_name\fR TO \fInew_constraint_name\fR
ALTER TABLE [ IF EXISTS ] \fIname\fR
    RENAME TO \fInew_name\fR
ALTER TABLE [ IF EXISTS ] \fIname\fR
    SET SCHEMA \fInew_schema\fR

where \fIaction\fR is one of:

    ADD [ COLUMN ] \fIcolumn_name\fR \fIdata_type\fR [ COLLATE \fIcollation\fR ] [ \fIcolumn_constraint\fR [ \&.\&.\&. ] ]
    DROP [ COLUMN ] [ IF EXISTS ] \fIcolumn_name\fR [ RESTRICT | CASCADE ]
    ALTER [ COLUMN ] \fIcolumn_name\fR [ SET DATA ] TYPE \fIdata_type\fR [ COLLATE \fIcollation\fR ] [ USING \fIexpression\fR ]
    ALTER [ COLUMN ] \fIcolumn_name\fR SET DEFAULT \fIexpression\fR
    ALTER [ COLUMN ] \fIcolumn_name\fR DROP DEFAULT
    ALTER [ COLUMN ] \fIcolumn_name\fR { SET | DROP } NOT NULL
    ALTER [ COLUMN ] \fIcolumn_name\fR SET STATISTICS \fIinteger\fR
    ALTER [ COLUMN ] \fIcolumn_name\fR SET ( \fIattribute_option\fR = \fIvalue\fR [, \&.\&.\&. ] )
    ALTER [ COLUMN ] \fIcolumn_name\fR RESET ( \fIattribute_option\fR [, \&.\&.\&. ] )
    ALTER [ COLUMN ] \fIcolumn_name\fR SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
    ADD \fItable_constraint\fR [ NOT VALID ]
    ADD \fItable_constraint_using_index\fR
    VALIDATE CONSTRAINT \fIconstraint_name\fR
    DROP CONSTRAINT [ IF EXISTS ]  \fIconstraint_name\fR [ RESTRICT | CASCADE ]
    DISABLE TRIGGER [ \fItrigger_name\fR | ALL | USER ]
    ENABLE TRIGGER [ \fItrigger_name\fR | ALL | USER ]
    ENABLE REPLICA TRIGGER \fItrigger_name\fR
    ENABLE ALWAYS TRIGGER \fItrigger_name\fR
    DISABLE RULE \fIrewrite_rule_name\fR
    ENABLE RULE \fIrewrite_rule_name\fR
    ENABLE REPLICA RULE \fIrewrite_rule_name\fR
    ENABLE ALWAYS RULE \fIrewrite_rule_name\fR
    CLUSTER ON \fIindex_name\fR
    SET WITHOUT CLUSTER
    SET WITH OIDS
    SET WITHOUT OIDS
    SET ( \fIstorage_parameter\fR = \fIvalue\fR [, \&.\&.\&. ] )
    RESET ( \fIstorage_parameter\fR [, \&.\&.\&. ] )
    INHERIT \fIparent_table\fR
    NO INHERIT \fIparent_table\fR
    OF \fItype_name\fR
    NOT OF
    OWNER TO \fInew_owner\fR
    SET TABLESPACE \fInew_tablespace\fR

and \fItable_constraint_using_index\fR is:

    [ CONSTRAINT \fIconstraint_name\fR ]
    { UNIQUE | PRIMARY KEY } USING INDEX \fIindex_name\fR
    [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
.fi
.SH "DESCRIPTION"
.PP
\fBALTER TABLE\fR
changes the definition of an existing table\&. There are several subforms:
.PP
ADD COLUMN
.RS 4
This form adds a new column to the table, using the same syntax as
CREATE TABLE (\fBCREATE_TABLE\fR(7))\&.
.RE
.PP
DROP COLUMN [ IF EXISTS ]
.RS 4
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
CASCADE
if anything outside the table depends on the column, for example, foreign key references or views\&. If
IF EXISTS
is specified and the column does not exist, no error is thrown\&. In this case a notice is issued instead\&.
.RE
.PP
IF EXISTS
.RS 4
Do not throw an error if the table does not exist\&. A notice is issued in this case\&.
.RE
.PP
SET DATA TYPE
.RS 4
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
COLLATE
clause specifies a collation for the new column; if omitted, the collation is the default for the new column type\&. The optional
USING
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
USING
clause must be provided if there is no implicit or assignment cast from old to new type\&.
.RE
.PP
SET/DROP DEFAULT
.RS 4
These forms set or remove the default value for a column\&. The default values only apply to subsequent
\fBINSERT\fR
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
\fBINSERT\fR
statements on the view before the view\*(Aqs
ON INSERT
rule is applied\&.
.RE
.PP
SET/DROP NOT NULL
.RS 4
These forms change whether a column is marked to allow null values or to reject null values\&. You can only use
SET NOT NULL
when the column contains no null values\&.
.RE
.PP
SET STATISTICS
.RS 4
This form sets the per\-column statistics\-gathering target for subsequent
\fBANALYZE\fR(7)
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 (default_statistics_target)\&. For more information on the use of statistics by the
PostgreSQL
query planner, refer to
Section 14.2, \(lqStatistics Used by the Planner\(rq, in the documentation\&.
.RE
.PP
SET ( \fIattribute_option\fR = \fIvalue\fR [, \&.\&.\&. ] ), RESET ( \fIattribute_option\fR [, \&.\&.\&. ] )
.RS 4
This form sets or resets per\-attribute options\&. Currently, the only defined per\-attribute options are
n_distinct
and
n_distinct_inherited, which override the number\-of\-distinct\-values estimates made by subsequent
\fBANALYZE\fR(7)
operations\&.
n_distinct
affects the statistics for the table itself, while
n_distinct_inherited
affects the statistics gathered for the table plus its inheritance children\&. When set to a positive value,
\fBANALYZE\fR
will assume that the column contains exactly the specified number of distinct nonnull values\&. When set to a negative value, which must be greater than or equal to \-1,
\fBANALYZE\fR
will assume that the number of distinct nonnull values in the column is linear in the size of the table; the exact count is to be computed by multiplying the estimated table size by the absolute value of the given number\&. For example, a value of \-1 implies that all values in the column are distinct, while a value of \-0\&.5 implies that each value appears twice on the average\&. This can be useful when the size of the table changes over time, since the multiplication by the number of rows in the table is not performed until query planning time\&. Specify a value of 0 to revert to estimating the number of distinct values normally\&. For more information on the use of statistics by the
PostgreSQL
query planner, refer to
Section 14.2, \(lqStatistics Used by the Planner\(rq, in the documentation\&.
.RE
.PP
SET STORAGE
.RS 4
.\" TOAST: per-column storage settings
This form sets the storage mode for a column\&. This controls whether this column is held inline or in a secondary
TOAST
table, and whether the data should be compressed or not\&.
PLAIN
must be used for fixed\-length values such as
integer
and is inline, uncompressed\&.
MAIN
is for inline, compressible data\&.
EXTERNAL
is for external, uncompressed data, and
EXTENDED
is for external, compressed data\&.
EXTENDED
is the default for most data types that support non\-PLAIN
storage\&. Use of
EXTERNAL
will make substring operations on very large
text
and
bytea
values run faster, at the penalty of increased storage space\&. Note that
SET STORAGE
doesn\*(Aqt itself change anything in the table, it just sets the strategy to be pursued during future table updates\&. See
Section 56.2, \(lqTOAST\(rq, in the documentation
for more information\&.
.RE
.PP
ADD \fItable_constraint\fR [ NOT VALID ]
.RS 4
This form adds a new constraint to a table using the same syntax as
CREATE TABLE (\fBCREATE_TABLE\fR(7)), plus the option
NOT VALID, which is currently only allowed for foreign key and CHECK constraints\&. If the constraint is marked
NOT VALID, the potentially\-lengthy initial check to verify that all rows in the table satisfy the constraint is skipped\&. The constraint will still be enforced against subsequent inserts or updates (that is, they\*(Aqll fail unless there is a matching row in the referenced table, in the case of foreign keys; and they\*(Aqll fail unless the new row matches the specified check constraints)\&. But the database will not assume that the constraint holds for all rows in the table, until it is validated by using the
VALIDATE CONSTRAINT
option\&.
.RE
.PP
ADD \fItable_constraint_using_index\fR
.RS 4
This form adds a new
PRIMARY KEY
or
UNIQUE
constraint to a table based on an existing unique index\&. All the columns of the index will be included in the constraint\&.
.sp
The index cannot have expression columns nor be a partial index\&. Also, it must be a b\-tree index with default sort ordering\&. These restrictions ensure that the index is equivalent to one that would be built by a regular
ADD PRIMARY KEY
or
ADD UNIQUE
command\&.
.sp
If
PRIMARY KEY
is specified, and the index\*(Aqs columns are not already marked
NOT NULL, then this command will attempt to do
ALTER COLUMN SET NOT NULL
against each such column\&. That requires a full table scan to verify the column(s) contain no nulls\&. In all other cases, this is a fast operation\&.
.sp
If a constraint name is provided then the index will be renamed to match the constraint name\&. Otherwise the constraint will be named the same as the index\&.
.sp
After this command is executed, the index is
\(lqowned\(rq
by the constraint, in the same way as if the index had been built by a regular
ADD PRIMARY KEY
or
ADD UNIQUE
command\&. In particular, dropping the constraint will make the index disappear too\&.
.if n \{\
.sp
.\}
.RS 4
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBNote\fR
.ps -1
.br
Adding a constraint using an existing index can be helpful in situations where a new constraint needs to be added without blocking table updates for a long time\&. To do that, create the index using
\fBCREATE INDEX CONCURRENTLY\fR, and then install it as an official constraint using this syntax\&. See the example below\&.
.sp .5v
.RE
.RE
.PP
VALIDATE CONSTRAINT
.RS 4
This form validates a foreign key or check constraint that was previously created as
NOT VALID, by scanning the table to ensure there are no rows for which the constraint is not satisfied\&. Nothing happens if the constraint is already marked valid\&.
.sp
Validation can be a long process on larger tables and currently requires an
ACCESS EXCLUSIVE
lock\&. The value of separating validation from initial creation is that you can defer validation to less busy times, or can be used to give additional time to correct pre\-existing errors while preventing new errors\&.
.RE
.PP
DROP CONSTRAINT [ IF EXISTS ]
.RS 4
This form drops the specified constraint on a table\&. If
IF EXISTS
is specified and the constraint does not exist, no error is thrown\&. In this case a notice is issued instead\&.
.RE
.PP
DISABLE/ENABLE [ REPLICA | ALWAYS ] TRIGGER
.RS 4
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 internally generated constraint triggers such as those that are used to implement foreign key constraints or deferrable uniqueness and exclusion constraints)\&. Disabling or enabling internally generated 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
session_replication_role\&. Simply enabled triggers will fire when the replication role is
\(lqorigin\(rq
(the default) or
\(lqlocal\(rq\&. Triggers configured as
ENABLE REPLICA
will only fire if the session is in
\(lqreplica\(rq
mode, and triggers configured as
ENABLE ALWAYS
will fire regardless of the current replication mode\&.
.RE
.PP
DISABLE/ENABLE [ REPLICA | ALWAYS ] RULE
.RS 4
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
ON SELECT
rules, which are always applied in order to keep views working even if the current session is in a non\-default replication role\&.
.RE
.PP
CLUSTER ON
.RS 4
This form selects the default index for future
\fBCLUSTER\fR(7)
operations\&. It does not actually re\-cluster the table\&.
.RE
.PP
SET WITHOUT CLUSTER
.RS 4
This form removes the most recently used
\fBCLUSTER\fR(7)
index specification from the table\&. This affects future cluster operations that don\*(Aqt specify an index\&.
.RE
.PP
SET WITH OIDS
.RS 4
This form adds an
oid
system column to the table (see
Section 5.4, \(lqSystem Columns\(rq, in the documentation)\&. It does nothing if the table already has OIDs\&.
.sp
Note that this is not equivalent to
ADD COLUMN oid oid; that would add a normal column that happened to be named
oid, not a system column\&.
.RE
.PP
SET WITHOUT OIDS
.RS 4
This form removes the
oid
system column from the table\&. This is exactly equivalent to
DROP COLUMN oid RESTRICT, except that it will not complain if there is already no
oid
column\&.
.RE
.PP
SET ( \fIstorage_parameter\fR = \fIvalue\fR [, \&.\&.\&. ] )
.RS 4
This form changes one or more storage parameters for the table\&. See
Storage Parameters
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
VACUUM FULL,
\fBCLUSTER\fR(7)
or one of the forms of
\fBALTER TABLE\fR
that forces a table rewrite\&.
.if n \{\
.sp
.\}
.RS 4
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBNote\fR
.ps -1
.br
While
\fBCREATE TABLE\fR
allows
OIDS
to be specified in the
WITH (\fIstorage_parameter\fR)
syntax,
\fBALTER TABLE\fR
does not treat
OIDS
as a storage parameter\&. Instead use the
SET WITH OIDS
and
SET WITHOUT OIDS
forms to change OID status\&.
.sp .5v
.RE
.RE
.PP
RESET ( \fIstorage_parameter\fR [, \&.\&.\&. ] )
.RS 4
This form resets one or more storage parameters to their defaults\&. As with
SET, a table rewrite might be needed to update the table entirely\&.
.RE
.PP
INHERIT \fIparent_table\fR
.RS 4
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
NOT NULL
constraints in the parent then they must also have
NOT NULL
constraints in the child\&.
.sp
There must also be matching child\-table constraints for all
CHECK
constraints of the parent, except those marked non\-inheritable (that is, created with
ALTER TABLE \&.\&.\&. ADD CONSTRAINT \&.\&.\&. NO INHERIT) in the parent, which are ignored; all child\-table constraints matched must not be marked non\-inheritable\&. Currently
UNIQUE,
PRIMARY KEY, and
FOREIGN KEY
constraints are not considered, but this might change in the future\&.
.RE
.PP
NO INHERIT \fIparent_table\fR
.RS 4
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\&.
.RE
.PP
OF \fItype_name\fR
.RS 4
This form links the table to a composite type as though
\fBCREATE TABLE OF\fR
had formed it\&. The table\*(Aqs list of column names and types must precisely match that of the composite type; the presence of an
oid
system column is permitted to differ\&. The table must not inherit from any other table\&. These restrictions ensure that
\fBCREATE TABLE OF\fR
would permit an equivalent table definition\&.
.RE
.PP
NOT OF
.RS 4
This form dissociates a typed table from its type\&.
.RE
.PP
OWNER
.RS 4
This form changes the owner of the table, sequence, or view to the specified user\&.
.RE
.PP
SET TABLESPACE
.RS 4
This form changes the table\*(Aqs 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
SET TABLESPACE
commands\&. See also
CREATE TABLESPACE (\fBCREATE_TABLESPACE\fR(7))\&.
.RE
.PP
RENAME
.RS 4
The
RENAME
forms change the name of a table (or an index, sequence, or view), the name of an individual column in a table, or the name of a constraint of the table\&. There is no effect on the stored data\&.
.RE
.PP
SET SCHEMA
.RS 4
This form moves the table into another schema\&. Associated indexes, constraints, and sequences owned by table columns are moved as well\&.
.RE
.PP
All the actions except
RENAME
and
SET SCHEMA
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\&.
.PP
You must own the table to use
\fBALTER TABLE\fR\&. To change the schema of a table, you must also have
CREATE
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
CREATE
privilege on the table\*(Aqs schema\&. (These restrictions enforce that altering the owner doesn\*(Aqt do anything you couldn\*(Aqt do by dropping and recreating the table\&. However, a superuser can alter ownership of any table anyway\&.) To add a column or alter a column type or use the
OF
clause, you must also have
USAGE
privilege on the data type\&.
.SH "PARAMETERS"
.PP
\fIname\fR
.RS 4
The name (optionally schema\-qualified) of an existing table to alter\&. If
ONLY
is specified before the table name, only that table is altered\&. If
ONLY
is not specified, the table and all its descendant tables (if any) are altered\&. Optionally,
*
can be specified after the table name to explicitly indicate that descendant tables are included\&.
.RE
.PP
\fIcolumn_name\fR
.RS 4
Name of a new or existing column\&.
.RE
.PP
\fInew_column_name\fR
.RS 4
New name for an existing column\&.
.RE
.PP
\fInew_name\fR
.RS 4
New name for the table\&.
.RE
.PP
\fItype\fR
.RS 4
Data type of the new column, or new data type for an existing column\&.
.RE
.PP
\fItable_constraint\fR
.RS 4
New table constraint for the table\&.
.RE
.PP
\fIconstraint_name\fR
.RS 4
Name of an existing constraint to drop\&.
.RE
.PP
CASCADE
.RS 4
Automatically drop objects that depend on the dropped column or constraint (for example, views referencing the column)\&.
.RE
.PP
RESTRICT
.RS 4
Refuse to drop the column or constraint if there are any dependent objects\&. This is the default behavior\&.
.RE
.PP
\fItrigger_name\fR
.RS 4
Name of a single trigger to disable or enable\&.
.RE
.PP
ALL
.RS 4
Disable or enable all triggers belonging to the table\&. (This requires superuser privilege if any of the triggers are internally generated constraint triggers such as those that are used to implement foreign key constraints or deferrable uniqueness and exclusion constraints\&.)
.RE
.PP
USER
.RS 4
Disable or enable all triggers belonging to the table except for internally generated constraint triggers such as those that are used to implement foreign key constraints or deferrable uniqueness and exclusion constraints\&.
.RE
.PP
\fIindex_name\fR
.RS 4
The index name on which the table should be marked for clustering\&.
.RE
.PP
\fIstorage_parameter\fR
.RS 4
The name of a table storage parameter\&.
.RE
.PP
\fIvalue\fR
.RS 4
The new value for a table storage parameter\&. This might be a number or a word depending on the parameter\&.
.RE
.PP
\fIparent_table\fR
.RS 4
A parent table to associate or de\-associate with this table\&.
.RE
.PP
\fInew_owner\fR
.RS 4
The user name of the new owner of the table\&.
.RE
.PP
\fInew_tablespace\fR
.RS 4
The name of the tablespace to which the table will be moved\&.
.RE
.PP
\fInew_schema\fR
.RS 4
The name of the schema to which the table will be moved\&.
.RE
.SH "NOTES"
.PP
The key word
COLUMN
is noise and can be omitted\&.
.PP
When a column is added with
ADD COLUMN, all existing rows in the table are initialized with the column\*(Aqs default value (NULL if no
DEFAULT
clause is specified)\&.
.PP
Adding a column with a non\-null default or changing the type of an existing column will require the entire table and indexes to be rewritten\&. As an exception, if the
USING
clause does not change the column contents and the old type is either binary coercible to the new type or an unconstrained domain over the new type, a table rewrite is not needed, but any indexes on the affected columns must still be rebuilt\&. Adding or removing a system
oid
column also requires rewriting the entire table\&. Table and/or index rebuilds may take a significant amount of time for a large table; and will temporarily require as much as double the disk space\&.
.PP
Adding a
CHECK
or
NOT NULL
constraint requires scanning the table to verify that existing rows meet the constraint, but does not require a table rewrite\&.
.PP
The main reason for providing the option to specify multiple changes in a single
\fBALTER TABLE\fR
is that multiple table scans or rewrites can thereby be combined into a single pass over the table\&.
.PP
The
DROP COLUMN
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
oid
column; that is done with an immediate rewrite\&.)
.PP
To force immediate reclamation of space occupied by a dropped column, you can execute one of the forms of
\fBALTER TABLE\fR
that performs a rewrite of the whole table\&. This results in reconstructing each row with the dropped column replaced by a null value\&.
.PP
The rewriting forms of
\fBALTER TABLE\fR
are not MVCC\-safe\&. After a table rewrite, the table will appear empty to concurrent transactions, if they are using a snapshot taken before the rewrite occurred\&. See
Section 13.5, \(lqCaveats\(rq, in the documentation
for more details\&.
.PP
The
USING
option of
SET DATA TYPE
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
SET DATA TYPE
syntax\&. Because of this flexibility, the
USING
expression is not applied to the column\*(Aqs 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,
SET DATA TYPE
might fail to convert the default even though a
USING
clause is supplied\&. In such cases, drop the default with
DROP DEFAULT, perform the
ALTER TYPE, and then use
SET DEFAULT
to add a suitable new default\&. Similar considerations apply to indexes and constraints involving the column\&.
.PP
If a table has any descendant tables, it is not permitted to add, rename, or change the type of a column, or rename an inherited constraint in the parent table without doing the same to the descendants\&. That is,
\fBALTER TABLE ONLY\fR
will be rejected\&. This ensures that the descendants always have columns matching the parent\&.
.PP
A recursive
DROP COLUMN
operation will remove a descendant table\*(Aqs 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
DROP COLUMN
(i\&.e\&.,
\fBALTER TABLE ONLY \&.\&.\&. DROP COLUMN\fR) never removes any descendant columns, but instead marks them as independently defined rather than inherited\&.
.PP
The
TRIGGER,
CLUSTER,
OWNER, and
TABLESPACE
actions never recurse to descendant tables; that is, they always act as though
ONLY
were specified\&. Adding a constraint recurses only for
CHECK
constraints that are not marked
NO INHERIT\&.
.PP
Changing any part of a system catalog table is not permitted\&.
.PP
Refer to
CREATE TABLE (\fBCREATE_TABLE\fR(7))
for a further description of valid parameters\&.
Chapter 5, Data Definition, in the documentation
has further information on inheritance\&.
.SH "EXAMPLES"
.PP
To add a column of type
varchar
to a table:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER TABLE distributors ADD COLUMN address varchar(30);
.fi
.if n \{\
.RE
.\}
.PP
To drop a column from a table:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER TABLE distributors DROP COLUMN address RESTRICT;
.fi
.if n \{\
.RE
.\}
.PP
To change the types of two existing columns in one operation:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER TABLE distributors
    ALTER COLUMN address TYPE varchar(80),
    ALTER COLUMN name TYPE varchar(100);
.fi
.if n \{\
.RE
.\}
.PP
To change an integer column containing UNIX timestamps to
timestamp with time zone
via a
USING
clause:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER TABLE foo
    ALTER COLUMN foo_timestamp SET DATA TYPE timestamp with time zone
    USING
        timestamp with time zone \*(Aqepoch\*(Aq + foo_timestamp * interval \*(Aq1 second\*(Aq;
.fi
.if n \{\
.RE
.\}
.PP
The same, when the column has a default expression that won\*(Aqt automatically cast to the new data type:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER TABLE foo
    ALTER COLUMN foo_timestamp DROP DEFAULT,
    ALTER COLUMN foo_timestamp TYPE timestamp with time zone
    USING
        timestamp with time zone \*(Aqepoch\*(Aq + foo_timestamp * interval \*(Aq1 second\*(Aq,
    ALTER COLUMN foo_timestamp SET DEFAULT now();
.fi
.if n \{\
.RE
.\}
.PP
To rename an existing column:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER TABLE distributors RENAME COLUMN address TO city;
.fi
.if n \{\
.RE
.\}
.PP
To rename an existing table:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER TABLE distributors RENAME TO suppliers;
.fi
.if n \{\
.RE
.\}
.PP
To rename an existing constraint:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER TABLE distributors RENAME CONSTRAINT zipchk TO zip_check;
.fi
.if n \{\
.RE
.\}
.PP
To add a not\-null constraint to a column:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER TABLE distributors ALTER COLUMN street SET NOT NULL;
.fi
.if n \{\
.RE
.\}
.sp
To remove a not\-null constraint from a column:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;
.fi
.if n \{\
.RE
.\}
.PP
To add a check constraint to a table and all its children:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);
.fi
.if n \{\
.RE
.\}
.PP
To add a check constraint only to a table and not to its children:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5) NO INHERIT;
.fi
.if n \{\
.RE
.\}
.sp
(The check constraint will not be inherited by future children, either\&.)
.PP
To remove a check constraint from a table and all its children:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER TABLE distributors DROP CONSTRAINT zipchk;
.fi
.if n \{\
.RE
.\}
.PP
To remove a check constraint from one table only:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER TABLE ONLY distributors DROP CONSTRAINT zipchk;
.fi
.if n \{\
.RE
.\}
.sp
(The check constraint remains in place for any child tables\&.)
.PP
To add a foreign key constraint to a table:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address);
.fi
.if n \{\
.RE
.\}
.PP
To add a (multicolumn) unique constraint to a table:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);
.fi
.if n \{\
.RE
.\}
.PP
To add an automatically named primary key constraint to a table, noting that a table can only ever have one primary key:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER TABLE distributors ADD PRIMARY KEY (dist_id);
.fi
.if n \{\
.RE
.\}
.PP
To move a table to a different tablespace:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER TABLE distributors SET TABLESPACE fasttablespace;
.fi
.if n \{\
.RE
.\}
.PP
To move a table to a different schema:
.sp
.if n \{\
.RS 4
.\}
.nf
ALTER TABLE myschema\&.distributors SET SCHEMA yourschema;
.fi
.if n \{\
.RE
.\}
.PP
To recreate a primary key constraint, without blocking updates while the index is rebuilt:
.sp
.if n \{\
.RS 4
.\}
.nf
CREATE UNIQUE INDEX CONCURRENTLY dist_id_temp_idx ON distributors (dist_id);
ALTER TABLE distributors DROP CONSTRAINT distributors_pkey,
    ADD CONSTRAINT distributors_pkey PRIMARY KEY USING INDEX dist_id_temp_idx;
.fi
.if n \{\
.RE
.\}
.SH "COMPATIBILITY"
.PP
The forms
ADD
(without
USING INDEX),
DROP,
SET DEFAULT, and
SET DATA TYPE
(without
USING) conform with the SQL standard\&. The other forms are
PostgreSQL
extensions of the SQL standard\&. Also, the ability to specify more than one manipulation in a single
\fBALTER TABLE\fR
command is an extension\&.
.PP
\fBALTER TABLE DROP COLUMN\fR
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\&.
.SH "SEE ALSO"
CREATE TABLE (\fBCREATE_TABLE\fR(7))

Zerion Mini Shell 1.0