2 $PostgreSQL: pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.85 2006/07/02 01:58:36 momjian Exp $
3 PostgreSQL documentation
6 <refentry id="SQL-ALTERTABLE">
8 <refentrytitle id="sql-altertable-title">ALTER TABLE</refentrytitle>
9 <refmiscinfo>SQL - Language Statements</refmiscinfo>
13 <refname>ALTER TABLE</refname>
14 <refpurpose>change the definition of a table</refpurpose>
17 <indexterm zone="sql-altertable">
18 <primary>ALTER TABLE</primary>
23 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ]
24 <replaceable class="PARAMETER">action</replaceable> [, ... ]
25 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [ * ]
26 RENAME [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> TO <replaceable class="PARAMETER">new_column</replaceable>
27 ALTER TABLE <replaceable class="PARAMETER">name</replaceable>
28 RENAME TO <replaceable class="PARAMETER">new_name</replaceable>
29 ALTER TABLE <replaceable class="PARAMETER">name</replaceable>
30 SET SCHEMA <replaceable class="PARAMETER">new_schema</replaceable>
32 where <replaceable class="PARAMETER">action</replaceable> is one of:
34 ADD [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> <replaceable class="PARAMETER">type</replaceable> [ <replaceable class="PARAMETER">column_constraint</replaceable> [ ... ] ]
35 DROP [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> [ RESTRICT | CASCADE ]
36 ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> TYPE <replaceable class="PARAMETER">type</replaceable> [ USING <replaceable class="PARAMETER">expression</replaceable> ]
37 ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET DEFAULT <replaceable class="PARAMETER">expression</replaceable>
38 ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> DROP DEFAULT
39 ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> { SET | DROP } NOT NULL
40 ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET STATISTICS <replaceable class="PARAMETER">integer</replaceable>
41 ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
42 ADD <replaceable class="PARAMETER">table_constraint</replaceable>
43 DROP CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> [ RESTRICT | CASCADE ]
44 DISABLE TRIGGER [ <replaceable class="PARAMETER">trigger_name</replaceable> | ALL | USER ]
45 ENABLE TRIGGER [ <replaceable class="PARAMETER">trigger_name</replaceable> | ALL | USER ]
46 CLUSTER ON <replaceable class="PARAMETER">index_name</replaceable>
49 INHERIT <replaceable class="PARAMETER">parent_table</replaceable>
50 NO INHERIT <replaceable class="PARAMETER">parent_table</replaceable>
51 OWNER TO <replaceable class="PARAMETER">new_owner</replaceable>
52 SET TABLESPACE <replaceable class="PARAMETER">new_tablespace</replaceable>
57 <title>Description</title>
60 <command>ALTER TABLE</command> changes the definition of an existing table.
61 There are several subforms:
65 <term><literal>ADD COLUMN</literal></term>
68 This form adds a new column to the table, using the same syntax as
69 <xref linkend="SQL-CREATETABLE" endterm="SQL-CREATETABLE-TITLE">.
75 <term><literal>DROP COLUMN</literal></term>
78 This form drops a column from a table. Indexes and
79 table constraints involving the column will be automatically
80 dropped as well. You will need to say <literal>CASCADE</> if
81 anything outside the table depends on the column, for example,
82 foreign key references or views.
88 <term><literal>ALTER COLUMN TYPE</literal></term>
91 This form changes the type of a column of a table. Indexes and
92 simple table constraints involving the column will be automatically
93 converted to use the new column type by reparsing the originally
94 supplied expression. The optional <literal>USING</literal>
95 clause specifies how to compute the new column value from the old;
96 if omitted, the default conversion is the same as an assignment
97 cast from old data type to new. A <literal>USING</literal>
98 clause must be provided if there is no implicit or assignment
99 cast from old to new type.
105 <term><literal>SET</literal>/<literal>DROP DEFAULT</literal></term>
108 These forms set or remove the default value for a column.
109 The default values only apply to subsequent <command>INSERT</command>
110 commands; they do not cause rows already in the table to change.
111 Defaults may also be created for views, in which case they are
112 inserted into <command>INSERT</> statements on the view before
113 the view's <literal>ON INSERT</literal> rule is applied.
119 <term><literal>SET</literal>/<literal>DROP NOT NULL</literal></term>
122 These forms change whether a column is marked to allow null
123 values or to reject null values. You can only use <literal>SET
124 NOT NULL</> when the column contains no null values.
130 <term><literal>SET STATISTICS</literal></term>
134 sets the per-column statistics-gathering target for subsequent
135 <xref linkend="sql-analyze" endterm="sql-analyze-title"> operations.
136 The target can be set in the range 0 to 1000; alternatively, set it
137 to -1 to revert to using the system default statistics
138 target (<xref linkend="guc-default-statistics-target">).
139 For more information on the use of statistics by the
140 <productname>PostgreSQL</productname> query planner, refer to
141 <xref linkend="planner-stats">.
148 <primary>TOAST</primary>
149 <secondary>per-column storage settings</secondary>
152 <term><literal>SET STORAGE</literal></term>
155 This form sets the storage mode for a column. This controls whether this
156 column is held inline or in a supplementary table, and whether the data
157 should be compressed or not. <literal>PLAIN</literal> must be used
158 for fixed-length values such as <type>integer</type> and is
159 inline, uncompressed. <literal>MAIN</literal> is for inline,
160 compressible data. <literal>EXTERNAL</literal> is for external,
161 uncompressed data, and <literal>EXTENDED</literal> is for external,
162 compressed data. <literal>EXTENDED</literal> is the default for most
163 data types that support non-<literal>PLAIN</literal> storage.
164 Use of <literal>EXTERNAL</literal> will
165 make substring operations on <type>text</type> and <type>bytea</type>
166 columns faster, at the penalty of increased storage space. Note that
167 <literal>SET STORAGE</> doesn't itself change anything in the table,
168 it just sets the strategy to be pursued during future table updates.
169 See <xref linkend="storage-toast"> for more information.
175 <term><literal>ADD <replaceable class="PARAMETER">table_constraint</replaceable></literal></term>
178 This form adds a new constraint to a table using the same syntax as
179 <xref linkend="SQL-CREATETABLE" endterm="SQL-CREATETABLE-TITLE">.
185 <term><literal>DROP CONSTRAINT</literal></term>
188 This form drops the specified constraint on a table.
194 <term><literal>DISABLE</literal>/<literal>ENABLE TRIGGER</literal></term>
197 These forms disable or enable trigger(s) belonging to the table.
198 A disabled trigger is still known to the system, but is not executed
199 when its triggering event occurs. For a deferred trigger, the enable
200 status is checked when the event occurs, not when the trigger function
201 is actually executed. One may disable or enable a single
202 trigger specified by name, or all triggers on the table, or only
203 user triggers (this option excludes triggers that are used to implement
204 foreign key constraints). Disabling or enabling constraint triggers
205 requires superuser privileges; it should be done with caution since
206 of course the integrity of the constraint cannot be guaranteed if the
207 triggers are not executed.
213 <term><literal>CLUSTER</literal></term>
216 This form selects the default index for future
217 <xref linkend="SQL-CLUSTER" endterm="sql-cluster-title">
218 operations. It does not actually re-cluster the table.
224 <term><literal>SET WITHOUT CLUSTER</literal></term>
227 This form removes the most recently used
228 <xref linkend="SQL-CLUSTER" endterm="sql-cluster-title">
229 index specification from the table. This affects
230 future cluster operations that don't specify an index.
236 <term><literal>SET WITHOUT OIDS</literal></term>
239 This form removes the <literal>oid</literal> system column from the
240 table. This is exactly equivalent to
241 <literal>DROP COLUMN oid RESTRICT</literal>,
242 except that it will not complain if there is already no
243 <literal>oid</literal> column.
247 Note that there is no variant of <command>ALTER TABLE</command>
248 that allows OIDs to be restored to a table once they have been
255 <term><literal>INHERIT <replaceable class="PARAMETER">parent_table</replaceable></literal></term>
259 This form adds a new parent table to the table. This won't add new
260 columns to the child table, instead all columns of the parent table must
261 already exist in the child table. They must have matching data types,
262 and if they have <literal>NOT NULL</literal> constraints in the parent
263 then they must also have <literal>NOT NULL</literal> constraints in the
269 There must also be matching table constraints for all
270 <literal>CHECK</literal> table constraints of the parent. Currently
271 <literal>UNIQUE</literal>, <literal>PRIMARY KEY</literal>, and
272 <literal>FOREIGN KEY</literal> constraints are ignored however this may
273 change in the future.
278 The easiest way to create a suitable table is to create a table using
279 <literal>INHERITS</literal> and then remove it via <literal>NO
280 INHERIT</literal>. Alternatively create a table using
281 <literal>LIKE</literal> however note that <literal>LIKE</literal> does
282 not create the necessary constraints.
290 <term><literal>NO INHERIT <replaceable class="PARAMETER">parent_table</replaceable></literal></term>
293 This form removes a parent table from the list of parents of the table.
294 Queries against the parent table will no longer include records drawn
295 from the target table.
301 <term><literal>OWNER</literal></term>
304 This form changes the owner of the table, sequence, or view to the
311 <term><literal>SET TABLESPACE</literal></term>
314 This form changes the table's tablespace to the specified tablespace and
315 moves the data file(s) associated with the table to the new tablespace.
316 Indexes on the table, if any, are not moved; but they can be moved
317 separately with additional <literal>SET TABLESPACE</literal> commands.
319 <xref linkend="SQL-CREATETABLESPACE" endterm="sql-createtablespace-title">.
325 <term><literal>RENAME</literal></term>
328 The <literal>RENAME</literal> forms change the name of a table
329 (or an index, sequence, or view) or the name of an individual column in
330 a table. There is no effect on the stored data.
336 <term><literal>SET SCHEMA</literal></term>
339 This form moves the table into another schema. Associated indexes,
340 constraints, and SERIAL-column sequences are moved as well.
349 All the actions except <literal>RENAME</literal> and <literal>SET SCHEMA</>
351 a list of multiple alterations to apply in parallel. For example, it
352 is possible to add several columns and/or alter the type of several
353 columns in a single command. This is particularly useful with large
354 tables, since only one pass over the table need be made.
358 You must own the table to use <command>ALTER TABLE</>.
359 To change the schema of a table, you must also have
360 <literal>CREATE</literal> privilege on the new schema.
361 To alter the owner, you must also be a direct or indirect member of the new
362 owning role, and that role must have <literal>CREATE</literal> privilege on
363 the table's schema. (These restrictions enforce that altering the owner
364 doesn't do anything you couldn't do by dropping and recreating the table.
365 However, a superuser can alter ownership of any table anyway.)
370 <title>Parameters</title>
375 <term><replaceable class="PARAMETER">name</replaceable></term>
378 The name (possibly schema-qualified) of an existing table to
379 alter. If <literal>ONLY</> is specified, only that table is
380 altered. If <literal>ONLY</> is not specified, the table and all
381 its descendant tables (if any) are updated. <literal>*</> can be
382 appended to the table name to indicate that descendant tables are
383 to be altered, but in the current version, this is the default
384 behavior. (In releases before 7.1, <literal>ONLY</> was the
385 default behavior. The default can be altered by changing the
386 configuration parameter <xref linkend="guc-sql-inheritance">.)
392 <term><replaceable class="PARAMETER">column</replaceable></term>
395 Name of a new or existing column.
401 <term><replaceable class="PARAMETER">new_column</replaceable></term>
404 New name for an existing column.
410 <term><replaceable class="PARAMETER">new_name</replaceable></term>
413 New name for the table.
419 <term><replaceable class="PARAMETER">type</replaceable></term>
422 Data type of the new column, or new data type for an existing
429 <term><replaceable class="PARAMETER">table_constraint</replaceable></term>
432 New table constraint for the table.
438 <term><replaceable class="PARAMETER">constraint_name</replaceable></term>
441 Name of an existing constraint to drop.
447 <term><literal>CASCADE</literal></term>
450 Automatically drop objects that depend on the dropped column
451 or constraint (for example, views referencing the column).
457 <term><literal>RESTRICT</literal></term>
460 Refuse to drop the column or constraint if there are any dependent
461 objects. This is the default behavior.
467 <term><replaceable class="PARAMETER">trigger_name</replaceable></term>
470 Name of a single trigger to disable or enable.
476 <term><literal>ALL</literal></term>
479 Disable or enable all triggers belonging to the table.
480 (This requires superuser privilege if any of the triggers are for
481 foreign key constraints.)
487 <term><literal>USER</literal></term>
490 Disable or enable all triggers belonging to the table except for
491 foreign key constraint triggers.
497 <term><replaceable class="PARAMETER">index_name</replaceable></term>
500 The index name on which the table should be marked for clustering.
506 <term><replaceable class="PARAMETER">new_owner</replaceable></term>
509 The user name of the new owner of the table.
515 <term><replaceable class="PARAMETER">new_tablespace</replaceable></term>
518 The name of the tablespace to which the table will be moved.
524 <term><replaceable class="PARAMETER">new_schema</replaceable></term>
527 The name of the schema to which the table will be moved.
539 The key word <literal>COLUMN</literal> is noise and can be omitted.
543 When a column is added with <literal>ADD COLUMN</literal>, all existing
544 rows in the table are initialized with the column's default value
545 (NULL if no <literal>DEFAULT</> clause is specified).
549 Adding a column with a non-null default or changing the type of an
550 existing column will require the entire table to be rewritten. This
551 may take a significant amount of time for a large table; and it will
552 temporarily require double the disk space.
556 Adding a <literal>CHECK</> or <literal>NOT NULL</> constraint requires
557 scanning the table to verify that existing rows meet the constraint.
561 The main reason for providing the option to specify multiple changes
562 in a single <command>ALTER TABLE</> is that multiple table scans or
563 rewrites can thereby be combined into a single pass over the table.
567 The <literal>DROP COLUMN</literal> form does not physically remove
568 the column, but simply makes it invisible to SQL operations. Subsequent
569 insert and update operations in the table will store a null value for the
570 column. Thus, dropping a column is quick but it will not immediately
571 reduce the on-disk size of your table, as the space occupied
572 by the dropped column is not reclaimed. The space will be
573 reclaimed over time as existing rows are updated.
577 The fact that <literal>ALTER TYPE</> requires rewriting the whole table
578 is sometimes an advantage, because the rewriting process eliminates
579 any dead space in the table. For example, to reclaim the space occupied
580 by a dropped column immediately, the fastest way is
582 ALTER TABLE table ALTER COLUMN anycol TYPE anytype;
584 where <literal>anycol</> is any remaining table column and
585 <literal>anytype</> is the same type that column already has.
586 This results in no semantically-visible change in the table,
587 but the command forces rewriting, which gets rid of no-longer-useful
592 The <literal>USING</literal> option of <literal>ALTER TYPE</> can actually
593 specify any expression involving the old values of the row; that is, it
594 can refer to other columns as well as the one being converted. This allows
595 very general conversions to be done with the <literal>ALTER TYPE</>
596 syntax. Because of this flexibility, the <literal>USING</literal>
597 expression is not applied to the column's default value (if any); the
598 result might not be a constant expression as required for a default.
599 This means that when there is no implicit or assignment cast from old to
600 new type, <literal>ALTER TYPE</> may fail to convert the default even
601 though a <literal>USING</literal> clause is supplied. In such cases,
602 drop the default with <literal>DROP DEFAULT</>, perform the <literal>ALTER
603 TYPE</>, and then use <literal>SET DEFAULT</> to add a suitable new
604 default. Similar considerations apply to indexes and constraints involving
609 If a table has any descendant tables, it is not permitted to add,
610 rename, or change the type of a column in the parent table without doing
611 the same to the descendants. That is, <command>ALTER TABLE ONLY</command>
612 will be rejected. This ensures that the descendants always have
613 columns matching the parent.
617 A recursive <literal>DROP COLUMN</literal> operation will remove a
618 descendant table's column only if the descendant does not inherit
619 that column from any other parents and never had an independent
620 definition of the column. A nonrecursive <literal>DROP
621 COLUMN</literal> (i.e., <command>ALTER TABLE ONLY ... DROP
622 COLUMN</command>) never removes any descendant columns, but
623 instead marks them as independently defined rather than inherited.
627 The <literal>TRIGGER</>, <literal>CLUSTER</>, <literal>OWNER</>,
628 and <literal>TABLESPACE</> actions never recurse to descendant tables;
629 that is, they always act as though <literal>ONLY</> were specified.
630 Adding a constraint can recurse only for <literal>CHECK</> constraints.
634 Changing any part of a system catalog table is not permitted.
638 Refer to <xref linkend="sql-createtable"
639 endterm="sql-createtable-title"> for a further description of valid
640 parameters. <xref linkend="ddl"> has further information on
646 <title>Examples</title>
649 To add a column of type <type>varchar</type> to a table:
651 ALTER TABLE distributors ADD COLUMN address varchar(30);
656 To drop a column from a table:
658 ALTER TABLE distributors DROP COLUMN address RESTRICT;
663 To change the types of two existing columns in one operation:
665 ALTER TABLE distributors
666 ALTER COLUMN address TYPE varchar(80),
667 ALTER COLUMN name TYPE varchar(100);
672 To change an integer column containing UNIX timestamps to <type>timestamp
673 with time zone</type> via a <literal>USING</literal> clause:
676 ALTER COLUMN foo_timestamp TYPE timestamp with time zone
678 timestamp with time zone 'epoch' + foo_timestamp * interval '1 second';
683 To rename an existing column:
685 ALTER TABLE distributors RENAME COLUMN address TO city;
690 To rename an existing table:
692 ALTER TABLE distributors RENAME TO suppliers;
697 To add a not-null constraint to a column:
699 ALTER TABLE distributors ALTER COLUMN street SET NOT NULL;
701 To remove a not-null constraint from a column:
703 ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;
708 To add a check constraint to a table:
710 ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);
715 To remove a check constraint from a table and all its children:
717 ALTER TABLE distributors DROP CONSTRAINT zipchk;
722 To add a foreign key constraint to a table:
724 ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) MATCH FULL;
729 To add a (multicolumn) unique constraint to a table:
731 ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);
736 To add an automatically named primary key constraint to a table, noting
737 that a table can only ever have one primary key:
739 ALTER TABLE distributors ADD PRIMARY KEY (dist_id);
744 To move a table to a different tablespace:
746 ALTER TABLE distributors SET TABLESPACE fasttablespace;
751 To move a table to a different schema:
753 ALTER TABLE myschema.distributors SET SCHEMA yourschema;
760 <title>Compatibility</title>
763 The <literal>ADD</literal>, <literal>DROP</>, and <literal>SET DEFAULT</>
764 forms conform with the SQL standard. The other forms are
765 <productname>PostgreSQL</productname> extensions of the SQL standard.
766 Also, the ability to specify more than one manipulation in a single
767 <command>ALTER TABLE</> command is an extension.
771 <command>ALTER TABLE DROP COLUMN</> can be used to drop the only
772 column of a table, leaving a zero-column table. This is an
773 extension of SQL, which disallows zero-column tables.
778 <!-- Keep this comment at the end of the file
783 sgml-minimize-attributes:nil
784 sgml-always-quote-attributes:t
787 sgml-parent-document:nil
788 sgml-default-dtd-file:"../reference.ced"
789 sgml-exposed-tags:nil
790 sgml-local-catalogs:"/usr/lib/sgml/catalog"
791 sgml-local-ecat-files:nil