1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
8
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
10
HREF="mailto:pgsql-docs@postgresql.org"><LINK
12
TITLE="PostgreSQL 9.1beta1 Documentation"
13
HREF="index.html"><LINK
16
HREF="sql-commands.html"><LINK
19
HREF="sql-alterserver.html"><LINK
21
TITLE="ALTER TABLESPACE"
22
HREF="sql-altertablespace.html"><LINK
25
HREF="stylesheet.css"><META
26
HTTP-EQUIV="Content-Type"
27
CONTENT="text/html; charset=ISO-8859-1"><META
29
CONTENT="2011-04-27T21:20:33"></HEAD
35
SUMMARY="Header navigation table"
47
>PostgreSQL 9.1beta1 Documentation</A
57
HREF="sql-alterserver.html"
67
HREF="sql-alterserver.html"
80
TITLE="ALTER TABLESPACE"
81
HREF="sql-altertablespace.html"
89
TITLE="ALTER TABLESPACE"
90
HREF="sql-altertablespace.html"
101
NAME="SQL-ALTERTABLE"
111
>ALTER TABLE -- change the definition of a table</DIV
113
CLASS="REFSYNOPSISDIV"
121
>ALTER TABLE [ ONLY ] <TT
133
ALTER TABLE [ ONLY ] <TT
139
RENAME [ COLUMN ] <TT
206
>column_constraint</I
209
DROP [ COLUMN ] [ IF EXISTS ] <TT
214
> [ RESTRICT | CASCADE ]
220
> [ SET DATA ] TYPE <TT
258
> { SET | DROP } NOT NULL
302
> SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
312
>table_constraint_using_index</I
321
VALIDATE CONSTRAINT <TT
327
DROP CONSTRAINT [ IF EXISTS ] <TT
332
> [ RESTRICT | CASCADE ]
333
DISABLE TRIGGER [ <TT
345
ENABLE REPLICA TRIGGER <TT
351
ENABLE ALWAYS TRIGGER <TT
360
>rewrite_rule_name</I
366
>rewrite_rule_name</I
369
ENABLE REPLICA RULE <TT
372
>rewrite_rule_name</I
375
ENABLE ALWAYS RULE <TT
378
>rewrite_rule_name</I
393
>storage_parameter</I
404
>storage_parameter</I
446
>table_constraint_using_index</I
458
{ UNIQUE | PRIMARY KEY } USING INDEX <TT
464
[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]</PRE
477
> changes the definition of an existing table.
478
There are several subforms:
492
> This form adds a new column to the table, using the same syntax as
494
HREF="sql-createtable.html"
502
>DROP COLUMN [ IF EXISTS ]</TT
506
> This form drops a column from a table. Indexes and
507
table constraints involving the column will be automatically
508
dropped as well. You will need to say <TT
512
anything outside the table depends on the column, for example,
513
foreign key references or views.
517
> is specified and the column
518
does not exist, no error is thrown. In this case a notice
529
> This form changes the type of a column of a table. Indexes and
530
simple table constraints involving the column will be automatically
531
converted to use the new column type by reparsing the originally
536
> clause specifies a collation
537
for the new column; if omitted, the collation is the default for the
543
clause specifies how to compute the new column value from the old;
544
if omitted, the default conversion is the same as an assignment
545
cast from old data type to new. A <TT
549
clause must be provided if there is no implicit or assignment
550
cast from old to new type.
563
> These forms set or remove the default value for a column.
564
The default values only apply to subsequent <TT
568
commands; they do not cause rows already in the table to change.
569
Defaults can also be created for views, in which case they are
573
> statements on the view before
590
> These forms change whether a column is marked to allow null
591
values or to reject null values. You can only use <TT
595
> when the column contains no null values.
606
sets the per-column statistics-gathering target for subsequent
608
HREF="sql-analyze.html"
611
The target can be set in the range 0 to 10000; alternatively, set it
612
to -1 to revert to using the system default statistics
614
HREF="runtime-config-query.html#GUC-DEFAULT-STATISTICS-TARGET"
615
>default_statistics_target</A
617
For more information on the use of statistics by the
621
> query planner, refer to
623
HREF="planner-stats.html"
653
> This form sets or resets per-attribute options. Currently, the only
654
defined per-attribute options are <TT
660
>n_distinct_inherited</TT
661
>, which override the
662
number-of-distinct-values estimates made by subsequent
664
HREF="sql-analyze.html"
670
> affects the statistics for the table
673
>n_distinct_inherited</TT
674
> affects the statistics
675
gathered for the table plus its inheritance children. When set to a
679
> will assume that the column contains
680
exactly the specified number of distinct nonnull values. When set to a
681
negative value, which must be greater
682
than or equal to -1, <TT
685
> will assume that the number of
686
distinct nonnull values in the column is linear in the size of the
687
table; the exact count is to be computed by multiplying the estimated
688
table size by the absolute value of the given number. For example,
689
a value of -1 implies that all values in the column are distinct, while
690
a value of -0.5 implies that each value appears twice on the average.
691
This can be useful when the size of the table changes over time, since
692
the multiplication by the number of rows in the table is not performed
693
until query planning time. Specify a value of 0 to revert to estimating
694
the number of distinct values normally. For more information on the use
695
of statistics by the <SPAN
700
HREF="planner-stats.html"
712
> This form sets the storage mode for a column. This controls whether this
713
column is held inline or in a secondary <ACRONYM
718
should be compressed or not. <TT
722
for fixed-length values such as <TT
726
inline, uncompressed. <TT
730
compressible data. <TT
734
uncompressed data, and <TT
741
> is the default for most
742
data types that support non-<TT
749
> will make substring operations on
757
at the penalty of increased storage space. Note that
761
> doesn't itself change anything in the table,
762
it just sets the strategy to be pursued during future table updates.
764
HREF="storage-toast.html"
766
> for more information.
782
> This form adds a new constraint to a table using the same syntax as
784
HREF="sql-createtable.html"
786
>. Newly added foreign key constraints can
787
also be defined as <TT
791
potentially lengthy initial check that must otherwise be performed.
792
Constraint checks are skipped at create table time, so
794
HREF="sql-createtable.html"
796
> does not contain this option.
802
>VALIDATE CONSTRAINT</TT
806
> This form validates a foreign key constraint that was previously created
810
>. Constraints already marked valid do not
811
cause an error response.
820
>table_constraint_using_index</I
826
> This form adds a new <TT
833
constraint to a table based on an existing unique index. All the
834
columns of the index will be included in the constraint.
837
> The index cannot have expression columns nor be a partial index.
838
Also, it must be a b-tree index with default sort ordering. These
839
restrictions ensure that the index is equivalent to one that would be
840
built by a regular <TT
853
> is specified, and the index's columns are not
857
>, then this command will attempt to
860
>ALTER COLUMN SET NOT NULL</TT
861
> against each such column.
862
That requires a full table scan to verify the column(s) contain no
863
nulls. In all other cases, this is a fast operation.
866
> If a constraint name is provided then the index will be renamed to match
867
the constraint name. Otherwise the constraint will be named the same as
871
> After this command is executed, the index is <SPAN
875
constraint, in the same way as if the index had been built by
883
command. In particular, dropping the constraint will make the index
893
> Adding a constraint using an existing index can be helpful in
894
situations where a new constraint needs to be added without blocking
895
table updates for a long time. To do that, create the index using
898
>CREATE INDEX CONCURRENTLY</TT
899
>, and then install it as an
900
official constraint using this syntax. See the example below.
908
>DROP CONSTRAINT [ IF EXISTS ]</TT
912
> This form drops the specified constraint on a table.
916
> is specified and the constraint
917
does not exist, no error is thrown. In this case a notice is issued instead.
926
>ENABLE [ REPLICA | ALWAYS ] TRIGGER</TT
930
> These forms configure the firing of trigger(s) belonging to the table.
931
A disabled trigger is still known to the system, but is not executed
932
when its triggering event occurs. For a deferred trigger, the enable
933
status is checked when the event occurs, not when the trigger function
934
is actually executed. One can disable or enable a single
935
trigger specified by name, or all triggers on the table, or only
936
user triggers (this option excludes internally generated constraint
937
triggers such as those that are used to implement foreign key
938
constraints or deferrable uniqueness and exclusion constraints).
939
Disabling or enabling internally generated constraint triggers
940
requires superuser privileges; it should be done with caution since
941
of course the integrity of the constraint cannot be guaranteed if the
942
triggers are not executed.
943
The trigger firing mechanism is also affected by the configuration
945
HREF="runtime-config-client.html#GUC-SESSION-REPLICATION-ROLE"
946
>session_replication_role</A
948
triggers will fire when the replication role is <SPAN
952
(the default) or <SPAN
955
>. Triggers configured as <TT
959
> will only fire if the session is in <SPAN
963
mode, and triggers configured as <TT
967
fire regardless of the current replication mode.
976
>ENABLE [ REPLICA | ALWAYS ] RULE</TT
980
> These forms configure the firing of rewrite rules belonging to the table.
981
A disabled rule is still known to the system, but is not applied
982
during query rewriting. The semantics are as for disabled/enabled
983
triggers. This configuration is ignored for <TT
987
are always applied in order to keep views working even if the current
988
session is in a non-default replication role.
998
> This form selects the default index for future
1000
HREF="sql-cluster.html"
1003
operations. It does not actually re-cluster the table.
1009
>SET WITHOUT CLUSTER</TT
1013
> This form removes the most recently used
1015
HREF="sql-cluster.html"
1018
index specification from the table. This affects
1019
future cluster operations that don't specify an index.
1029
> This form adds an <TT
1032
> system column to the
1034
HREF="ddl-system-columns.html"
1037
It does nothing if the table already has OIDs.
1040
> Note that this is not equivalent to <TT
1042
>ADD COLUMN oid oid</TT
1044
that would add a normal column that happened to be named
1048
>, not a system column.
1054
>SET WITHOUT OIDS</TT
1058
> This form removes the <TT
1061
> system column from the
1062
table. This is exactly equivalent to
1065
>DROP COLUMN oid RESTRICT</TT
1067
except that it will not complain if there is already no
1080
>storage_parameter</I
1091
> This form changes one or more storage parameters for the table. See
1093
HREF="sql-createtable.html#SQL-CREATETABLE-STORAGE-PARAMETERS"
1095
>Storage Parameters</I
1098
for details on the available parameters. Note that the table contents
1099
will not be modified immediately by this command; depending on the
1100
parameter you might need to rewrite the table to get the desired effects.
1101
That can be done with <A
1102
HREF="sql-vacuum.html"
1106
HREF="sql-cluster.html"
1108
> or one of the forms
1112
> that forces a table rewrite.
1133
>storage_parameter</I
1140
> does not treat <TT
1144
storage parameter. Instead use the <TT
1150
>SET WITHOUT OIDS</TT
1151
> forms to change OID status.
1162
>storage_parameter</I
1168
> This form resets one or more storage parameters to their
1169
defaults. As with <TT
1172
>, a table rewrite might be
1173
needed to update the table entirely.
1188
> This form adds the target table as a new child of the specified parent
1189
table. Subsequently, queries against the parent will include records
1190
of the target table. To be added as a child, the target table must
1191
already contain all the same columns as the parent (it could have
1192
additional columns, too). The columns must have matching data types,
1193
and if they have <TT
1196
> constraints in the parent
1197
then they must also have <TT
1200
> constraints in the
1204
> There must also be matching child-table constraints for all
1208
> constraints of the parent. Currently
1219
> constraints are not considered, but
1220
this might change in the future.
1235
> This form removes the target table from the list of children of the
1236
specified parent table.
1237
Queries against the parent table will no longer include records drawn
1238
from the target table.
1253
> This form links the table to a composite type as though <TT
1257
> had formed it. The table's list of column names and types
1258
must precisely match that of the composite type; the presence of
1262
> system column is permitted to differ. The table must
1263
not inherit from any other table. These restrictions ensure
1266
>CREATE TABLE OF</TT
1267
> would permit an equivalent table
1278
> This form dissociates a typed table from its type.
1288
> This form changes the owner of the table, sequence, or view to the
1299
> This form changes the table's tablespace to the specified tablespace and
1300
moves the data file(s) associated with the table to the new tablespace.
1301
Indexes on the table, if any, are not moved; but they can be moved
1302
separately with additional <TT
1308
HREF="sql-createtablespace.html"
1309
>CREATE TABLESPACE</A
1323
> forms change the name of a table
1324
(or an index, sequence, or view) or the name of an individual column in
1325
a table. There is no effect on the stored data.
1335
> This form moves the table into another schema. Associated indexes,
1336
constraints, and sequences owned by table columns are moved as well.
1344
> All the actions except <TT
1351
can be combined into
1352
a list of multiple alterations to apply in parallel. For example, it
1353
is possible to add several columns and/or alter the type of several
1354
columns in a single command. This is particularly useful with large
1355
tables, since only one pass over the table need be made.
1358
> You must own the table to use <TT
1362
To change the schema of a table, you must also have
1366
> privilege on the new schema.
1367
To add the table as a new child of a parent table, you must own the
1368
parent table as well.
1369
To alter the owner, you must also be a direct or indirect member of the new
1370
owning role, and that role must have <TT
1374
the table's schema. (These restrictions enforce that altering the owner
1375
doesn't do anything you couldn't do by dropping and recreating the table.
1376
However, a superuser can alter ownership of any table anyway.)
1389
CLASS="VARIABLELIST"
1400
> The name (possibly schema-qualified) of an existing table to
1404
> is specified, only that table is
1408
> is not specified, the table and any
1409
descendant tables are altered.
1421
> Name of a new or existing column.
1433
> New name for an existing column.
1445
> New name for the table.
1457
> Data type of the new column, or new data type for an existing
1465
>table_constraint</I
1470
> New table constraint for the table.
1482
> Name of an existing constraint to drop.
1492
> Automatically drop objects that depend on the dropped column
1493
or constraint (for example, views referencing the column).
1503
> Refuse to drop the column or constraint if there are any dependent
1504
objects. This is the default behavior.
1516
> Name of a single trigger to disable or enable.
1526
> Disable or enable all triggers belonging to the table.
1527
(This requires superuser privilege if any of the triggers are
1528
internally generated constraint triggers such as those that are used
1529
to implement foreign key constraints or deferrable uniqueness and
1530
exclusion constraints.)
1540
> Disable or enable all triggers belonging to the table except for
1541
internally generated constraint triggers such as those that are used
1542
to implement foreign key constraints or deferrable uniqueness and
1543
exclusion constraints.
1555
> The index name on which the table should be marked for clustering.
1562
>storage_parameter</I
1567
> The name of a table storage parameter.
1579
> The new value for a table storage parameter.
1580
This might be a number or a word depending on the parameter.
1592
> A parent table to associate or de-associate with this table.
1604
> The user name of the new owner of the table.
1616
> The name of the tablespace to which the table will be moved.
1628
> The name of the schema to which the table will be moved.
1645
> is noise and can be omitted.
1648
> When a column is added with <TT
1652
rows in the table are initialized with the column's default value
1656
> clause is specified).
1659
> Adding a column with a non-null default or changing the type of an
1660
existing column will require the entire table and indexes to be rewritten.
1661
As an exception, if the <TT
1664
> clause does not change the column
1665
contents and the old type is either binary coercible to the new type or
1666
an unconstrained domain over the new type, a table rewrite is not needed,
1667
but any indexes on the affected columns must still be rebuilt. Adding or
1668
removing a system <TT
1671
> column also requires rewriting the entire
1672
table. Table and/or index rebuilds may take a significant amount of time
1673
for a large table; and will temporarily require as much as double the disk
1683
> constraint requires
1684
scanning the table to verify that existing rows meet the constraint.
1687
> The main reason for providing the option to specify multiple changes
1691
> is that multiple table scans or
1692
rewrites can thereby be combined into a single pass over the table.
1698
> form does not physically remove
1699
the column, but simply makes it invisible to SQL operations. Subsequent
1700
insert and update operations in the table will store a null value for the
1701
column. Thus, dropping a column is quick but it will not immediately
1702
reduce the on-disk size of your table, as the space occupied
1703
by the dropped column is not reclaimed. The space will be
1704
reclaimed over time as existing rows are updated. (These statements do
1705
not apply when dropping the system <TT
1708
> column; that is done
1709
with an immediate rewrite.)
1712
> To force an immediate rewrite of the table, you can use
1714
HREF="sql-vacuum.html"
1717
HREF="sql-cluster.html"
1720
or one of the forms of ALTER TABLE that forces a rewrite. This results in
1721
no semantically-visible change in the table, but gets rid of
1722
no-longer-useful data.
1732
specify any expression involving the old values of the row; that is, it
1733
can refer to other columns as well as the one being converted. This allows
1734
very general conversions to be done with the <TT
1738
syntax. Because of this flexibility, the <TT
1742
expression is not applied to the column's default value (if any); the
1743
result might not be a constant expression as required for a default.
1744
This means that when there is no implicit or assignment cast from old to
1748
> might fail to convert the default even
1752
> clause is supplied. In such cases,
1753
drop the default with <TT
1763
> to add a suitable new
1764
default. Similar considerations apply to indexes and constraints involving
1768
> If a table has any descendant tables, it is not permitted to add,
1769
rename, or change the type of a column in the parent table without doing
1770
the same to the descendants. That is, <TT
1772
>ALTER TABLE ONLY</TT
1774
will be rejected. This ensures that the descendants always have
1775
columns matching the parent.
1781
> operation will remove a
1782
descendant table's column only if the descendant does not inherit
1783
that column from any other parents and never had an independent
1784
definition of the column. A nonrecursive <TT
1790
>ALTER TABLE ONLY ... DROP
1792
>) never removes any descendant columns, but
1793
instead marks them as independently defined rather than inherited.
1809
> actions never recurse to descendant tables;
1810
that is, they always act as though <TT
1814
Adding a constraint can recurse only for <TT
1818
and is required to do so for such constraints.
1821
> Changing any part of a system catalog table is not permitted.
1825
HREF="sql-createtable.html"
1827
> for a further description of valid
1831
> has further information on
1843
> To add a column of type <TT
1848
CLASS="PROGRAMLISTING"
1849
>ALTER TABLE distributors ADD COLUMN address varchar(30);</PRE
1853
> To drop a column from a table:
1855
CLASS="PROGRAMLISTING"
1856
>ALTER TABLE distributors DROP COLUMN address RESTRICT;</PRE
1860
> To change the types of two existing columns in one operation:
1862
CLASS="PROGRAMLISTING"
1863
>ALTER TABLE distributors
1864
ALTER COLUMN address TYPE varchar(80),
1865
ALTER COLUMN name TYPE varchar(100);</PRE
1869
> To change an integer column containing UNIX timestamps to <TT
1878
CLASS="PROGRAMLISTING"
1880
ALTER COLUMN foo_timestamp SET DATA TYPE timestamp with time zone
1882
timestamp with time zone 'epoch' + foo_timestamp * interval '1 second';</PRE
1886
> The same, when the column has a default expression that won't automatically
1887
cast to the new data type:
1889
CLASS="PROGRAMLISTING"
1891
ALTER COLUMN foo_timestamp DROP DEFAULT,
1892
ALTER COLUMN foo_timestamp TYPE timestamp with time zone
1894
timestamp with time zone 'epoch' + foo_timestamp * interval '1 second',
1895
ALTER COLUMN foo_timestamp SET DEFAULT now();</PRE
1899
> To rename an existing column:
1901
CLASS="PROGRAMLISTING"
1902
>ALTER TABLE distributors RENAME COLUMN address TO city;</PRE
1906
> To rename an existing table:
1908
CLASS="PROGRAMLISTING"
1909
>ALTER TABLE distributors RENAME TO suppliers;</PRE
1913
> To add a not-null constraint to a column:
1915
CLASS="PROGRAMLISTING"
1916
>ALTER TABLE distributors ALTER COLUMN street SET NOT NULL;</PRE
1918
To remove a not-null constraint from a column:
1920
CLASS="PROGRAMLISTING"
1921
>ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;</PRE
1925
> To add a check constraint to a table and all its children:
1927
CLASS="PROGRAMLISTING"
1928
>ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);</PRE
1932
> To remove a check constraint from a table and all its children:
1934
CLASS="PROGRAMLISTING"
1935
>ALTER TABLE distributors DROP CONSTRAINT zipchk;</PRE
1939
> To remove a check constraint from a table only:
1941
CLASS="PROGRAMLISTING"
1942
>ALTER TABLE ONLY distributors DROP CONSTRAINT zipchk;</PRE
1944
(The check constraint remains in place for any child tables.)
1947
> To add a foreign key constraint to a table:
1949
CLASS="PROGRAMLISTING"
1950
>ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) MATCH FULL;</PRE
1954
> To add a (multicolumn) unique constraint to a table:
1956
CLASS="PROGRAMLISTING"
1957
>ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);</PRE
1961
> To add an automatically named primary key constraint to a table, noting
1962
that a table can only ever have one primary key:
1964
CLASS="PROGRAMLISTING"
1965
>ALTER TABLE distributors ADD PRIMARY KEY (dist_id);</PRE
1969
> To move a table to a different tablespace:
1971
CLASS="PROGRAMLISTING"
1972
>ALTER TABLE distributors SET TABLESPACE fasttablespace;</PRE
1976
> To move a table to a different schema:
1978
CLASS="PROGRAMLISTING"
1979
>ALTER TABLE myschema.distributors SET SCHEMA yourschema;</PRE
1983
> To recreate a primary key constraint, without blocking updates while the
1986
CLASS="PROGRAMLISTING"
1987
>CREATE UNIQUE INDEX CONCURRENTLY dist_id_temp_idx ON distributors (dist_id);
1988
ALTER TABLE distributors DROP CONSTRAINT distributors_pkey,
1989
ADD CONSTRAINT distributors_pkey PRIMARY KEY USING INDEX dist_id_temp_idx;</PRE
2022
conform with the SQL standard. The other forms are
2026
> extensions of the SQL standard.
2027
Also, the ability to specify more than one manipulation in a single
2031
> command is an extension.
2036
>ALTER TABLE DROP COLUMN</TT
2037
> can be used to drop the only
2038
column of a table, leaving a zero-column table. This is an
2039
extension of SQL, which disallows zero-column tables.
2050
HREF="sql-createtable.html"
2058
SUMMARY="Footer navigation table"
2069
HREF="sql-alterserver.html"
2087
HREF="sql-altertablespace.html"
2103
HREF="sql-commands.html"
2111
>ALTER TABLESPACE</TD
b'\\ No newline at end of file'