2 $Header: /cvsroot/pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.55 2003/02/13 05:19:59 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>
16 change the definition of a table
21 <date>1999-07-20</date>
24 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
25 ADD [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> <replaceable class="PARAMETER">type</replaceable> [ <replaceable class="PARAMETER">column_constraint</replaceable> [ ... ] ]
26 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
27 DROP [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> [ RESTRICT | CASCADE ]
28 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
29 ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> { SET DEFAULT <replaceable class="PARAMETER">value</replaceable> | DROP DEFAULT }
30 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
31 ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> { SET | DROP } NOT NULL
32 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
33 ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET STATISTICS <replaceable class="PARAMETER">integer</replaceable>
34 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
35 ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
36 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
38 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
39 RENAME [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> TO <replaceable
40 class="PARAMETER">new_column</replaceable>
41 ALTER TABLE <replaceable class="PARAMETER">table</replaceable>
42 RENAME TO <replaceable class="PARAMETER">new_table</replaceable>
43 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
44 ADD <replaceable class="PARAMETER">table_constraint</replaceable>
45 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
46 DROP CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> [ RESTRICT | CASCADE ]
47 ALTER TABLE <replaceable class="PARAMETER">table</replaceable>
48 OWNER TO <replaceable class="PARAMETER">new_owner</replaceable>
51 <refsect2 id="R2-SQL-ALTERTABLE-1">
53 <date>1998-04-15</date>
62 <term><replaceable class="PARAMETER"> table </replaceable></term>
65 The name (possibly schema-qualified) of an existing table to
66 alter. If <literal>ONLY</> is specified, only that table is
67 altered. If <literal>ONLY</> is not specified, the table and all
68 its descendant tables (if any) are updated. <literal>*</> can be
69 appended to the table name to indicate that descendant tables are
70 to be scanned, but in the current version, this is the default
71 behavior. (In releases before 7.1, <literal>ONLY</> was the
72 default behavior.) The default can be altered by changing the
73 <option>SQL_INHERITANCE</option> configuration option.
79 <term><replaceable class="PARAMETER"> column </replaceable></term>
82 Name of a new or existing column.
88 <term><replaceable class="PARAMETER"> type </replaceable></term>
91 Type of the new column.
97 <term><replaceable class="PARAMETER"> new_column </replaceable></term>
100 New name for an existing column.
106 <term><replaceable class="PARAMETER"> new_table </replaceable></term>
109 New name for the table.
115 <term><replaceable class="PARAMETER"> table_constraint </replaceable></term>
118 New table constraint for the table.
124 <term><replaceable class="PARAMETER"> constraint_name </replaceable></term>
127 Name of an existing constraint to drop.
133 <term><replaceable class="PARAMETER">new_owner </replaceable></term>
136 The user name of the new owner of the table.
145 Automatically drop objects that depend on the dropped column
146 or constraint (for example, views referencing the column).
152 <term>RESTRICT</term>
155 Refuse to drop the column or constraint if there are any dependent
156 objects. This is the default behavior.
165 <refsect2 id="R2-SQL-ALTERTABLE-2">
167 <date>1998-04-15</date>
176 <term><computeroutput>ALTER TABLE</computeroutput></term>
179 Message returned from column or table renaming.
185 <term><computeroutput>ERROR</computeroutput></term>
188 Message returned if table or column is not available.
197 <refsect1 id="R1-SQL-ALTERTABLE-1">
199 <date>1998-04-15</date>
205 <command>ALTER TABLE</command> changes the definition of an existing table.
206 There are several sub-forms:
212 <term>ADD COLUMN</term>
215 This form adds a new column to the table using the same syntax as
216 <xref linkend="SQL-CREATETABLE" endterm="SQL-CREATETABLE-TITLE">.
222 <term>DROP COLUMN</term>
225 This form drops a column from a table. Note that indexes and
226 table constraints involving the column will be automatically
227 dropped as well. You will need to say <literal>CASCADE</> if
228 anything outside the table depends on the column --- for example,
229 foreign key references, views, etc.
235 <term>SET/DROP DEFAULT</term>
238 These forms set or remove the default value for a column. Note
239 that defaults only apply to subsequent <command>INSERT</command>
240 commands; they do not cause rows already in the table to change.
241 Defaults may also be created for views, in which case they are
242 inserted into <command>INSERT</> statements on the view before
243 the view's ON INSERT rule is applied.
249 <term>SET/DROP NOT NULL</term>
252 These forms change whether a column is marked to allow NULL
253 values or to reject NULL values. You may only <literal>SET NOT NULL</>
254 when the table contains no null values in the column.
260 <term>SET STATISTICS</term>
264 sets the per-column statistics-gathering target for subsequent
265 <xref linkend="sql-analyze" endterm="sql-analyze-title"> operations.
266 The target can be set in the range 0 to 1000; alternatively, set it
267 to -1 to revert to using the system default statistics target.
273 <term>SET STORAGE</term>
276 This form sets the storage mode for a column. This controls whether this
277 column is held inline or in a supplementary table, and whether the data
278 should be compressed or not. <literal>PLAIN</literal> must be used
279 for fixed-length values such as <literal>INTEGER</literal> and is
280 inline, uncompressed. <literal>MAIN</literal> is for inline,
281 compressible data. <literal>EXTERNAL</literal> is for external,
282 uncompressed data and <literal>EXTENDED</literal> is for external,
283 compressed data. <literal>EXTENDED</literal> is the default for all
284 data types that support it. The use of <literal>EXTERNAL</literal> will
285 make substring operations on a TEXT column faster, at the penalty of
286 increased storage space.
292 <term>SET WITHOUT OIDS</term>
295 Removes the <literal>OID</literal> column from the the table. Removing (setting without)
296 oids from a table also do not occur immediately. The space an <literal>OID</literal>
297 uses will be reclaimed when the tuple is updated. Without updating the tuple, both the
298 space and the value of the <literal>OID</literal> are maintained indefinitely. This is
299 semantically similar to the <literal>DROP COLUMN</literal> process.
308 The <literal>RENAME</literal> forms change the name of a table
309 (or an index, sequence, or view) or the name of an individual column in
310 a table. There is no effect on the stored data.
316 <term>ADD <replaceable class="PARAMETER">table_constraint</replaceable></term>
319 This form adds a new constraint to a table using the same syntax as
320 <xref linkend="SQL-CREATETABLE" endterm="SQL-CREATETABLE-TITLE">.
326 <term>DROP CONSTRAINT</term>
329 This form drops constraints on a table.
330 Currently, constraints on tables are not required to have unique
331 names, so there may be more than one constraint matching the specified
332 name. All such constraints will be dropped.
341 This form changes the owner of the table, index, sequence or view to the
350 You must own the table to use <command>ALTER TABLE</>; except for
351 <command>ALTER TABLE OWNER</>, which may only be executed by a superuser.
354 <refsect2 id="R2-SQL-ALTERTABLE-3">
356 <date>1998-04-15</date>
362 The keyword <literal>COLUMN</literal> is noise and can be omitted.
366 In the current implementation of <literal>ADD COLUMN</literal>,
367 default and NOT NULL clauses for the new column are not supported.
368 The new column always comes into being with all values NULL.
369 You can use the <literal>SET DEFAULT</literal> form
370 of <command>ALTER TABLE</command> to set the default afterwards.
371 (You may also want to update the already existing rows to the
372 new default value, using
373 <xref linkend="sql-update" endterm="sql-update-title">.)
374 If you want to mark the column non-null, use the <literal>SET NOT NULL</>
375 form after you've entered non-null values for the column in all rows.
379 The <literal>DROP COLUMN</literal> command does not physically remove
380 the column, but simply makes it invisible to SQL operations. Subsequent
381 inserts and updates of the table will store a NULL for the column.
382 Thus, dropping a column is quick but it will not immediately reduce the
383 on-disk size of your table, as the space occupied
384 by the dropped column is not reclaimed. The space will be
385 reclaimed over time as existing rows are updated.
386 To reclaim the space at once, do a dummy <command>UPDATE</> of all rows
387 and then vacuum, as in:
389 UPDATE table SET col = col;
395 If a table has any descendant tables, it is not permitted to ADD or
396 RENAME a column in the parent table without doing the same to the
397 descendants --- that is, ALTER TABLE ONLY will be rejected. This
398 ensures that the descendants always have columns matching the parent.
402 A recursive DROP COLUMN operation will remove a descendant table's column
403 only if the descendant does not inherit that column from any other
404 parents and never had an independent definition of the column.
405 A nonrecursive DROP COLUMN (i.e., ALTER TABLE ONLY ... DROP COLUMN)
406 never removes any descendant columns, but instead marks them as
407 independently defined rather than inherited.
411 Changing any part of the schema of a system
412 catalog is not permitted.
416 Refer to <command>CREATE TABLE</command> for a further description
417 of valid arguments. The &cite-user; has further information on
423 <refsect1 id="R1-SQL-ALTERTABLE-2">
428 To add a column of type <type>varchar</type> to a table:
430 ALTER TABLE distributors ADD COLUMN address VARCHAR(30);
435 To drop a column from a table:
437 ALTER TABLE distributors DROP COLUMN address RESTRICT;
442 To rename an existing column:
444 ALTER TABLE distributors RENAME COLUMN address TO city;
449 To rename an existing table:
451 ALTER TABLE distributors RENAME TO suppliers;
456 To add a NOT NULL constraint to a column:
458 ALTER TABLE distributors ALTER COLUMN street SET NOT NULL;
460 To remove a NOT NULL constraint from a column:
462 ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;
467 To add a check constraint to a table:
469 ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);
474 To remove a check constraint from a table and all its children:
476 ALTER TABLE distributors DROP CONSTRAINT zipchk;
481 To add a foreign key constraint to a table:
483 ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses(address) MATCH FULL;
488 To add a (multicolumn) unique constraint to a table:
490 ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);
495 To add an automatically named primary key constraint to a table, noting
496 that a table can only ever have one primary key:
498 ALTER TABLE distributors ADD PRIMARY KEY (dist_id);
503 <refsect1 id="R1-SQL-ALTERTABLE-3">
508 <refsect2 id="R2-SQL-ALTERTABLE-4">
510 <date>1998-04-15</date>
514 The <literal>ADD COLUMN</literal> form is compliant with the exception that
515 it does not support defaults and NOT NULL constraints, as explained above.
516 The <literal>ALTER COLUMN</literal> form is in full compliance.
520 The clauses to rename tables, columns, indexes, and sequences are
521 <productname>PostgreSQL</productname> extensions from SQL92.
525 <command>ALTER TABLE DROP COLUMN</> can be used to drop the only column
526 of a table, leaving a zero-column table. This is an extension from SQL92,
527 which disallows zero-column tables.
534 <!-- Keep this comment at the end of the file
539 sgml-minimize-attributes:nil
540 sgml-always-quote-attributes:t
543 sgml-parent-document:nil
544 sgml-default-dtd-file:"../reference.ced"
545 sgml-exposed-tags:nil
546 sgml-local-catalogs:"/usr/lib/sgml/catalog"
547 sgml-local-ecat-files:nil