]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/alter_table.sgml
e5856d9bfb4cea718b2e886313267f96f6cf7b92
[postgresql] / doc / src / sgml / ref / alter_table.sgml
1 <!--
2 $PostgreSQL: pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.74 2004/10/22 17:20:04 tgl Exp $
3 PostgreSQL documentation
4 -->
5
6 <refentry id="SQL-ALTERTABLE">
7  <refmeta>
8   <refentrytitle id="sql-altertable-title">ALTER TABLE</refentrytitle>
9   <refmiscinfo>SQL - Language Statements</refmiscinfo>
10  </refmeta>
11
12  <refnamediv>
13   <refname>ALTER TABLE</refname>
14   <refpurpose>change the definition of a table</refpurpose>
15  </refnamediv>
16
17  <indexterm zone="sql-altertable">
18   <primary>ALTER TABLE</primary>
19  </indexterm>
20
21  <refsynopsisdiv>
22 <synopsis>
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
30 where <replaceable class="PARAMETER">action</replaceable> is one of:
31
32     ADD [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> <replaceable class="PARAMETER">type</replaceable> [ <replaceable class="PARAMETER">column_constraint</replaceable> [ ... ] ]
33     DROP [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> [ RESTRICT | CASCADE ]
34     ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> TYPE <replaceable class="PARAMETER">type</replaceable> [ USING <replaceable class="PARAMETER">expression</replaceable> ]
35     ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET DEFAULT <replaceable class="PARAMETER">expression</replaceable>
36     ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> DROP DEFAULT
37     ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> { SET | DROP } NOT NULL
38     ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET STATISTICS <replaceable class="PARAMETER">integer</replaceable>
39     ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
40     ADD <replaceable class="PARAMETER">table_constraint</replaceable>
41     DROP CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> [ RESTRICT | CASCADE ]
42     CLUSTER ON <replaceable class="PARAMETER">index_name</replaceable>
43     SET WITHOUT CLUSTER
44     SET WITHOUT OIDS
45     OWNER TO <replaceable class="PARAMETER">new_owner</replaceable>
46     SET TABLESPACE <replaceable class="PARAMETER">tablespace_name</replaceable>
47 </synopsis>
48  </refsynopsisdiv>
49
50  <refsect1>
51   <title>Description</title>
52
53   <para>
54    <command>ALTER TABLE</command> changes the definition of an existing table.
55    There are several subforms:
56
57   <variablelist>
58    <varlistentry>
59     <term><literal>ADD COLUMN</literal></term>
60     <listitem>
61      <para>
62       This form adds a new column to the table using the same syntax as
63       <xref linkend="SQL-CREATETABLE" endterm="SQL-CREATETABLE-TITLE">.
64      </para>
65     </listitem>
66    </varlistentry>
67
68    <varlistentry>
69     <term><literal>DROP COLUMN</literal></term>
70     <listitem>
71      <para>
72       This form drops a column from a table.  Indexes and
73       table constraints involving the column will be automatically
74       dropped as well.  You will need to say <literal>CASCADE</> if
75       anything outside the table depends on the column, for example,
76       foreign key references or views.
77      </para>
78     </listitem>
79    </varlistentry>
80
81    <varlistentry>
82     <term><literal>ALTER COLUMN TYPE</literal></term>
83     <listitem>
84      <para>
85       This form changes the type of a column of a table. Indexes and
86       simple table constraints involving the column will be automatically
87       converted to use the new column type by reparsing the originally
88       supplied expression.  The optional <literal>USING</literal>
89       clause specifies how to compute the new column value from the old;
90       if omitted, the default conversion is the same as an assignment
91       cast from old data type to new.  A  <literal>USING</literal>
92       clause must be provided if there is no implicit or assignment
93       cast from old to new type.
94      </para>
95     </listitem>
96    </varlistentry>
97
98    <varlistentry>
99     <term><literal>SET</literal>/<literal>DROP DEFAULT</literal></term>
100     <listitem>
101      <para>
102       These forms set or remove the default value for a column.
103       The default values only apply to subsequent <command>INSERT</command>
104       commands; they do not cause rows already in the table to change.
105       Defaults may also be created for views, in which case they are
106       inserted into <command>INSERT</> statements on the view before
107       the view's <literal>ON INSERT</literal> rule is applied.
108      </para>
109     </listitem>
110    </varlistentry>
111
112    <varlistentry>
113     <term><literal>SET</literal>/<literal>DROP NOT NULL</literal></term>
114     <listitem>
115      <para>
116       These forms change whether a column is marked to allow null
117       values or to reject null values.  You can only use <literal>SET
118       NOT NULL</> when the column contains no null values.
119      </para>
120     </listitem>
121    </varlistentry>
122
123    <varlistentry>
124     <term><literal>SET STATISTICS</literal></term>
125     <listitem>
126      <para>
127       This form
128       sets the per-column statistics-gathering target for subsequent
129       <xref linkend="sql-analyze" endterm="sql-analyze-title"> operations.
130       The target can be set in the range 0 to 1000; alternatively, set it
131       to -1 to revert to using the system default statistics
132       target. For more information on the use of statistics by the
133       <productname>PostgreSQL</productname> query planner, refer to
134       <xref linkend="planner-stats">.
135      </para>
136     </listitem>
137    </varlistentry>
138
139    <varlistentry>
140     <indexterm>
141      <primary>TOAST</primary>
142      <secondary>per-column storage settings</secondary>
143     </indexterm>
144
145     <term><literal>SET STORAGE</literal></term>
146     <listitem>
147      <para>
148       This form sets the storage mode for a column. This controls whether this
149       column is held inline or in a supplementary table, and whether the data
150       should be compressed or not. <literal>PLAIN</literal> must be used
151       for fixed-length values such as <type>integer</type> and is
152       inline, uncompressed. <literal>MAIN</literal> is for inline,
153       compressible data. <literal>EXTERNAL</literal> is for external,
154       uncompressed data, and <literal>EXTENDED</literal> is for external,
155       compressed data.  <literal>EXTENDED</literal> is the default for all
156       data types that support it.  The use of <literal>EXTERNAL</literal> will, for example,
157       make substring operations on a <type>text</type> column faster, at the penalty of
158       increased storage space.
159      </para>
160     </listitem>
161    </varlistentry>
162
163    <varlistentry>
164     <term><literal>ADD <replaceable class="PARAMETER">table_constraint</replaceable></literal></term>
165     <listitem>
166      <para>
167       This form adds a new constraint to a table using the same syntax as
168       <xref linkend="SQL-CREATETABLE" endterm="SQL-CREATETABLE-TITLE">. 
169      </para>
170     </listitem>
171    </varlistentry>
172
173    <varlistentry>
174     <term><literal>DROP CONSTRAINT</literal></term>
175     <listitem>
176      <para>
177       This form drops constraints on a table.
178       Currently, constraints on tables are not required to have unique
179       names, so there may be more than one constraint matching the specified
180       name.  All matching constraints will be dropped.
181      </para>
182     </listitem>
183    </varlistentry>
184
185    <varlistentry>
186     <term><literal>CLUSTER</literal></term>
187     <listitem>
188      <para>
189       This form selects the default index for future 
190       <xref linkend="SQL-CLUSTER" endterm="sql-cluster-title">
191       operations.  It does not actually re-cluster the table.
192      </para>
193     </listitem>
194    </varlistentry>
195
196    <varlistentry>
197     <term><literal>SET WITHOUT CLUSTER</literal></term>
198     <listitem>
199      <para>
200       This form removes the most recently used
201       <xref linkend="SQL-CLUSTER" endterm="sql-cluster-title">
202       index specification from the table.  This affects
203       future cluster operations that don't specify an index.
204      </para>
205     </listitem>
206    </varlistentry>
207
208    <varlistentry>
209     <term><literal>SET WITHOUT OIDS</literal></term>
210     <listitem>
211      <para>
212       This form removes the <literal>oid</literal> system column from the
213       table.  This is exactly equivalent to
214       <literal>DROP COLUMN oid RESTRICT</literal>,
215       except that it will not complain if there is already no
216       <literal>oid</literal> column.
217      </para>
218
219      <para>
220       Note that there is no variant of <command>ALTER TABLE</command>
221       that allows OIDs to be restored to a table once they have been
222       removed.
223      </para>
224     </listitem>
225    </varlistentry>
226
227    <varlistentry>
228     <term><literal>OWNER</literal></term>
229     <listitem>
230      <para>
231       This form changes the owner of the table, index, sequence, or view to the
232       specified user.
233      </para>
234     </listitem>
235    </varlistentry>
236
237    <varlistentry>
238     <term><literal>SET TABLESPACE</literal></term>
239     <listitem>
240      <para>
241       This form changes the table's tablespace to the specified tablespace and
242       moves the data file(s) associated with the table to the new tablespace.
243       Indexes on the table, if any, are not moved; but they can be moved
244       separately with additional <literal>SET TABLESPACE</literal> commands.
245       See also 
246       <xref linkend="SQL-CREATETABLESPACE" endterm="sql-createtablespace-title">.
247      </para>
248     </listitem>
249    </varlistentry>
250
251    <varlistentry>
252     <term><literal>RENAME</literal></term>
253     <listitem>
254      <para>
255       The <literal>RENAME</literal> forms change the name of a table
256       (or an index, sequence, or view) or the name of an individual column in
257       a table. There is no effect on the stored data.
258      </para>
259     </listitem>
260    </varlistentry>
261
262   </variablelist>
263   </para>
264
265   <para>
266    All the actions except <literal>RENAME</literal> can be combined into
267    a list of multiple alterations to apply in parallel.  For example, it
268    is possible to add several columns and/or alter the type of several
269    columns in a single command.  This is particularly useful with large
270    tables, since only one pass over the table need be made.
271   </para>
272
273   <para>
274    You must own the table to use <command>ALTER TABLE</>; except for
275    <command>ALTER TABLE OWNER</>, which may only be executed by a superuser.
276   </para>
277  </refsect1>
278
279  <refsect1>
280   <title>Parameters</title>
281
282     <variablelist>
283
284      <varlistentry>
285       <term><replaceable class="PARAMETER">name</replaceable></term>
286       <listitem>
287        <para>
288         The name (possibly schema-qualified) of an existing table to
289         alter. If <literal>ONLY</> is specified, only that table is
290         altered. If <literal>ONLY</> is not specified, the table and all
291         its descendant tables (if any) are updated. <literal>*</> can be
292         appended to the table name to indicate that descendant tables are
293         to be altered, but in the current version, this is the default
294         behavior.  (In releases before 7.1, <literal>ONLY</> was the
295         default behavior.  The default can be altered by changing the
296         configuration parameter <xref linkend="guc-sql-inheritance">.)
297        </para>
298       </listitem>
299      </varlistentry>
300
301      <varlistentry>
302       <term><replaceable class="PARAMETER">column</replaceable></term>
303       <listitem>
304        <para>
305         Name of a new or existing column.
306        </para>
307       </listitem>
308      </varlistentry>
309
310      <varlistentry>
311       <term><replaceable class="PARAMETER">new_column</replaceable></term>
312       <listitem>
313        <para>
314         New name for an existing column.
315        </para>
316       </listitem>
317      </varlistentry>
318
319      <varlistentry>
320       <term><replaceable class="PARAMETER">new_name</replaceable></term>
321       <listitem>
322        <para>
323         New name for the table.
324        </para>
325       </listitem>
326      </varlistentry>
327
328      <varlistentry>
329       <term><replaceable class="PARAMETER">type</replaceable></term>
330       <listitem>
331        <para>
332         Data type of the new column, or new data type for an existing
333         column.
334        </para>
335       </listitem>
336      </varlistentry>
337
338      <varlistentry>
339       <term><replaceable class="PARAMETER">table_constraint</replaceable></term>
340       <listitem>
341        <para>
342         New table constraint for the table.
343        </para>
344       </listitem>
345      </varlistentry>
346
347      <varlistentry>
348       <term><replaceable class="PARAMETER">constraint_name</replaceable></term>
349       <listitem>
350        <para>
351         Name of an existing constraint to drop.
352        </para>
353       </listitem>
354      </varlistentry>
355
356      <varlistentry>
357       <term><literal>CASCADE</literal></term>
358       <listitem>
359        <para>
360         Automatically drop objects that depend on the dropped column
361         or constraint (for example, views referencing the column).
362        </para>
363       </listitem>
364      </varlistentry>
365
366      <varlistentry>
367       <term><literal>RESTRICT</literal></term>
368       <listitem>
369        <para>
370         Refuse to drop the column or constraint if there are any dependent
371         objects. This is the default behavior.
372        </para>
373       </listitem>
374      </varlistentry>
375
376      <varlistentry>
377       <term><replaceable class="PARAMETER">index_name</replaceable></term>
378       <listitem>
379        <para>
380         The index name on which the table should be marked for clustering.
381        </para>
382       </listitem>
383      </varlistentry>
384
385      <varlistentry>
386       <term><replaceable class="PARAMETER">new_owner</replaceable></term>
387       <listitem>
388        <para>
389         The user name of the new owner of the table.
390        </para>
391       </listitem>
392      </varlistentry>
393
394      <varlistentry>
395       <term><replaceable class="PARAMETER">tablespace_name</replaceable></term>
396       <listitem>
397        <para>
398         The tablespace name to which the table will be moved.
399        </para>
400       </listitem>
401      </varlistentry>
402
403     </variablelist>
404  </refsect1>
405
406  <refsect1>
407   <title>Notes</title>
408
409    <para>
410     The key word <literal>COLUMN</literal> is noise and can be omitted.
411    </para>
412
413    <para>
414     When a column is added with <literal>ADD COLUMN</literal>, all existing
415     rows in the table are initialized with the column's default value
416     (NULL if no <literal>DEFAULT</> clause is specified).
417    </para>
418
419    <para>
420     Adding a column with a non-null default or changing the type of an
421     existing column will require the entire table to be rewritten.  This
422     may take a significant amount of time for a large table; and it will
423     temporarily require double the disk space.
424    </para>
425
426    <para>
427     Adding a <literal>CHECK</> or <literal>NOT NULL</> constraint requires
428     scanning the table to verify that existing rows meet the constraint.
429    </para>
430
431    <para>
432     The main reason for providing the option to specify multiple changes
433     in a single <command>ALTER TABLE</> is that multiple table scans or
434     rewrites can thereby be combined into a single pass over the table.
435    </para>
436
437    <para>
438     The <literal>DROP COLUMN</literal> form does not physically remove
439     the column, but simply makes it invisible to SQL operations.  Subsequent
440     insert and update operations in the table will store a null value for the
441     column. Thus, dropping a column is quick but it will not immediately
442     reduce the on-disk size of your table, as the space occupied 
443     by the dropped column is not reclaimed.  The space will be
444     reclaimed over time as existing rows are updated.
445    </para>
446
447    <para>
448     The fact that <literal>ALTER TYPE</> requires rewriting the whole table
449     is sometimes an advantage, because the rewriting process eliminates
450     any dead space in the table.  For example, to reclaim the space occupied
451     by a dropped column immediately, the fastest way is
452 <programlisting>
453 ALTER TABLE table ALTER COLUMN anycol TYPE anytype;
454 </programlisting>
455     where <literal>anycol</> is any remaining table column and
456     <literal>anytype</> is the same type that column already has.
457     This results in no semantically-visible change in the table,
458     but the command forces rewriting, which gets rid of no-longer-useful
459     data.
460    </para>
461
462    <para>
463     The <literal>USING</literal> option of <literal>ALTER TYPE</> can actually
464     specify any expression involving the old values of the row; that is, it
465     can refer to other columns as well as the one being converted.  This allows
466     very general conversions to be done with the <literal>ALTER TYPE</>
467     syntax.  Because of this flexibility, the <literal>USING</literal>
468     expression is not applied to the column's default value (if any); the
469     result might not be a constant expression as required for a default.
470     This means that when there is no implicit or assignment cast from old to
471     new type, <literal>ALTER TYPE</> may fail to convert the default even
472     though a <literal>USING</literal> clause is supplied.  In such cases,
473     drop the default with <literal>DROP DEFAULT</>, perform the <literal>ALTER
474     TYPE</>, and then use <literal>SET DEFAULT</> to add a suitable new
475     default.
476    </para>
477
478    <para>
479     If a table has any descendant tables, it is not permitted to add,
480     rename, or change the type of a column in the parent table without doing
481     the same to the descendants.  That is, <command>ALTER TABLE ONLY</command>
482     will be rejected.  This ensures that the descendants always have
483     columns matching the parent.
484    </para>
485
486    <para>
487     A recursive <literal>DROP COLUMN</literal> operation will remove a
488     descendant table's column only if the descendant does not inherit
489     that column from any other parents and never had an independent
490     definition of the column.  A nonrecursive <literal>DROP
491     COLUMN</literal> (i.e., <command>ALTER TABLE ONLY ... DROP
492     COLUMN</command>) never removes any descendant columns, but
493     instead marks them as independently defined rather than inherited.
494    </para>
495
496    <para>
497     Changing any part of a system catalog table is not permitted.
498    </para>
499
500    <para>
501     Refer to <xref linkend="sql-createtable"
502     endterm="sql-createtable-title"> for a further description of valid
503     parameters. <xref linkend="ddl"> has further information on
504     inheritance.
505    </para>
506  </refsect1>
507
508  <refsect1>
509   <title>Examples</title>
510
511   <para>
512    To add a column of type <type>varchar</type> to a table:
513 <programlisting>
514 ALTER TABLE distributors ADD COLUMN address varchar(30);
515 </programlisting>
516   </para>
517
518   <para>
519    To drop a column from a table:
520 <programlisting>
521 ALTER TABLE distributors DROP COLUMN address RESTRICT;
522 </programlisting>
523   </para>
524
525   <para>
526    To change the types of two existing columns in one operation:
527 <programlisting>
528 ALTER TABLE distributors
529     ALTER COLUMN address TYPE varchar(80),
530     ALTER COLUMN name TYPE varchar(100);
531 </programlisting>
532   </para>
533
534   <para>
535    To rename an existing column:
536 <programlisting>
537 ALTER TABLE distributors RENAME COLUMN address TO city;
538 </programlisting>
539   </para>
540
541   <para>
542    To rename an existing table:
543 <programlisting>
544 ALTER TABLE distributors RENAME TO suppliers;
545 </programlisting>
546   </para>
547
548   <para>
549    To add a not-null constraint to a column:
550 <programlisting>
551 ALTER TABLE distributors ALTER COLUMN street SET NOT NULL;
552 </programlisting>
553    To remove a not-null constraint from a column:
554 <programlisting>
555 ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;
556 </programlisting>
557   </para>
558
559   <para> 
560    To add a check constraint to a table:
561 <programlisting>
562 ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);
563 </programlisting>
564   </para>
565
566   <para> 
567    To remove a check constraint from a table and all its children:
568 <programlisting>
569 ALTER TABLE distributors DROP CONSTRAINT zipchk;
570 </programlisting>
571   </para>
572
573   <para> 
574    To add a foreign key constraint to a table:
575 <programlisting>
576 ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) MATCH FULL;
577 </programlisting>
578   </para>
579
580   <para> 
581    To add a (multicolumn) unique constraint to a table:
582 <programlisting>
583 ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);
584 </programlisting>
585   </para>
586
587   <para> 
588    To add an automatically named primary key constraint to a table, noting
589    that a table can only ever have one primary key:
590 <programlisting>
591 ALTER TABLE distributors ADD PRIMARY KEY (dist_id);
592 </programlisting>
593   </para>
594
595   <para> 
596         To move a table to a different tablespace:
597 <programlisting>
598 ALTER TABLE distributors SET TABLESPACE fasttablespace;
599 </programlisting>
600   </para>
601
602  </refsect1>
603
604  <refsect1>
605   <title>Compatibility</title>
606
607   <para>
608    The <literal>ADD</literal>, <literal>DROP</>, and <literal>SET DEFAULT</>
609    forms conform with the SQL standard.  The other forms are
610    <productname>PostgreSQL</productname> extensions of the SQL standard.
611    Also, the ability to specify more than one manipulation in a single
612    <command>ALTER TABLE</> command is an extension.
613   </para>
614
615   <para>
616    <command>ALTER TABLE DROP COLUMN</> can be used to drop the only
617    column of a table, leaving a zero-column table.  This is an
618    extension of SQL, which disallows zero-column tables.
619   </para>
620  </refsect1>
621 </refentry>
622
623 <!-- Keep this comment at the end of the file
624 Local variables:
625 mode: sgml
626 sgml-omittag:nil
627 sgml-shorttag:t
628 sgml-minimize-attributes:nil
629 sgml-always-quote-attributes:t
630 sgml-indent-step:1
631 sgml-indent-data:t
632 sgml-parent-document:nil
633 sgml-default-dtd-file:"../reference.ced"
634 sgml-exposed-tags:nil
635 sgml-local-catalogs:"/usr/lib/sgml/catalog"
636 sgml-local-ecat-files:nil
637 End:
638 -->