~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to doc/src/sgml/ref/alter_table.sgml

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

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