2 $Header: /cvsroot/pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.59 2003/04/15 13:25:08 petere 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>
19 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
20 ADD [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> <replaceable class="PARAMETER">type</replaceable> [ <replaceable class="PARAMETER">column_constraint</replaceable> [ ... ] ]
21 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
22 DROP [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> [ RESTRICT | CASCADE ]
23 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
24 ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> { SET DEFAULT <replaceable class="PARAMETER">value</replaceable> | DROP DEFAULT }
25 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
26 ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> { SET | DROP } NOT NULL
27 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
28 ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET STATISTICS <replaceable class="PARAMETER">integer</replaceable>
29 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
30 ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
31 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
33 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
34 RENAME [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> TO <replaceable
35 class="PARAMETER">new_column</replaceable>
36 ALTER TABLE <replaceable class="PARAMETER">table</replaceable>
37 RENAME TO <replaceable class="PARAMETER">new_table</replaceable>
38 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
39 ADD <replaceable class="PARAMETER">table_constraint</replaceable>
40 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
41 DROP CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> [ RESTRICT | CASCADE ]
42 ALTER TABLE <replaceable class="PARAMETER">table</replaceable>
43 OWNER TO <replaceable class="PARAMETER">new_owner</replaceable>
44 ALTER TABLE <replaceable class="PARAMETER">table</replaceable>
45 CLUSTER ON <replaceable class="PARAMETER">index_name</replaceable>
50 <title>Description</title>
53 <command>ALTER TABLE</command> changes the definition of an existing table.
54 There are several subforms:
58 <term><literal>ADD COLUMN</literal></term>
61 This form adds a new column to the table using the same syntax as
62 <xref linkend="SQL-CREATETABLE" endterm="SQL-CREATETABLE-TITLE">.
68 <term><literal>DROP COLUMN</literal></term>
71 This form drops a column from a table. Indexes and
72 table constraints involving the column will be automatically
73 dropped as well. You will need to say <literal>CASCADE</> if
74 anything outside the table depends on the column, for example,
75 foreign key references or views.
81 <term><literal>SET</literal>/<literal>DROP DEFAULT</literal></term>
84 These forms set or remove the default value for a column.
85 The default values only apply to subsequent <command>INSERT</command>
86 commands; they do not cause rows already in the table to change.
87 Defaults may also be created for views, in which case they are
88 inserted into <command>INSERT</> statements on the view before
89 the view's <literal>ON INSERT</literal> rule is applied.
95 <term><literal>SET</literal>/<literal>DROP NOT NULL</literal></term>
98 These forms change whether a column is marked to allow null
99 values or to reject null values. You can only use <literal>SET
100 NOT NULL</> when the column contains no null values.
106 <term><literal>SET STATISTICS</literal></term>
110 sets the per-column statistics-gathering target for subsequent
111 <xref linkend="sql-analyze" endterm="sql-analyze-title"> operations.
112 The target can be set in the range 0 to 1000; alternatively, set it
113 to -1 to revert to using the system default statistics target.
119 <term><literal>SET STORAGE</literal></term>
122 This form sets the storage mode for a column. This controls whether this
123 column is held inline or in a supplementary table, and whether the data
124 should be compressed or not. <literal>PLAIN</literal> must be used
125 for fixed-length values such as <type>integer</type> and is
126 inline, uncompressed. <literal>MAIN</literal> is for inline,
127 compressible data. <literal>EXTERNAL</literal> is for external,
128 uncompressed data, and <literal>EXTENDED</literal> is for external,
129 compressed data. <literal>EXTENDED</literal> is the default for all
130 data types that support it. The use of <literal>EXTERNAL</literal> will, for example,
131 make substring operations on a <type>text</type> column faster, at the penalty of
132 increased storage space.
138 <term><literal>SET WITHOUT OIDS</literal></term>
141 This form removes the <literal>oid</literal> column from the
142 table. Removing OIDs from a table does not occur immediately.
143 The space that the OID uses will be reclaimed when the row is
144 updated. Without updating the row, both the space and the value
145 of the OID are kept indefinitely. This is semantically similar
146 to the <literal>DROP COLUMN</literal> process.
152 <term><literal>RENAME</literal></term>
155 The <literal>RENAME</literal> forms change the name of a table
156 (or an index, sequence, or view) or the name of an individual column in
157 a table. There is no effect on the stored data.
163 <term><literal>ADD <replaceable class="PARAMETER">table_constraint</replaceable></literal></term>
166 This form adds a new constraint to a table using the same syntax as
167 <xref linkend="SQL-CREATETABLE" endterm="SQL-CREATETABLE-TITLE">.
173 <term><literal>DROP CONSTRAINT</literal></term>
176 This form drops constraints on a table.
177 Currently, constraints on tables are not required to have unique
178 names, so there may be more than one constraint matching the specified
179 name. All such constraints will be dropped.
185 <term><literal>OWNER</literal></term>
188 This form changes the owner of the table, index, sequence, or view to the
195 <term><literal>CLUSTER</literal></term>
198 This form marks a table for future <xref linkend="SQL-CLUSTER" endterm="sql-cluster-title">
208 You must own the table to use <command>ALTER TABLE</>; except for
209 <command>ALTER TABLE OWNER</>, which may only be executed by a superuser.
214 <title>Parameters</title>
219 <term><replaceable class="PARAMETER">table</replaceable></term>
222 The name (possibly schema-qualified) of an existing table to
223 alter. If <literal>ONLY</> is specified, only that table is
224 altered. If <literal>ONLY</> is not specified, the table and all
225 its descendant tables (if any) are updated. <literal>*</> can be
226 appended to the table name to indicate that descendant tables are
227 to be altered, but in the current version, this is the default
228 behavior. (In releases before 7.1, <literal>ONLY</> was the
229 default behavior. The default can be altered by changing the
230 configuration parameter <varname>sql_inheritance</varname>.)
236 <term><replaceable class="PARAMETER">column</replaceable></term>
239 Name of a new or existing column.
245 <term><replaceable class="PARAMETER">type</replaceable></term>
248 Data type of the new column.
254 <term><replaceable class="PARAMETER">new_column</replaceable></term>
257 New name for an existing column.
263 <term><replaceable class="PARAMETER">new_table</replaceable></term>
266 New name for the table.
272 <term><replaceable class="PARAMETER">table_constraint</replaceable></term>
275 New table constraint for the table.
281 <term><replaceable class="PARAMETER">constraint_name</replaceable></term>
284 Name of an existing constraint to drop.
290 <term><replaceable class="PARAMETER">new_owner</replaceable></term>
293 The user name of the new owner of the table.
299 <term><replaceable class="PARAMETER">index_name</replaceable></term>
302 The index name on which the table should be marked for clustering.
308 <term><literal>CASCADE</literal></term>
311 Automatically drop objects that depend on the dropped column
312 or constraint (for example, views referencing the column).
318 <term><literal>RESTRICT</literal></term>
321 Refuse to drop the column or constraint if there are any dependent
322 objects. This is the default behavior.
331 <title>Diagnostics</title>
335 <term><computeroutput>ALTER TABLE</computeroutput></term>
338 Message returned if successful.
344 <term><computeroutput>ERROR</computeroutput></term>
347 Message returned if table or column does not exist.
358 The key word <literal>COLUMN</literal> is noise and can be omitted.
362 In the current implementation of <literal>ADD COLUMN</literal>,
363 default and <literal>NOT NULL</> clauses for the new column are not supported.
364 The new column always comes into being with all values null.
365 You can use the <literal>SET DEFAULT</literal> form
366 of <command>ALTER TABLE</command> to set the default afterward.
367 (You may also want to update the already existing rows to the
368 new default value, using
369 <xref linkend="sql-update" endterm="sql-update-title">.)
370 If you want to mark the column non-null, use the <literal>SET NOT NULL</>
371 form after you've entered non-null values for the column in all rows.
375 The <literal>DROP COLUMN</literal> form does not physically remove
376 the column, but simply makes it invisible to SQL operations. Subsequent
377 insert and update operations in the table will store a null value for the column.
378 Thus, dropping a column is quick but it will not immediately reduce the
379 on-disk size of your table, as the space occupied
380 by the dropped column is not reclaimed. The space will be
381 reclaimed over time as existing rows are updated.
382 To reclaim the space at once, do a dummy <command>UPDATE</> of all rows
383 and then vacuum, as in:
385 UPDATE table SET col = col;
391 If a table has any descendant tables, it is not permitted to add
392 or rename a column in the parent table without doing the same to
393 the descendants. That is, <command>ALTER TABLE ONLY</command>
394 will be rejected. This ensures that the descendants always have
395 columns matching the parent.
399 A recursive <literal>DROP COLUMN</literal> operation will remove a
400 descendant table's column only if the descendant does not inherit
401 that column from any other parents and never had an independent
402 definition of the column. A nonrecursive <literal>DROP
403 COLUMN</literal> (i.e., <command>ALTER TABLE ONLY ... DROP
404 COLUMN</command>) never removes any descendant columns, but
405 instead marks them as independently defined rather than inherited.
409 Changing any part of a system catalog table is not permitted.
413 Refer to <command>CREATE TABLE</command> for a further description
414 of valid parameters. <xref linkend="ddl"> has further information on
420 <title>Examples</title>
423 To add a column of type <type>varchar</type> to a table:
425 ALTER TABLE distributors ADD COLUMN address varchar(30);
430 To drop a column from a table:
432 ALTER TABLE distributors DROP COLUMN address RESTRICT;
437 To rename an existing column:
439 ALTER TABLE distributors RENAME COLUMN address TO city;
444 To rename an existing table:
446 ALTER TABLE distributors RENAME TO suppliers;
451 To add a not-null constraint to a column:
453 ALTER TABLE distributors ALTER COLUMN street SET NOT NULL;
455 To remove a not-null constraint from a column:
457 ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;
462 To add a check constraint to a table:
464 ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);
469 To remove a check constraint from a table and all its children:
471 ALTER TABLE distributors DROP CONSTRAINT zipchk;
476 To add a foreign key constraint to a table:
478 ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) MATCH FULL;
483 To add a (multicolumn) unique constraint to a table:
485 ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);
490 To add an automatically named primary key constraint to a table, noting
491 that a table can only ever have one primary key:
493 ALTER TABLE distributors ADD PRIMARY KEY (dist_id);
499 <title>Compatibility</title>
502 The <literal>ADD COLUMN</literal> form conforms with the SQL
503 standard, with the exception that it does not support defaults and
504 not-null constraints, as explained above. The <literal>ALTER
505 COLUMN</literal> form is in full conformance.
509 The clauses to rename tables, columns, indexes, views, and sequences are
510 <productname>PostgreSQL</productname> extensions of the SQL standard.
514 <command>ALTER TABLE DROP COLUMN</> can be used to drop the only
515 column of a table, leaving a zero-column table. This is an
516 extension of SQL, which disallows zero-column tables.
521 <!-- Keep this comment at the end of the file
526 sgml-minimize-attributes:nil
527 sgml-always-quote-attributes:t
530 sgml-parent-document:nil
531 sgml-default-dtd-file:"../reference.ced"
532 sgml-exposed-tags:nil
533 sgml-local-catalogs:"/usr/lib/sgml/catalog"
534 sgml-local-ecat-files:nil