2 $Header: /cvsroot/pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.42 2002/04/21 19:02:39 thomas 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
28 class="PARAMETER">value</replaceable> | DROP DEFAULT }
29 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
30 ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> { SET | DROP } NOT NULL
31 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
32 ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET STATISTICS <replaceable class="PARAMETER">integer</replaceable>
33 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
34 ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET STORAGE {PLAIN | EXTERNAL | EXTENDED | MAIN}
35 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
36 RENAME [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> TO <replaceable
37 class="PARAMETER">newcolumn</replaceable>
38 ALTER TABLE <replaceable class="PARAMETER">table</replaceable>
39 RENAME TO <replaceable class="PARAMETER">new_table</replaceable>
40 ALTER TABLE <replaceable class="PARAMETER">table</replaceable>
41 ADD <replaceable class="PARAMETER">table_constraint_definition</replaceable>
42 ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable>
43 DROP CONSTRAINT <replaceable class="PARAMETER">constraint</replaceable> { RESTRICT | CASCADE }
44 ALTER TABLE <replaceable class="PARAMETER">table</replaceable>
45 OWNER TO <replaceable class="PARAMETER">new_owner</replaceable>
48 <refsect2 id="R2-SQL-ALTERTABLE-1">
50 <date>1998-04-15</date>
58 <term><replaceable class="PARAMETER"> table </replaceable></term>
61 The name 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"> newcolumn </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">new_owner </replaceable></term>
115 The user name of the new owner of the table.
124 <refsect2 id="R2-SQL-ALTERTABLE-2">
126 <date>1998-04-15</date>
135 <term><computeroutput>ALTER</computeroutput></term>
138 Message returned from column or table renaming.
144 <term><computeroutput>ERROR</computeroutput></term>
147 Message returned if table or column is not available.
156 <refsect1 id="R1-SQL-ALTERTABLE-1">
158 <date>1998-04-15</date>
164 <command>ALTER TABLE</command> changes the definition of an existing table.
165 The <literal>ADD COLUMN</literal> form adds a new column to the table
166 using the same syntax as <xref linkend="SQL-CREATETABLE" endterm="SQL-CREATETABLE-TITLE">.
167 The <literal>ALTER COLUMN SET/DROP DEFAULT</literal> forms
168 allow you to set or remove the default for the column. Note that defaults
169 only apply to subsequent <command>INSERT</command> commands; they do not
170 cause rows already in the table to change.
171 The <literal>ALTER COLUMN SET/DROP NOT NULL</literal> forms allow you to
172 change whether a column is marked to allow NULL values or to reject NULL
174 The <literal>ALTER COLUMN SET STATISTICS</literal> form allows you to
175 set the statistics-gathering target for subsequent
176 <xref linkend="sql-analyze" endterm="sql-analyze-title"> operations.
177 The <literal>ALTER COLUMN SET STORAGE</literal> form allows the
178 column storage mode to be set. This controls whether this column is
179 held inline or in a supplementary table, and whether the data
180 should be compressed or not. <literal>PLAIN</literal> must be used
181 for fixed-length values such as <literal>INTEGER</literal> and is
182 inline, uncompressed. <literal>MAIN</literal> is for inline,
183 compressible data. <literal>EXTERNAL</literal> is for external,
184 uncompressed data and <literal>EXTENDED</literal> is for external,
185 compressed data. The use of <literal>EXTERNAL</literal> will make
186 substring operations on a column faster, at the penalty of
187 increased storage space.
188 The <literal>RENAME</literal> clause causes the name of a table,
189 column, index, sequence or view to change without changing any of the
190 data. The data will remain of the same type and size after the
192 The ADD <replaceable class="PARAMETER">table_constraint_definition</replaceable> clause
193 adds a new constraint to the table using the same syntax as <xref
194 linkend="SQL-CREATETABLE" endterm="SQL-CREATETABLE-TITLE">.
195 The DROP CONSTRAINT <replaceable class="PARAMETER">constraint</replaceable> clause
196 drops all constraints on the table (and its children) that match <replaceable class="PARAMETER">constraint</replaceable>.
197 The OWNER clause changes the owner of the table, index, sequence or view to the
198 user <replaceable class="PARAMETER">new user</replaceable>.
202 You must own the table in order to change its schema.
205 <refsect2 id="R2-SQL-ALTERTABLE-3">
207 <date>1998-04-15</date>
213 The keyword <literal>COLUMN</literal> is noise and can be omitted.
217 In the current implementation of <literal>ADD COLUMN</literal>,
218 default and NOT NULL clauses for the new column are not supported.
219 You can use the <literal>SET DEFAULT</literal> form
220 of <command>ALTER TABLE</command> to set the default later.
221 (You may also want to update the already existing rows to the
222 new default value, using <xref linkend="sql-update" endterm="sql-update-title">.)
226 In DROP CONSTRAINT, the RESTRICT keyword is required, although
227 dependencies are not yet checked. The CASCADE option is unsupported.
228 Currently DROP CONSTRAINT drops only CHECK constraints.
229 To remove a PRIMARY or UNIQUE constraint, drop the
230 relevant index using the <xref linkend="SQL-DROPINDEX" endterm="sql-dropindex-title"> command.
231 To remove FOREIGN KEY constraints you need to recreate
232 and reload the table, using other parameters to the
233 <xref linkend="SQL-CREATETABLE" endterm="sql-createtable-title"> command.
236 For example, to drop all constraints on a table <literal>distributors</literal>:
238 CREATE TABLE temp AS SELECT * FROM distributors;
239 DROP TABLE distributors;
240 CREATE TABLE distributors AS SELECT * FROM temp;
246 You must own the table in order to change it.
247 Changing any part of the schema of a system
248 catalog is not permitted.
249 The <citetitle>PostgreSQL User's Guide</citetitle> has further
250 information on inheritance.
254 Refer to <command>CREATE TABLE</command> for a further description
260 <refsect1 id="R1-SQL-ALTERTABLE-2">
265 To add a column of type <type>varchar</type> to a table:
267 ALTER TABLE distributors ADD COLUMN address VARCHAR(30);
272 To rename an existing column:
274 ALTER TABLE distributors RENAME COLUMN address TO city;
279 To rename an existing table:
281 ALTER TABLE distributors RENAME TO suppliers;
286 To add a NOT NULL constraint to a column:
288 ALTER TABLE distributors ALTER COLUMN street SET NOT NULL;
290 To remove a NOT NULL constraint from a column:
292 ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;
297 To add a check constraint to a table:
299 ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);
304 To remove a check constraint from a table and all its children:
306 ALTER TABLE distributors DROP CONSTRAINT zipchk RESTRICT;
311 To add a foreign key constraint to a table:
313 ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses(address) MATCH FULL;
318 To add a (multicolumn) unique constraint to a table:
320 ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);
325 To add an automatically named primary key constraint to a table, noting
326 that a table can only ever have one primary key:
328 ALTER TABLE distributors ADD PRIMARY KEY (dist_id);
333 <refsect1 id="R1-SQL-ALTERTABLE-3">
338 <refsect2 id="R2-SQL-ALTERTABLE-4">
340 <date>1998-04-15</date>
344 The <literal>ADD COLUMN</literal> form is compliant with the exception that
345 it does not support defaults and NOT NULL constraints, as explained above.
346 The <literal>ALTER COLUMN</literal> form is in full compliance.
350 SQL92 specifies some additional capabilities for <command>ALTER TABLE</command>
351 statement which are not yet directly supported by <productname>PostgreSQL</productname>:
357 ALTER TABLE <replaceable class="PARAMETER">table</replaceable> DROP [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> { RESTRICT | CASCADE }
362 Removes a column from a table.
363 Currently, to remove an existing column the table must be
364 recreated and reloaded:
366 CREATE TABLE temp AS SELECT did, city FROM distributors;
367 DROP TABLE distributors;
368 CREATE TABLE distributors (
369 did DECIMAL(3) DEFAULT 1,
370 name VARCHAR(40) NOT NULL
372 INSERT INTO distributors SELECT * FROM temp;
382 The clauses to rename tables, columns, indexes, and sequences are
383 <productname>PostgreSQL</productname> extensions from SQL92.
390 <!-- Keep this comment at the end of the file
395 sgml-minimize-attributes:nil
396 sgml-always-quote-attributes:t
399 sgml-parent-document:nil
400 sgml-default-dtd-file:"../reference.ced"
401 sgml-exposed-tags:nil
402 sgml-local-catalogs:"/usr/lib/sgml/catalog"
403 sgml-local-ecat-files:nil