]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/alter_table.sgml
ee501a1a37261f72cf21f480a846f62df8fb2ce8
[postgresql] / doc / src / sgml / ref / alter_table.sgml
1 <!--
2 $PostgreSQL: pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.85 2006/07/02 01:58:36 momjian 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 ALTER TABLE <replaceable class="PARAMETER">name</replaceable>
30     SET SCHEMA <replaceable class="PARAMETER">new_schema</replaceable>
31
32 where <replaceable class="PARAMETER">action</replaceable> is one of:
33
34     ADD [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> <replaceable class="PARAMETER">type</replaceable> [ <replaceable class="PARAMETER">column_constraint</replaceable> [ ... ] ]
35     DROP [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> [ RESTRICT | CASCADE ]
36     ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> TYPE <replaceable class="PARAMETER">type</replaceable> [ USING <replaceable class="PARAMETER">expression</replaceable> ]
37     ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET DEFAULT <replaceable class="PARAMETER">expression</replaceable>
38     ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> DROP DEFAULT
39     ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> { SET | DROP } NOT NULL
40     ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET STATISTICS <replaceable class="PARAMETER">integer</replaceable>
41     ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
42     ADD <replaceable class="PARAMETER">table_constraint</replaceable>
43     DROP CONSTRAINT <replaceable class="PARAMETER">constraint_name</replaceable> [ RESTRICT | CASCADE ]
44     DISABLE TRIGGER [ <replaceable class="PARAMETER">trigger_name</replaceable> | ALL | USER ]
45     ENABLE TRIGGER [ <replaceable class="PARAMETER">trigger_name</replaceable> | ALL | USER ]
46     CLUSTER ON <replaceable class="PARAMETER">index_name</replaceable>
47     SET WITHOUT CLUSTER
48     SET WITHOUT OIDS
49     INHERIT <replaceable class="PARAMETER">parent_table</replaceable>
50     NO INHERIT <replaceable class="PARAMETER">parent_table</replaceable>
51     OWNER TO <replaceable class="PARAMETER">new_owner</replaceable>
52     SET TABLESPACE <replaceable class="PARAMETER">new_tablespace</replaceable>
53 </synopsis>
54  </refsynopsisdiv>
55
56  <refsect1>
57   <title>Description</title>
58
59   <para>
60    <command>ALTER TABLE</command> changes the definition of an existing table.
61    There are several subforms:
62
63   <variablelist>
64    <varlistentry>
65     <term><literal>ADD COLUMN</literal></term>
66     <listitem>
67      <para>
68       This form adds a new column to the table, using the same syntax as
69       <xref linkend="SQL-CREATETABLE" endterm="SQL-CREATETABLE-TITLE">.
70      </para>
71     </listitem>
72    </varlistentry>
73
74    <varlistentry>
75     <term><literal>DROP COLUMN</literal></term>
76     <listitem>
77      <para>
78       This form drops a column from a table.  Indexes and
79       table constraints involving the column will be automatically
80       dropped as well.  You will need to say <literal>CASCADE</> if
81       anything outside the table depends on the column, for example,
82       foreign key references or views.
83      </para>
84     </listitem>
85    </varlistentry>
86
87    <varlistentry>
88     <term><literal>ALTER COLUMN TYPE</literal></term>
89     <listitem>
90      <para>
91       This form changes the type of a column of a table. Indexes and
92       simple table constraints involving the column will be automatically
93       converted to use the new column type by reparsing the originally
94       supplied expression.  The optional <literal>USING</literal>
95       clause specifies how to compute the new column value from the old;
96       if omitted, the default conversion is the same as an assignment
97       cast from old data type to new.  A  <literal>USING</literal>
98       clause must be provided if there is no implicit or assignment
99       cast from old to new type.
100      </para>
101     </listitem>
102    </varlistentry>
103
104    <varlistentry>
105     <term><literal>SET</literal>/<literal>DROP DEFAULT</literal></term>
106     <listitem>
107      <para>
108       These forms set or remove the default value for a column.
109       The default values only apply to subsequent <command>INSERT</command>
110       commands; they do not cause rows already in the table to change.
111       Defaults may also be created for views, in which case they are
112       inserted into <command>INSERT</> statements on the view before
113       the view's <literal>ON INSERT</literal> rule is applied.
114      </para>
115     </listitem>
116    </varlistentry>
117
118    <varlistentry>
119     <term><literal>SET</literal>/<literal>DROP NOT NULL</literal></term>
120     <listitem>
121      <para>
122       These forms change whether a column is marked to allow null
123       values or to reject null values.  You can only use <literal>SET
124       NOT NULL</> when the column contains no null values.
125      </para>
126     </listitem>
127    </varlistentry>
128
129    <varlistentry>
130     <term><literal>SET STATISTICS</literal></term>
131     <listitem>
132      <para>
133       This form
134       sets the per-column statistics-gathering target for subsequent
135       <xref linkend="sql-analyze" endterm="sql-analyze-title"> operations.
136       The target can be set in the range 0 to 1000; alternatively, set it
137       to -1 to revert to using the system default statistics
138       target (<xref linkend="guc-default-statistics-target">).
139       For more information on the use of statistics by the
140       <productname>PostgreSQL</productname> query planner, refer to
141       <xref linkend="planner-stats">.
142      </para>
143     </listitem>
144    </varlistentry>
145
146    <varlistentry>
147     <indexterm>
148      <primary>TOAST</primary>
149      <secondary>per-column storage settings</secondary>
150     </indexterm>
151
152     <term><literal>SET STORAGE</literal></term>
153     <listitem>
154      <para>
155       This form sets the storage mode for a column. This controls whether this
156       column is held inline or in a supplementary table, and whether the data
157       should be compressed or not. <literal>PLAIN</literal> must be used
158       for fixed-length values such as <type>integer</type> and is
159       inline, uncompressed. <literal>MAIN</literal> is for inline,
160       compressible data. <literal>EXTERNAL</literal> is for external,
161       uncompressed data, and <literal>EXTENDED</literal> is for external,
162       compressed data.  <literal>EXTENDED</literal> is the default for most
163       data types that support non-<literal>PLAIN</literal> storage.
164       Use of <literal>EXTERNAL</literal> will
165       make substring operations on <type>text</type> and <type>bytea</type>
166       columns faster, at the penalty of increased storage space.  Note that
167       <literal>SET STORAGE</> doesn't itself change anything in the table,
168       it just sets the strategy to be pursued during future table updates.
169       See <xref linkend="storage-toast"> for more information.
170      </para>
171     </listitem>
172    </varlistentry>
173
174    <varlistentry>
175     <term><literal>ADD <replaceable class="PARAMETER">table_constraint</replaceable></literal></term>
176     <listitem>
177      <para>
178       This form adds a new constraint to a table using the same syntax as
179       <xref linkend="SQL-CREATETABLE" endterm="SQL-CREATETABLE-TITLE">. 
180      </para>
181     </listitem>
182    </varlistentry>
183
184    <varlistentry>
185     <term><literal>DROP CONSTRAINT</literal></term>
186     <listitem>
187      <para>
188       This form drops the specified constraint on a table.
189      </para>
190     </listitem>
191    </varlistentry>
192
193    <varlistentry>
194     <term><literal>DISABLE</literal>/<literal>ENABLE TRIGGER</literal></term>
195     <listitem>
196      <para>
197       These forms disable or enable trigger(s) belonging to the table.
198       A disabled trigger is still known to the system, but is not executed
199       when its triggering event occurs.  For a deferred trigger, the enable
200       status is checked when the event occurs, not when the trigger function
201       is actually executed.  One may disable or enable a single
202       trigger specified by name, or all triggers on the table, or only
203       user triggers (this option excludes triggers that are used to implement
204       foreign key constraints).  Disabling or enabling constraint triggers
205       requires superuser privileges; it should be done with caution since
206       of course the integrity of the constraint cannot be guaranteed if the
207       triggers are not executed.
208      </para>
209     </listitem>
210    </varlistentry>
211
212    <varlistentry>
213     <term><literal>CLUSTER</literal></term>
214     <listitem>
215      <para>
216       This form selects the default index for future 
217       <xref linkend="SQL-CLUSTER" endterm="sql-cluster-title">
218       operations.  It does not actually re-cluster the table.
219      </para>
220     </listitem>
221    </varlistentry>
222
223    <varlistentry>
224     <term><literal>SET WITHOUT CLUSTER</literal></term>
225     <listitem>
226      <para>
227       This form removes the most recently used
228       <xref linkend="SQL-CLUSTER" endterm="sql-cluster-title">
229       index specification from the table.  This affects
230       future cluster operations that don't specify an index.
231      </para>
232     </listitem>
233    </varlistentry>
234
235    <varlistentry>
236     <term><literal>SET WITHOUT OIDS</literal></term>
237     <listitem>
238      <para>
239       This form removes the <literal>oid</literal> system column from the
240       table.  This is exactly equivalent to
241       <literal>DROP COLUMN oid RESTRICT</literal>,
242       except that it will not complain if there is already no
243       <literal>oid</literal> column.
244      </para>
245
246      <para>
247       Note that there is no variant of <command>ALTER TABLE</command>
248       that allows OIDs to be restored to a table once they have been
249       removed.
250      </para>
251     </listitem>
252    </varlistentry>
253
254    <varlistentry>
255     <term><literal>INHERIT <replaceable class="PARAMETER">parent_table</replaceable></literal></term>
256     <listitem>
257      <para>
258
259       This form adds a new parent table to the table. This won't add new
260       columns to the child table, instead all columns of the parent table must
261       already exist in the child table. They must have matching data types,
262       and if they have <literal>NOT NULL</literal> constraints in the parent
263       then they must also have <literal>NOT NULL</literal> constraints in the
264       child.
265
266           </para>
267           <para>
268
269       There must also be matching table constraints for all
270       <literal>CHECK</literal> table constraints of the parent. Currently
271       <literal>UNIQUE</literal>, <literal>PRIMARY KEY</literal>, and
272       <literal>FOREIGN KEY</literal> constraints are ignored however this may
273       change in the future.
274
275           </para>
276           <para>
277
278       The easiest way to create a suitable table is to create a table using
279       <literal>INHERITS</literal> and then remove it via <literal>NO
280       INHERIT</literal>. Alternatively create a table using
281       <literal>LIKE</literal> however note that <literal>LIKE</literal> does
282       not create the necessary constraints.
283
284      </para>
285
286     </listitem>
287    </varlistentry>
288
289    <varlistentry>
290     <term><literal>NO INHERIT <replaceable class="PARAMETER">parent_table</replaceable></literal></term>
291     <listitem>
292      <para>
293          This form removes a parent table from the list of parents of the table.
294          Queries against the parent table will no longer include records drawn
295          from the target table.
296      </para>
297     </listitem>
298    </varlistentry>
299
300    <varlistentry>
301     <term><literal>OWNER</literal></term>
302     <listitem>
303      <para>
304       This form changes the owner of the table, sequence, or view to the
305       specified user.
306      </para>
307     </listitem>
308    </varlistentry>
309
310    <varlistentry>
311     <term><literal>SET TABLESPACE</literal></term>
312     <listitem>
313      <para>
314       This form changes the table's tablespace to the specified tablespace and
315       moves the data file(s) associated with the table to the new tablespace.
316       Indexes on the table, if any, are not moved; but they can be moved
317       separately with additional <literal>SET TABLESPACE</literal> commands.
318       See also 
319       <xref linkend="SQL-CREATETABLESPACE" endterm="sql-createtablespace-title">.
320      </para>
321     </listitem>
322    </varlistentry>
323
324    <varlistentry>
325     <term><literal>RENAME</literal></term>
326     <listitem>
327      <para>
328       The <literal>RENAME</literal> forms change the name of a table
329       (or an index, sequence, or view) or the name of an individual column in
330       a table. There is no effect on the stored data.
331      </para>
332     </listitem>
333    </varlistentry>
334
335    <varlistentry>
336     <term><literal>SET SCHEMA</literal></term>
337     <listitem>
338      <para>
339       This form moves the table into another schema.  Associated indexes,
340       constraints, and SERIAL-column sequences are moved as well.
341      </para>
342     </listitem>
343    </varlistentry>
344
345   </variablelist>
346   </para>
347
348   <para>
349    All the actions except <literal>RENAME</literal> and <literal>SET SCHEMA</>
350    can be combined into
351    a list of multiple alterations to apply in parallel.  For example, it
352    is possible to add several columns and/or alter the type of several
353    columns in a single command.  This is particularly useful with large
354    tables, since only one pass over the table need be made.
355   </para>
356
357   <para>
358    You must own the table to use <command>ALTER TABLE</>.
359    To change the schema of a table, you must also have
360    <literal>CREATE</literal> privilege on the new schema.
361    To alter the owner, you must also be a direct or indirect member of the new
362    owning role, and that role must have <literal>CREATE</literal> privilege on
363    the table's schema.  (These restrictions enforce that altering the owner
364    doesn't do anything you couldn't do by dropping and recreating the table.
365    However, a superuser can alter ownership of any table anyway.)
366   </para>
367  </refsect1>
368
369  <refsect1>
370   <title>Parameters</title>
371
372     <variablelist>
373
374      <varlistentry>
375       <term><replaceable class="PARAMETER">name</replaceable></term>
376       <listitem>
377        <para>
378         The name (possibly schema-qualified) of an existing table to
379         alter. If <literal>ONLY</> is specified, only that table is
380         altered. If <literal>ONLY</> is not specified, the table and all
381         its descendant tables (if any) are updated. <literal>*</> can be
382         appended to the table name to indicate that descendant tables are
383         to be altered, but in the current version, this is the default
384         behavior.  (In releases before 7.1, <literal>ONLY</> was the
385         default behavior.  The default can be altered by changing the
386         configuration parameter <xref linkend="guc-sql-inheritance">.)
387        </para>
388       </listitem>
389      </varlistentry>
390
391      <varlistentry>
392       <term><replaceable class="PARAMETER">column</replaceable></term>
393       <listitem>
394        <para>
395         Name of a new or existing column.
396        </para>
397       </listitem>
398      </varlistentry>
399
400      <varlistentry>
401       <term><replaceable class="PARAMETER">new_column</replaceable></term>
402       <listitem>
403        <para>
404         New name for an existing column.
405        </para>
406       </listitem>
407      </varlistentry>
408
409      <varlistentry>
410       <term><replaceable class="PARAMETER">new_name</replaceable></term>
411       <listitem>
412        <para>
413         New name for the table.
414        </para>
415       </listitem>
416      </varlistentry>
417
418      <varlistentry>
419       <term><replaceable class="PARAMETER">type</replaceable></term>
420       <listitem>
421        <para>
422         Data type of the new column, or new data type for an existing
423         column.
424        </para>
425       </listitem>
426      </varlistentry>
427
428      <varlistentry>
429       <term><replaceable class="PARAMETER">table_constraint</replaceable></term>
430       <listitem>
431        <para>
432         New table constraint for the table.
433        </para>
434       </listitem>
435      </varlistentry>
436
437      <varlistentry>
438       <term><replaceable class="PARAMETER">constraint_name</replaceable></term>
439       <listitem>
440        <para>
441         Name of an existing constraint to drop.
442        </para>
443       </listitem>
444      </varlistentry>
445
446      <varlistentry>
447       <term><literal>CASCADE</literal></term>
448       <listitem>
449        <para>
450         Automatically drop objects that depend on the dropped column
451         or constraint (for example, views referencing the column).
452        </para>
453       </listitem>
454      </varlistentry>
455
456      <varlistentry>
457       <term><literal>RESTRICT</literal></term>
458       <listitem>
459        <para>
460         Refuse to drop the column or constraint if there are any dependent
461         objects. This is the default behavior.
462        </para>
463       </listitem>
464      </varlistentry>
465
466      <varlistentry>
467       <term><replaceable class="PARAMETER">trigger_name</replaceable></term>
468       <listitem>
469        <para>
470         Name of a single trigger to disable or enable.
471        </para>
472       </listitem>
473      </varlistentry>
474
475      <varlistentry>
476       <term><literal>ALL</literal></term>
477       <listitem>
478        <para>
479         Disable or enable all triggers belonging to the table.
480         (This requires superuser privilege if any of the triggers are for
481         foreign key constraints.)
482        </para>
483       </listitem>
484      </varlistentry>
485
486      <varlistentry>
487       <term><literal>USER</literal></term>
488       <listitem>
489        <para>
490         Disable or enable all triggers belonging to the table except for
491         foreign key constraint triggers.
492        </para>
493       </listitem>
494      </varlistentry>
495
496      <varlistentry>
497       <term><replaceable class="PARAMETER">index_name</replaceable></term>
498       <listitem>
499        <para>
500         The index name on which the table should be marked for clustering.
501        </para>
502       </listitem>
503      </varlistentry>
504
505      <varlistentry>
506       <term><replaceable class="PARAMETER">new_owner</replaceable></term>
507       <listitem>
508        <para>
509         The user name of the new owner of the table.
510        </para>
511       </listitem>
512      </varlistentry>
513
514      <varlistentry>
515       <term><replaceable class="PARAMETER">new_tablespace</replaceable></term>
516       <listitem>
517        <para>
518         The name of the tablespace to which the table will be moved.
519        </para>
520       </listitem>
521      </varlistentry>
522
523      <varlistentry>
524       <term><replaceable class="PARAMETER">new_schema</replaceable></term>
525       <listitem>
526        <para>
527         The name of the schema to which the table will be moved.
528        </para>
529       </listitem>
530      </varlistentry>
531
532     </variablelist>
533  </refsect1>
534
535  <refsect1>
536   <title>Notes</title>
537
538    <para>
539     The key word <literal>COLUMN</literal> is noise and can be omitted.
540    </para>
541
542    <para>
543     When a column is added with <literal>ADD COLUMN</literal>, all existing
544     rows in the table are initialized with the column's default value
545     (NULL if no <literal>DEFAULT</> clause is specified).
546    </para>
547
548    <para>
549     Adding a column with a non-null default or changing the type of an
550     existing column will require the entire table to be rewritten.  This
551     may take a significant amount of time for a large table; and it will
552     temporarily require double the disk space.
553    </para>
554
555    <para>
556     Adding a <literal>CHECK</> or <literal>NOT NULL</> constraint requires
557     scanning the table to verify that existing rows meet the constraint.
558    </para>
559
560    <para>
561     The main reason for providing the option to specify multiple changes
562     in a single <command>ALTER TABLE</> is that multiple table scans or
563     rewrites can thereby be combined into a single pass over the table.
564    </para>
565
566    <para>
567     The <literal>DROP COLUMN</literal> form does not physically remove
568     the column, but simply makes it invisible to SQL operations.  Subsequent
569     insert and update operations in the table will store a null value for the
570     column. Thus, dropping a column is quick but it will not immediately
571     reduce the on-disk size of your table, as the space occupied 
572     by the dropped column is not reclaimed.  The space will be
573     reclaimed over time as existing rows are updated.
574    </para>
575
576    <para>
577     The fact that <literal>ALTER TYPE</> requires rewriting the whole table
578     is sometimes an advantage, because the rewriting process eliminates
579     any dead space in the table.  For example, to reclaim the space occupied
580     by a dropped column immediately, the fastest way is
581 <programlisting>
582 ALTER TABLE table ALTER COLUMN anycol TYPE anytype;
583 </programlisting>
584     where <literal>anycol</> is any remaining table column and
585     <literal>anytype</> is the same type that column already has.
586     This results in no semantically-visible change in the table,
587     but the command forces rewriting, which gets rid of no-longer-useful
588     data.
589    </para>
590
591    <para>
592     The <literal>USING</literal> option of <literal>ALTER TYPE</> can actually
593     specify any expression involving the old values of the row; that is, it
594     can refer to other columns as well as the one being converted.  This allows
595     very general conversions to be done with the <literal>ALTER TYPE</>
596     syntax.  Because of this flexibility, the <literal>USING</literal>
597     expression is not applied to the column's default value (if any); the
598     result might not be a constant expression as required for a default.
599     This means that when there is no implicit or assignment cast from old to
600     new type, <literal>ALTER TYPE</> may fail to convert the default even
601     though a <literal>USING</literal> clause is supplied.  In such cases,
602     drop the default with <literal>DROP DEFAULT</>, perform the <literal>ALTER
603     TYPE</>, and then use <literal>SET DEFAULT</> to add a suitable new
604     default.  Similar considerations apply to indexes and constraints involving
605     the column.
606    </para>
607
608    <para>
609     If a table has any descendant tables, it is not permitted to add,
610     rename, or change the type of a column in the parent table without doing
611     the same to the descendants.  That is, <command>ALTER TABLE ONLY</command>
612     will be rejected.  This ensures that the descendants always have
613     columns matching the parent.
614    </para>
615
616    <para>
617     A recursive <literal>DROP COLUMN</literal> operation will remove a
618     descendant table's column only if the descendant does not inherit
619     that column from any other parents and never had an independent
620     definition of the column.  A nonrecursive <literal>DROP
621     COLUMN</literal> (i.e., <command>ALTER TABLE ONLY ... DROP
622     COLUMN</command>) never removes any descendant columns, but
623     instead marks them as independently defined rather than inherited.
624    </para>
625
626    <para>
627     The <literal>TRIGGER</>, <literal>CLUSTER</>, <literal>OWNER</>,
628     and <literal>TABLESPACE</> actions never recurse to descendant tables;
629     that is, they always act as though <literal>ONLY</> were specified.
630     Adding a constraint can recurse only for <literal>CHECK</> constraints.
631    </para>
632
633    <para>
634     Changing any part of a system catalog table is not permitted.
635    </para>
636
637    <para>
638     Refer to <xref linkend="sql-createtable"
639     endterm="sql-createtable-title"> for a further description of valid
640     parameters. <xref linkend="ddl"> has further information on
641     inheritance.
642    </para>
643  </refsect1>
644
645  <refsect1>
646   <title>Examples</title>
647
648   <para>
649    To add a column of type <type>varchar</type> to a table:
650 <programlisting>
651 ALTER TABLE distributors ADD COLUMN address varchar(30);
652 </programlisting>
653   </para>
654
655   <para>
656    To drop a column from a table:
657 <programlisting>
658 ALTER TABLE distributors DROP COLUMN address RESTRICT;
659 </programlisting>
660   </para>
661
662   <para>
663    To change the types of two existing columns in one operation:
664 <programlisting>
665 ALTER TABLE distributors
666     ALTER COLUMN address TYPE varchar(80),
667     ALTER COLUMN name TYPE varchar(100);
668 </programlisting>
669   </para>
670
671   <para>
672    To change an integer column containing UNIX timestamps to <type>timestamp
673    with time zone</type> via a <literal>USING</literal> clause:
674 <programlisting>
675 ALTER TABLE foo
676     ALTER COLUMN foo_timestamp TYPE timestamp with time zone
677     USING
678         timestamp with time zone 'epoch' + foo_timestamp * interval '1 second';
679 </programlisting>
680   </para>
681
682   <para>
683    To rename an existing column:
684 <programlisting>
685 ALTER TABLE distributors RENAME COLUMN address TO city;
686 </programlisting>
687   </para>
688
689   <para>
690    To rename an existing table:
691 <programlisting>
692 ALTER TABLE distributors RENAME TO suppliers;
693 </programlisting>
694   </para>
695
696   <para>
697    To add a not-null constraint to a column:
698 <programlisting>
699 ALTER TABLE distributors ALTER COLUMN street SET NOT NULL;
700 </programlisting>
701    To remove a not-null constraint from a column:
702 <programlisting>
703 ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;
704 </programlisting>
705   </para>
706
707   <para> 
708    To add a check constraint to a table:
709 <programlisting>
710 ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);
711 </programlisting>
712   </para>
713
714   <para> 
715    To remove a check constraint from a table and all its children:
716 <programlisting>
717 ALTER TABLE distributors DROP CONSTRAINT zipchk;
718 </programlisting>
719   </para>
720
721   <para> 
722    To add a foreign key constraint to a table:
723 <programlisting>
724 ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) MATCH FULL;
725 </programlisting>
726   </para>
727
728   <para> 
729    To add a (multicolumn) unique constraint to a table:
730 <programlisting>
731 ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);
732 </programlisting>
733   </para>
734
735   <para> 
736    To add an automatically named primary key constraint to a table, noting
737    that a table can only ever have one primary key:
738 <programlisting>
739 ALTER TABLE distributors ADD PRIMARY KEY (dist_id);
740 </programlisting>
741   </para>
742
743   <para> 
744    To move a table to a different tablespace:
745 <programlisting>
746 ALTER TABLE distributors SET TABLESPACE fasttablespace;
747 </programlisting>
748   </para>
749
750   <para> 
751    To move a table to a different schema:
752 <programlisting>
753 ALTER TABLE myschema.distributors SET SCHEMA yourschema;
754 </programlisting>
755   </para>
756
757  </refsect1>
758
759  <refsect1>
760   <title>Compatibility</title>
761
762   <para>
763    The <literal>ADD</literal>, <literal>DROP</>, and <literal>SET DEFAULT</>
764    forms conform with the SQL standard.  The other forms are
765    <productname>PostgreSQL</productname> extensions of the SQL standard.
766    Also, the ability to specify more than one manipulation in a single
767    <command>ALTER TABLE</> command is an extension.
768   </para>
769
770   <para>
771    <command>ALTER TABLE DROP COLUMN</> can be used to drop the only
772    column of a table, leaving a zero-column table.  This is an
773    extension of SQL, which disallows zero-column tables.
774   </para>
775  </refsect1>
776 </refentry>
777
778 <!-- Keep this comment at the end of the file
779 Local variables:
780 mode: sgml
781 sgml-omittag:nil
782 sgml-shorttag:t
783 sgml-minimize-attributes:nil
784 sgml-always-quote-attributes:t
785 sgml-indent-step:1
786 sgml-indent-data:t
787 sgml-parent-document:nil
788 sgml-default-dtd-file:"../reference.ced"
789 sgml-exposed-tags:nil
790 sgml-local-catalogs:"/usr/lib/sgml/catalog"
791 sgml-local-ecat-files:nil
792 End:
793 -->