2 $Header: /cvsroot/pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.45 2002/05/18 15:44:47 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>
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 ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> { SET DEFAULT <replaceable class="PARAMETER">value</replaceable> | DROP DEFAULT }
28 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
29 ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> { SET | DROP } NOT NULL
30 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
31 ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET STATISTICS <replaceable class="PARAMETER">integer</replaceable>
32 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
33 ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
34 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
35 RENAME [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> TO <replaceable
36 class="PARAMETER">new_column</replaceable>
37 ALTER TABLE <replaceable class="PARAMETER">table</replaceable>
38 RENAME TO <replaceable class="PARAMETER">new_table</replaceable>
39 ALTER TABLE <replaceable class="PARAMETER">table</replaceable>
40 ADD <replaceable class="PARAMETER">table_constraint_definition</replaceable>
41 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable>
42 DROP CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> { RESTRICT | CASCADE }
43 ALTER TABLE <replaceable class="PARAMETER">table</replaceable>
44 OWNER TO <replaceable class="PARAMETER">new_owner</replaceable>
47 <refsect2 id="R2-SQL-ALTERTABLE-1">
49 <date>1998-04-15</date>
58 <term><replaceable class="PARAMETER"> table </replaceable></term>
61 The name (possibly schema-qualified) of an existing table to alter.
67 <term><replaceable class="PARAMETER"> column </replaceable></term>
70 Name of a new or existing column.
76 <term><replaceable class="PARAMETER"> type </replaceable></term>
79 Type of the new column.
85 <term><replaceable class="PARAMETER"> new_column </replaceable></term>
88 New name for an existing column.
94 <term><replaceable class="PARAMETER"> new_table </replaceable></term>
97 New name for the table.
103 <term><replaceable class="PARAMETER"> table_constraint_definition </replaceable></term>
106 New table constraint for the table.
112 <term><replaceable class="PARAMETER"> constraint_name </replaceable></term>
115 Name of an existing constraint to drop.
121 <term><replaceable class="PARAMETER">new_owner </replaceable></term>
124 The user name of the new owner of the table.
133 <refsect2 id="R2-SQL-ALTERTABLE-2">
135 <date>1998-04-15</date>
144 <term><computeroutput>ALTER TABLE</computeroutput></term>
147 Message returned from column or table renaming.
153 <term><computeroutput>ERROR</computeroutput></term>
156 Message returned if table or column is not available.
165 <refsect1 id="R1-SQL-ALTERTABLE-1">
167 <date>1998-04-15</date>
173 <command>ALTER TABLE</command> changes the definition of an existing table.
174 There are several sub-forms:
180 <term>ADD COLUMN</term>
183 This form adds a new column to the table using the same syntax as
184 <xref linkend="SQL-CREATETABLE" endterm="SQL-CREATETABLE-TITLE">.
190 <term>SET/DROP DEFAULT</term>
193 These forms set or remove the default value for a column. Note
194 that defaults only apply to subsequent <command>INSERT</command>
195 commands; they do not cause rows already in the table to change.
196 Defaults may also be created for views, in which case they are
197 inserted into <command>INSERT</> statements on the view before
198 the view's ON INSERT rule is applied.
204 <term>SET/DROP NOT NULL</term>
207 These forms change whether a column is marked to allow NULL
208 values or to reject NULL values. You may only <literal>SET NOT NULL</>
209 when the table contains no NULLs in the column.
215 <term>SET STATISTICS</term>
219 sets the per-column statistics-gathering target for subsequent
220 <xref linkend="sql-analyze" endterm="sql-analyze-title"> operations.
226 <term>SET STORAGE</term>
229 This form sets the storage mode for a column. This controls whether this
230 column is held inline or in a supplementary table, and whether the data
231 should be compressed or not. <literal>PLAIN</literal> must be used
232 for fixed-length values such as <literal>INTEGER</literal> and is
233 inline, uncompressed. <literal>MAIN</literal> is for inline,
234 compressible data. <literal>EXTERNAL</literal> is for external,
235 uncompressed data and <literal>EXTENDED</literal> is for external,
236 compressed data. <literal>EXTENDED</literal> is the default for all
237 datatypes that support it. The use of <literal>EXTERNAL</literal> will
238 make substring operations on a TEXT column faster, at the penalty of
239 increased storage space.
248 The <literal>RENAME</literal> forms change the name of a table
249 (or an index, sequence, or view) or the name of an individual column in
250 a table. There is no effect on the stored data.
256 <term>ADD <replaceable class="PARAMETER">table_constraint_definition</replaceable></term>
259 This form adds a new constraint to a table using the same syntax as
260 <xref linkend="SQL-CREATETABLE" endterm="SQL-CREATETABLE-TITLE">.
266 <term>DROP CONSTRAINT</term>
269 This form drops constraints on a table (and its children).
270 Currently, constraints on tables are not required to have unique
271 names, so there may be more than one constraint matching the specified
272 name. All such constraints will be dropped.
281 This form changes the owner of the table, index, sequence or view to the
290 You must own the table to use <command>ALTER TABLE</>; except for
291 <command>ALTER TABLE OWNER</>, which may only be executed by a superuser.
294 <refsect2 id="R2-SQL-ALTERTABLE-3">
296 <date>1998-04-15</date>
302 The keyword <literal>COLUMN</literal> is noise and can be omitted.
306 In the current implementation of <literal>ADD COLUMN</literal>,
307 default and NOT NULL clauses for the new column are not supported.
308 The new column always comes into being with all values NULL.
309 You can use the <literal>SET DEFAULT</literal> form
310 of <command>ALTER TABLE</command> to set the default afterwards.
311 (You may also want to update the already existing rows to the
312 new default value, using
313 <xref linkend="sql-update" endterm="sql-update-title">.)
314 If you want to mark the column non-null, use the <literal>SET NOT NULL</>
315 form after you've entered non-null values for the column in all rows.
319 In DROP CONSTRAINT, the RESTRICT keyword is required, although
320 dependencies are not yet checked. The CASCADE option is unsupported.
321 Currently DROP CONSTRAINT only handles CHECK constraints.
322 To remove a PRIMARY or UNIQUE constraint, drop the
323 relevant index using the <xref linkend="SQL-DROPINDEX" endterm="sql-dropindex-title"> command.
324 To remove FOREIGN KEY constraints you need to recreate
325 and reload the table, using other parameters to the
326 <xref linkend="SQL-CREATETABLE" endterm="sql-createtable-title"> command.
329 For example, to drop all constraints on a table <literal>distributors</literal>:
331 CREATE TABLE temp AS SELECT * FROM distributors;
332 DROP TABLE distributors;
333 CREATE TABLE distributors AS SELECT * FROM temp;
339 Changing any part of the schema of a system
340 catalog is not permitted.
344 Refer to <command>CREATE TABLE</command> for a further description
346 The <citetitle>PostgreSQL User's Guide</citetitle> has further
347 information on inheritance.
352 <refsect1 id="R1-SQL-ALTERTABLE-2">
357 To add a column of type <type>varchar</type> to a table:
359 ALTER TABLE distributors ADD COLUMN address VARCHAR(30);
364 To rename an existing column:
366 ALTER TABLE distributors RENAME COLUMN address TO city;
371 To rename an existing table:
373 ALTER TABLE distributors RENAME TO suppliers;
378 To add a NOT NULL constraint to a column:
380 ALTER TABLE distributors ALTER COLUMN street SET NOT NULL;
382 To remove a NOT NULL constraint from a column:
384 ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;
389 To add a check constraint to a table:
391 ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);
396 To remove a check constraint from a table and all its children:
398 ALTER TABLE distributors DROP CONSTRAINT zipchk RESTRICT;
403 To add a foreign key constraint to a table:
405 ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses(address) MATCH FULL;
410 To add a (multicolumn) unique constraint to a table:
412 ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);
417 To add an automatically named primary key constraint to a table, noting
418 that a table can only ever have one primary key:
420 ALTER TABLE distributors ADD PRIMARY KEY (dist_id);
425 <refsect1 id="R1-SQL-ALTERTABLE-3">
430 <refsect2 id="R2-SQL-ALTERTABLE-4">
432 <date>1998-04-15</date>
436 The <literal>ADD COLUMN</literal> form is compliant with the exception that
437 it does not support defaults and NOT NULL constraints, as explained above.
438 The <literal>ALTER COLUMN</literal> form is in full compliance.
442 SQL92 specifies some additional capabilities for <command>ALTER TABLE</command>
443 statement which are not yet directly supported by <productname>PostgreSQL</productname>:
449 ALTER TABLE <replaceable class="PARAMETER">table</replaceable> DROP [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> { RESTRICT | CASCADE }
454 Removes a column from a table.
455 Currently, to remove an existing column the table must be
456 recreated and reloaded:
458 CREATE TABLE temp AS SELECT did, city FROM distributors;
459 DROP TABLE distributors;
460 CREATE TABLE distributors (
461 did DECIMAL(3) DEFAULT 1,
462 name VARCHAR(40) NOT NULL
464 INSERT INTO distributors SELECT * FROM temp;
474 The clauses to rename tables, columns, indexes, and sequences are
475 <productname>PostgreSQL</productname> extensions from SQL92.
482 <!-- Keep this comment at the end of the file
487 sgml-minimize-attributes:nil
488 sgml-always-quote-attributes:t
491 sgml-parent-document:nil
492 sgml-default-dtd-file:"../reference.ced"
493 sgml-exposed-tags:nil
494 sgml-local-catalogs:"/usr/lib/sgml/catalog"
495 sgml-local-ecat-files:nil