~ubuntu-branches/ubuntu/oneiric/postgresql-9.1/oneiric-security

« back to all changes in this revision

Viewing changes to doc/src/sgml/html/sql-altertable.html

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-05-11 10:41:53 UTC
  • Revision ID: james.westby@ubuntu.com-20110511104153-psbh2o58553fv1m0
Tags: upstream-9.1~beta1
ImportĀ upstreamĀ versionĀ 9.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 
2
<HTML
 
3
><HEAD
 
4
><TITLE
 
5
>ALTER TABLE</TITLE
 
6
><META
 
7
NAME="GENERATOR"
 
8
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
 
9
REV="MADE"
 
10
HREF="mailto:pgsql-docs@postgresql.org"><LINK
 
11
REL="HOME"
 
12
TITLE="PostgreSQL 9.1beta1 Documentation"
 
13
HREF="index.html"><LINK
 
14
REL="UP"
 
15
TITLE="SQL Commands"
 
16
HREF="sql-commands.html"><LINK
 
17
REL="PREVIOUS"
 
18
TITLE="ALTER SERVER"
 
19
HREF="sql-alterserver.html"><LINK
 
20
REL="NEXT"
 
21
TITLE="ALTER TABLESPACE"
 
22
HREF="sql-altertablespace.html"><LINK
 
23
REL="STYLESHEET"
 
24
TYPE="text/css"
 
25
HREF="stylesheet.css"><META
 
26
HTTP-EQUIV="Content-Type"
 
27
CONTENT="text/html; charset=ISO-8859-1"><META
 
28
NAME="creation"
 
29
CONTENT="2011-04-27T21:20:33"></HEAD
 
30
><BODY
 
31
CLASS="REFENTRY"
 
32
><DIV
 
33
CLASS="NAVHEADER"
 
34
><TABLE
 
35
SUMMARY="Header navigation table"
 
36
WIDTH="100%"
 
37
BORDER="0"
 
38
CELLPADDING="0"
 
39
CELLSPACING="0"
 
40
><TR
 
41
><TH
 
42
COLSPAN="5"
 
43
ALIGN="center"
 
44
VALIGN="bottom"
 
45
><A
 
46
HREF="index.html"
 
47
>PostgreSQL 9.1beta1 Documentation</A
 
48
></TH
 
49
></TR
 
50
><TR
 
51
><TD
 
52
WIDTH="10%"
 
53
ALIGN="left"
 
54
VALIGN="top"
 
55
><A
 
56
TITLE="ALTER SERVER"
 
57
HREF="sql-alterserver.html"
 
58
ACCESSKEY="P"
 
59
>Prev</A
 
60
></TD
 
61
><TD
 
62
WIDTH="10%"
 
63
ALIGN="left"
 
64
VALIGN="top"
 
65
><A
 
66
TITLE="ALTER SERVER"
 
67
HREF="sql-alterserver.html"
 
68
>Fast Backward</A
 
69
></TD
 
70
><TD
 
71
WIDTH="60%"
 
72
ALIGN="center"
 
73
VALIGN="bottom"
 
74
></TD
 
75
><TD
 
76
WIDTH="10%"
 
77
ALIGN="right"
 
78
VALIGN="top"
 
79
><A
 
80
TITLE="ALTER TABLESPACE"
 
81
HREF="sql-altertablespace.html"
 
82
>Fast Forward</A
 
83
></TD
 
84
><TD
 
85
WIDTH="10%"
 
86
ALIGN="right"
 
87
VALIGN="top"
 
88
><A
 
89
TITLE="ALTER TABLESPACE"
 
90
HREF="sql-altertablespace.html"
 
91
ACCESSKEY="N"
 
92
>Next</A
 
93
></TD
 
94
></TR
 
95
></TABLE
 
96
><HR
 
97
ALIGN="LEFT"
 
98
WIDTH="100%"></DIV
 
99
><H1
 
100
><A
 
101
NAME="SQL-ALTERTABLE"
 
102
></A
 
103
>ALTER TABLE</H1
 
104
><DIV
 
105
CLASS="REFNAMEDIV"
 
106
><A
 
107
NAME="AEN61631"
 
108
></A
 
109
><H2
 
110
>Name</H2
 
111
>ALTER TABLE&nbsp;--&nbsp;change the definition of a table</DIV
 
112
><DIV
 
113
CLASS="REFSYNOPSISDIV"
 
114
><A
 
115
NAME="AEN61636"
 
116
></A
 
117
><H2
 
118
>Synopsis</H2
 
119
><PRE
 
120
CLASS="SYNOPSIS"
 
121
>ALTER TABLE [ ONLY ] <TT
 
122
CLASS="REPLACEABLE"
 
123
><I
 
124
>name</I
 
125
></TT
 
126
> [ * ]
 
127
    <TT
 
128
CLASS="REPLACEABLE"
 
129
><I
 
130
>action</I
 
131
></TT
 
132
> [, ... ]
 
133
ALTER TABLE [ ONLY ] <TT
 
134
CLASS="REPLACEABLE"
 
135
><I
 
136
>name</I
 
137
></TT
 
138
> [ * ]
 
139
    RENAME [ COLUMN ] <TT
 
140
CLASS="REPLACEABLE"
 
141
><I
 
142
>column</I
 
143
></TT
 
144
> TO <TT
 
145
CLASS="REPLACEABLE"
 
146
><I
 
147
>new_column</I
 
148
></TT
 
149
>
 
150
ALTER TABLE <TT
 
151
CLASS="REPLACEABLE"
 
152
><I
 
153
>name</I
 
154
></TT
 
155
>
 
156
    RENAME TO <TT
 
157
CLASS="REPLACEABLE"
 
158
><I
 
159
>new_name</I
 
160
></TT
 
161
>
 
162
ALTER TABLE <TT
 
163
CLASS="REPLACEABLE"
 
164
><I
 
165
>name</I
 
166
></TT
 
167
>
 
168
    SET SCHEMA <TT
 
169
CLASS="REPLACEABLE"
 
170
><I
 
171
>new_schema</I
 
172
></TT
 
173
>
 
174
 
 
175
<SPAN
 
176
CLASS="phrase"
 
177
><SPAN
 
178
CLASS="PHRASE"
 
179
>where <TT
 
180
CLASS="REPLACEABLE"
 
181
><I
 
182
>action</I
 
183
></TT
 
184
> is one of:</SPAN
 
185
></SPAN
 
186
>
 
187
 
 
188
    ADD [ COLUMN ] <TT
 
189
CLASS="REPLACEABLE"
 
190
><I
 
191
>column</I
 
192
></TT
 
193
> <TT
 
194
CLASS="REPLACEABLE"
 
195
><I
 
196
>data_type</I
 
197
></TT
 
198
> [ COLLATE <TT
 
199
CLASS="REPLACEABLE"
 
200
><I
 
201
>collation</I
 
202
></TT
 
203
> ] [ <TT
 
204
CLASS="REPLACEABLE"
 
205
><I
 
206
>column_constraint</I
 
207
></TT
 
208
> [ ... ] ]
 
209
    DROP [ COLUMN ] [ IF EXISTS ] <TT
 
210
CLASS="REPLACEABLE"
 
211
><I
 
212
>column</I
 
213
></TT
 
214
> [ RESTRICT | CASCADE ]
 
215
    ALTER [ COLUMN ] <TT
 
216
CLASS="REPLACEABLE"
 
217
><I
 
218
>column</I
 
219
></TT
 
220
> [ SET DATA ] TYPE <TT
 
221
CLASS="REPLACEABLE"
 
222
><I
 
223
>data_type</I
 
224
></TT
 
225
> [ COLLATE <TT
 
226
CLASS="REPLACEABLE"
 
227
><I
 
228
>collation</I
 
229
></TT
 
230
> ] [ USING <TT
 
231
CLASS="REPLACEABLE"
 
232
><I
 
233
>expression</I
 
234
></TT
 
235
> ]
 
236
    ALTER [ COLUMN ] <TT
 
237
CLASS="REPLACEABLE"
 
238
><I
 
239
>column</I
 
240
></TT
 
241
> SET DEFAULT <TT
 
242
CLASS="REPLACEABLE"
 
243
><I
 
244
>expression</I
 
245
></TT
 
246
>
 
247
    ALTER [ COLUMN ] <TT
 
248
CLASS="REPLACEABLE"
 
249
><I
 
250
>column</I
 
251
></TT
 
252
> DROP DEFAULT
 
253
    ALTER [ COLUMN ] <TT
 
254
CLASS="REPLACEABLE"
 
255
><I
 
256
>column</I
 
257
></TT
 
258
> { SET | DROP } NOT NULL
 
259
    ALTER [ COLUMN ] <TT
 
260
CLASS="REPLACEABLE"
 
261
><I
 
262
>column</I
 
263
></TT
 
264
> SET STATISTICS <TT
 
265
CLASS="REPLACEABLE"
 
266
><I
 
267
>integer</I
 
268
></TT
 
269
>
 
270
    ALTER [ COLUMN ] <TT
 
271
CLASS="REPLACEABLE"
 
272
><I
 
273
>column</I
 
274
></TT
 
275
> SET ( <TT
 
276
CLASS="REPLACEABLE"
 
277
><I
 
278
>attribute_option</I
 
279
></TT
 
280
> = <TT
 
281
CLASS="REPLACEABLE"
 
282
><I
 
283
>value</I
 
284
></TT
 
285
> [, ... ] )
 
286
    ALTER [ COLUMN ] <TT
 
287
CLASS="REPLACEABLE"
 
288
><I
 
289
>column</I
 
290
></TT
 
291
> RESET ( <TT
 
292
CLASS="REPLACEABLE"
 
293
><I
 
294
>attribute_option</I
 
295
></TT
 
296
> [, ... ] )
 
297
    ALTER [ COLUMN ] <TT
 
298
CLASS="REPLACEABLE"
 
299
><I
 
300
>column</I
 
301
></TT
 
302
> SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
 
303
    ADD <TT
 
304
CLASS="REPLACEABLE"
 
305
><I
 
306
>table_constraint</I
 
307
></TT
 
308
>
 
309
    ADD <TT
 
310
CLASS="REPLACEABLE"
 
311
><I
 
312
>table_constraint_using_index</I
 
313
></TT
 
314
>
 
315
    ADD <TT
 
316
CLASS="REPLACEABLE"
 
317
><I
 
318
>table_constraint</I
 
319
></TT
 
320
> [ NOT VALID ]
 
321
    VALIDATE CONSTRAINT <TT
 
322
CLASS="REPLACEABLE"
 
323
><I
 
324
>constraint_name</I
 
325
></TT
 
326
>
 
327
    DROP CONSTRAINT [ IF EXISTS ]  <TT
 
328
CLASS="REPLACEABLE"
 
329
><I
 
330
>constraint_name</I
 
331
></TT
 
332
> [ RESTRICT | CASCADE ]
 
333
    DISABLE TRIGGER [ <TT
 
334
CLASS="REPLACEABLE"
 
335
><I
 
336
>trigger_name</I
 
337
></TT
 
338
> | ALL | USER ]
 
339
    ENABLE TRIGGER [ <TT
 
340
CLASS="REPLACEABLE"
 
341
><I
 
342
>trigger_name</I
 
343
></TT
 
344
> | ALL | USER ]
 
345
    ENABLE REPLICA TRIGGER <TT
 
346
CLASS="REPLACEABLE"
 
347
><I
 
348
>trigger_name</I
 
349
></TT
 
350
>
 
351
    ENABLE ALWAYS TRIGGER <TT
 
352
CLASS="REPLACEABLE"
 
353
><I
 
354
>trigger_name</I
 
355
></TT
 
356
>
 
357
    DISABLE RULE <TT
 
358
CLASS="REPLACEABLE"
 
359
><I
 
360
>rewrite_rule_name</I
 
361
></TT
 
362
>
 
363
    ENABLE RULE <TT
 
364
CLASS="REPLACEABLE"
 
365
><I
 
366
>rewrite_rule_name</I
 
367
></TT
 
368
>
 
369
    ENABLE REPLICA RULE <TT
 
370
CLASS="REPLACEABLE"
 
371
><I
 
372
>rewrite_rule_name</I
 
373
></TT
 
374
>
 
375
    ENABLE ALWAYS RULE <TT
 
376
CLASS="REPLACEABLE"
 
377
><I
 
378
>rewrite_rule_name</I
 
379
></TT
 
380
>
 
381
    CLUSTER ON <TT
 
382
CLASS="REPLACEABLE"
 
383
><I
 
384
>index_name</I
 
385
></TT
 
386
>
 
387
    SET WITHOUT CLUSTER
 
388
    SET WITH OIDS
 
389
    SET WITHOUT OIDS
 
390
    SET ( <TT
 
391
CLASS="REPLACEABLE"
 
392
><I
 
393
>storage_parameter</I
 
394
></TT
 
395
> = <TT
 
396
CLASS="REPLACEABLE"
 
397
><I
 
398
>value</I
 
399
></TT
 
400
> [, ... ] )
 
401
    RESET ( <TT
 
402
CLASS="REPLACEABLE"
 
403
><I
 
404
>storage_parameter</I
 
405
></TT
 
406
> [, ... ] )
 
407
    INHERIT <TT
 
408
CLASS="REPLACEABLE"
 
409
><I
 
410
>parent_table</I
 
411
></TT
 
412
>
 
413
    NO INHERIT <TT
 
414
CLASS="REPLACEABLE"
 
415
><I
 
416
>parent_table</I
 
417
></TT
 
418
>
 
419
    OF <TT
 
420
CLASS="REPLACEABLE"
 
421
><I
 
422
>type_name</I
 
423
></TT
 
424
>
 
425
    NOT OF
 
426
    OWNER TO <TT
 
427
CLASS="REPLACEABLE"
 
428
><I
 
429
>new_owner</I
 
430
></TT
 
431
>
 
432
    SET TABLESPACE <TT
 
433
CLASS="REPLACEABLE"
 
434
><I
 
435
>new_tablespace</I
 
436
></TT
 
437
>
 
438
 
 
439
<SPAN
 
440
CLASS="phrase"
 
441
><SPAN
 
442
CLASS="PHRASE"
 
443
>and <TT
 
444
CLASS="REPLACEABLE"
 
445
><I
 
446
>table_constraint_using_index</I
 
447
></TT
 
448
> is:</SPAN
 
449
></SPAN
 
450
>
 
451
 
 
452
    [ CONSTRAINT <TT
 
453
CLASS="REPLACEABLE"
 
454
><I
 
455
>constraint_name</I
 
456
></TT
 
457
> ]
 
458
    { UNIQUE | PRIMARY KEY } USING INDEX <TT
 
459
CLASS="REPLACEABLE"
 
460
><I
 
461
>index_name</I
 
462
></TT
 
463
>
 
464
    [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]</PRE
 
465
></DIV
 
466
><DIV
 
467
CLASS="REFSECT1"
 
468
><A
 
469
NAME="AEN61696"
 
470
></A
 
471
><H2
 
472
>Description</H2
 
473
><P
 
474
>   <TT
 
475
CLASS="COMMAND"
 
476
>ALTER TABLE</TT
 
477
> changes the definition of an existing table.
 
478
   There are several subforms:
 
479
 
 
480
  <P
 
481
></P
 
482
></P><DIV
 
483
CLASS="VARIABLELIST"
 
484
><DL
 
485
><DT
 
486
><TT
 
487
CLASS="LITERAL"
 
488
>ADD COLUMN</TT
 
489
></DT
 
490
><DD
 
491
><P
 
492
>      This form adds a new column to the table, using the same syntax as
 
493
      <A
 
494
HREF="sql-createtable.html"
 
495
>CREATE TABLE</A
 
496
>.
 
497
     </P
 
498
></DD
 
499
><DT
 
500
><TT
 
501
CLASS="LITERAL"
 
502
>DROP COLUMN [ IF EXISTS ]</TT
 
503
></DT
 
504
><DD
 
505
><P
 
506
>      This form drops a column from a table.  Indexes and
 
507
      table constraints involving the column will be automatically
 
508
      dropped as well.  You will need to say <TT
 
509
CLASS="LITERAL"
 
510
>CASCADE</TT
 
511
> if
 
512
      anything outside the table depends on the column, for example,
 
513
      foreign key references or views.
 
514
      If <TT
 
515
CLASS="LITERAL"
 
516
>IF EXISTS</TT
 
517
> is specified and the column
 
518
      does not exist, no error is thrown. In this case a notice
 
519
      is issued instead.
 
520
     </P
 
521
></DD
 
522
><DT
 
523
><TT
 
524
CLASS="LITERAL"
 
525
>SET DATA TYPE</TT
 
526
></DT
 
527
><DD
 
528
><P
 
529
>      This form changes the type of a column of a table. Indexes and
 
530
      simple table constraints involving the column will be automatically
 
531
      converted to use the new column type by reparsing the originally
 
532
      supplied expression.
 
533
      The optional <TT
 
534
CLASS="LITERAL"
 
535
>COLLATE</TT
 
536
> clause specifies a collation
 
537
      for the new column; if omitted, the collation is the default for the
 
538
      new column type.
 
539
      The optional <TT
 
540
CLASS="LITERAL"
 
541
>USING</TT
 
542
>
 
543
      clause specifies how to compute the new column value from the old;
 
544
      if omitted, the default conversion is the same as an assignment
 
545
      cast from old data type to new.  A  <TT
 
546
CLASS="LITERAL"
 
547
>USING</TT
 
548
>
 
549
      clause must be provided if there is no implicit or assignment
 
550
      cast from old to new type.
 
551
     </P
 
552
></DD
 
553
><DT
 
554
><TT
 
555
CLASS="LITERAL"
 
556
>SET</TT
 
557
>/<TT
 
558
CLASS="LITERAL"
 
559
>DROP DEFAULT</TT
 
560
></DT
 
561
><DD
 
562
><P
 
563
>      These forms set or remove the default value for a column.
 
564
      The default values only apply to subsequent <TT
 
565
CLASS="COMMAND"
 
566
>INSERT</TT
 
567
>
 
568
      commands; they do not cause rows already in the table to change.
 
569
      Defaults can also be created for views, in which case they are
 
570
      inserted into <TT
 
571
CLASS="COMMAND"
 
572
>INSERT</TT
 
573
> statements on the view before
 
574
      the view's <TT
 
575
CLASS="LITERAL"
 
576
>ON INSERT</TT
 
577
> rule is applied.
 
578
     </P
 
579
></DD
 
580
><DT
 
581
><TT
 
582
CLASS="LITERAL"
 
583
>SET</TT
 
584
>/<TT
 
585
CLASS="LITERAL"
 
586
>DROP NOT NULL</TT
 
587
></DT
 
588
><DD
 
589
><P
 
590
>      These forms change whether a column is marked to allow null
 
591
      values or to reject null values.  You can only use <TT
 
592
CLASS="LITERAL"
 
593
>SET
 
594
      NOT NULL</TT
 
595
> when the column contains no null values.
 
596
     </P
 
597
></DD
 
598
><DT
 
599
><TT
 
600
CLASS="LITERAL"
 
601
>SET STATISTICS</TT
 
602
></DT
 
603
><DD
 
604
><P
 
605
>      This form
 
606
      sets the per-column statistics-gathering target for subsequent
 
607
      <A
 
608
HREF="sql-analyze.html"
 
609
>ANALYZE</A
 
610
> operations.
 
611
      The target can be set in the range 0 to 10000; alternatively, set it
 
612
      to -1 to revert to using the system default statistics
 
613
      target (<A
 
614
HREF="runtime-config-query.html#GUC-DEFAULT-STATISTICS-TARGET"
 
615
>default_statistics_target</A
 
616
>).
 
617
      For more information on the use of statistics by the
 
618
      <SPAN
 
619
CLASS="PRODUCTNAME"
 
620
>PostgreSQL</SPAN
 
621
> query planner, refer to
 
622
      <A
 
623
HREF="planner-stats.html"
 
624
>Section 14.2</A
 
625
>.
 
626
     </P
 
627
></DD
 
628
><DT
 
629
><TT
 
630
CLASS="LITERAL"
 
631
>SET ( <TT
 
632
CLASS="REPLACEABLE"
 
633
><I
 
634
>attribute_option</I
 
635
></TT
 
636
> = <TT
 
637
CLASS="REPLACEABLE"
 
638
><I
 
639
>value</I
 
640
></TT
 
641
> [, ... ] )</TT
 
642
><BR><TT
 
643
CLASS="LITERAL"
 
644
>RESET ( <TT
 
645
CLASS="REPLACEABLE"
 
646
><I
 
647
>attribute_option</I
 
648
></TT
 
649
> [, ... ] )</TT
 
650
></DT
 
651
><DD
 
652
><P
 
653
>      This form sets or resets per-attribute options.  Currently, the only
 
654
      defined per-attribute options are <TT
 
655
CLASS="LITERAL"
 
656
>n_distinct</TT
 
657
> and
 
658
      <TT
 
659
CLASS="LITERAL"
 
660
>n_distinct_inherited</TT
 
661
>, which override the
 
662
      number-of-distinct-values estimates made by subsequent
 
663
      <A
 
664
HREF="sql-analyze.html"
 
665
>ANALYZE</A
 
666
>
 
667
      operations.  <TT
 
668
CLASS="LITERAL"
 
669
>n_distinct</TT
 
670
> affects the statistics for the table
 
671
      itself, while <TT
 
672
CLASS="LITERAL"
 
673
>n_distinct_inherited</TT
 
674
> affects the statistics
 
675
      gathered for the table plus its inheritance children.  When set to a
 
676
      positive value, <TT
 
677
CLASS="COMMAND"
 
678
>ANALYZE</TT
 
679
> will assume that the column contains
 
680
      exactly the specified number of distinct nonnull values.  When set to a
 
681
      negative value, which must be greater
 
682
      than or equal to -1, <TT
 
683
CLASS="COMMAND"
 
684
>ANALYZE</TT
 
685
> will assume that the number of
 
686
      distinct nonnull values in the column is linear in the size of the
 
687
      table; the exact count is to be computed by multiplying the estimated
 
688
      table size by the absolute value of the given number.  For example,
 
689
      a value of -1 implies that all values in the column are distinct, while
 
690
      a value of -0.5 implies that each value appears twice on the average.
 
691
      This can be useful when the size of the table changes over time, since
 
692
      the multiplication by the number of rows in the table is not performed
 
693
      until query planning time.  Specify a value of 0 to revert to estimating
 
694
      the number of distinct values normally.  For more information on the use
 
695
      of statistics by the <SPAN
 
696
CLASS="PRODUCTNAME"
 
697
>PostgreSQL</SPAN
 
698
> query
 
699
      planner, refer to <A
 
700
HREF="planner-stats.html"
 
701
>Section 14.2</A
 
702
>.
 
703
     </P
 
704
></DD
 
705
><DT
 
706
><TT
 
707
CLASS="LITERAL"
 
708
>SET STORAGE</TT
 
709
></DT
 
710
><DD
 
711
><P
 
712
>      This form sets the storage mode for a column. This controls whether this
 
713
      column is held inline or in a secondary <ACRONYM
 
714
CLASS="ACRONYM"
 
715
>TOAST</ACRONYM
 
716
> table, and
 
717
      whether the data
 
718
      should be compressed or not. <TT
 
719
CLASS="LITERAL"
 
720
>PLAIN</TT
 
721
> must be used
 
722
      for fixed-length values such as <TT
 
723
CLASS="TYPE"
 
724
>integer</TT
 
725
> and is
 
726
      inline, uncompressed. <TT
 
727
CLASS="LITERAL"
 
728
>MAIN</TT
 
729
> is for inline,
 
730
      compressible data. <TT
 
731
CLASS="LITERAL"
 
732
>EXTERNAL</TT
 
733
> is for external,
 
734
      uncompressed data, and <TT
 
735
CLASS="LITERAL"
 
736
>EXTENDED</TT
 
737
> is for external,
 
738
      compressed data.  <TT
 
739
CLASS="LITERAL"
 
740
>EXTENDED</TT
 
741
> is the default for most
 
742
      data types that support non-<TT
 
743
CLASS="LITERAL"
 
744
>PLAIN</TT
 
745
> storage.
 
746
      Use of <TT
 
747
CLASS="LITERAL"
 
748
>EXTERNAL</TT
 
749
> will make substring operations on
 
750
      very large <TT
 
751
CLASS="TYPE"
 
752
>text</TT
 
753
> and <TT
 
754
CLASS="TYPE"
 
755
>bytea</TT
 
756
> values run faster,
 
757
      at the penalty of increased storage space.  Note that
 
758
      <TT
 
759
CLASS="LITERAL"
 
760
>SET STORAGE</TT
 
761
> doesn't itself change anything in the table,
 
762
      it just sets the strategy to be pursued during future table updates.
 
763
      See <A
 
764
HREF="storage-toast.html"
 
765
>Section 55.2</A
 
766
> for more information.
 
767
     </P
 
768
></DD
 
769
><DT
 
770
><TT
 
771
CLASS="LITERAL"
 
772
>ADD <TT
 
773
CLASS="REPLACEABLE"
 
774
><I
 
775
>table_constraint</I
 
776
></TT
 
777
>
 
778
          [ NOT VALID ]</TT
 
779
></DT
 
780
><DD
 
781
><P
 
782
>      This form adds a new constraint to a table using the same syntax as
 
783
      <A
 
784
HREF="sql-createtable.html"
 
785
>CREATE TABLE</A
 
786
>. Newly added foreign key constraints can
 
787
      also be defined as <TT
 
788
CLASS="LITERAL"
 
789
>NOT VALID</TT
 
790
> to avoid the
 
791
      potentially lengthy initial check that must otherwise be performed.
 
792
      Constraint checks are skipped at create table time, so
 
793
      <A
 
794
HREF="sql-createtable.html"
 
795
>CREATE TABLE</A
 
796
> does not contain this option.
 
797
     </P
 
798
></DD
 
799
><DT
 
800
><TT
 
801
CLASS="LITERAL"
 
802
>VALIDATE CONSTRAINT</TT
 
803
></DT
 
804
><DD
 
805
><P
 
806
>      This form validates a foreign key constraint that was previously created
 
807
      as <TT
 
808
CLASS="LITERAL"
 
809
>NOT VALID</TT
 
810
>. Constraints already marked valid do not
 
811
      cause an error response.
 
812
     </P
 
813
></DD
 
814
><DT
 
815
><TT
 
816
CLASS="LITERAL"
 
817
>ADD <TT
 
818
CLASS="REPLACEABLE"
 
819
><I
 
820
>table_constraint_using_index</I
 
821
></TT
 
822
></TT
 
823
></DT
 
824
><DD
 
825
><P
 
826
>      This form adds a new <TT
 
827
CLASS="LITERAL"
 
828
>PRIMARY KEY</TT
 
829
> or <TT
 
830
CLASS="LITERAL"
 
831
>UNIQUE</TT
 
832
>
 
833
      constraint to a table based on an existing unique index.  All the
 
834
      columns of the index will be included in the constraint.
 
835
     </P
 
836
><P
 
837
>      The index cannot have expression columns nor be a partial index.
 
838
      Also, it must be a b-tree index with default sort ordering.  These
 
839
      restrictions ensure that the index is equivalent to one that would be
 
840
      built by a regular <TT
 
841
CLASS="LITERAL"
 
842
>ADD PRIMARY KEY</TT
 
843
> or <TT
 
844
CLASS="LITERAL"
 
845
>ADD UNIQUE</TT
 
846
>
 
847
      command.
 
848
     </P
 
849
><P
 
850
>      If <TT
 
851
CLASS="LITERAL"
 
852
>PRIMARY KEY</TT
 
853
> is specified, and the index's columns are not
 
854
      already marked <TT
 
855
CLASS="LITERAL"
 
856
>NOT NULL</TT
 
857
>, then this command will attempt to
 
858
      do <TT
 
859
CLASS="LITERAL"
 
860
>ALTER COLUMN SET NOT NULL</TT
 
861
> against each such column.
 
862
      That requires a full table scan to verify the column(s) contain no
 
863
      nulls.  In all other cases, this is a fast operation.
 
864
     </P
 
865
><P
 
866
>      If a constraint name is provided then the index will be renamed to match
 
867
      the constraint name.  Otherwise the constraint will be named the same as
 
868
      the index.
 
869
     </P
 
870
><P
 
871
>      After this command is executed, the index is <SPAN
 
872
CLASS="QUOTE"
 
873
>"owned"</SPAN
 
874
> by the
 
875
      constraint, in the same way as if the index had been built by
 
876
      a regular <TT
 
877
CLASS="LITERAL"
 
878
>ADD PRIMARY KEY</TT
 
879
> or <TT
 
880
CLASS="LITERAL"
 
881
>ADD UNIQUE</TT
 
882
>
 
883
      command.  In particular, dropping the constraint will make the index
 
884
      disappear too.
 
885
     </P
 
886
><DIV
 
887
CLASS="NOTE"
 
888
><BLOCKQUOTE
 
889
CLASS="NOTE"
 
890
><P
 
891
><B
 
892
>Note: </B
 
893
>       Adding a constraint using an existing index can be helpful in
 
894
       situations where a new constraint needs to be added without blocking
 
895
       table updates for a long time.  To do that, create the index using
 
896
       <TT
 
897
CLASS="COMMAND"
 
898
>CREATE INDEX CONCURRENTLY</TT
 
899
>, and then install it as an
 
900
       official constraint using this syntax.  See the example below.
 
901
      </P
 
902
></BLOCKQUOTE
 
903
></DIV
 
904
></DD
 
905
><DT
 
906
><TT
 
907
CLASS="LITERAL"
 
908
>DROP CONSTRAINT [ IF EXISTS ]</TT
 
909
></DT
 
910
><DD
 
911
><P
 
912
>      This form drops the specified constraint on a table.
 
913
      If <TT
 
914
CLASS="LITERAL"
 
915
>IF EXISTS</TT
 
916
> is specified and the constraint
 
917
      does not exist, no error is thrown. In this case a notice is issued instead.
 
918
     </P
 
919
></DD
 
920
><DT
 
921
><TT
 
922
CLASS="LITERAL"
 
923
>DISABLE</TT
 
924
>/<TT
 
925
CLASS="LITERAL"
 
926
>ENABLE [ REPLICA | ALWAYS ] TRIGGER</TT
 
927
></DT
 
928
><DD
 
929
><P
 
930
>      These forms configure the firing of trigger(s) belonging to the table.
 
931
      A disabled trigger is still known to the system, but is not executed
 
932
      when its triggering event occurs.  For a deferred trigger, the enable
 
933
      status is checked when the event occurs, not when the trigger function
 
934
      is actually executed.  One can disable or enable a single
 
935
      trigger specified by name, or all triggers on the table, or only
 
936
      user triggers (this option excludes internally generated constraint
 
937
      triggers such as those that are used to implement foreign key
 
938
      constraints or deferrable uniqueness and exclusion constraints).
 
939
      Disabling or enabling internally generated constraint triggers
 
940
      requires superuser privileges; it should be done with caution since
 
941
      of course the integrity of the constraint cannot be guaranteed if the
 
942
      triggers are not executed.
 
943
      The trigger firing mechanism is also affected by the configuration
 
944
      variable <A
 
945
HREF="runtime-config-client.html#GUC-SESSION-REPLICATION-ROLE"
 
946
>session_replication_role</A
 
947
>. Simply enabled
 
948
      triggers will fire when the replication role is <SPAN
 
949
CLASS="QUOTE"
 
950
>"origin"</SPAN
 
951
>
 
952
      (the default) or <SPAN
 
953
CLASS="QUOTE"
 
954
>"local"</SPAN
 
955
>. Triggers configured as <TT
 
956
CLASS="LITERAL"
 
957
>ENABLE
 
958
      REPLICA</TT
 
959
> will only fire if the session is in <SPAN
 
960
CLASS="QUOTE"
 
961
>"replica"</SPAN
 
962
>
 
963
      mode, and triggers configured as <TT
 
964
CLASS="LITERAL"
 
965
>ENABLE ALWAYS</TT
 
966
> will
 
967
      fire regardless of the current replication mode.
 
968
     </P
 
969
></DD
 
970
><DT
 
971
><TT
 
972
CLASS="LITERAL"
 
973
>DISABLE</TT
 
974
>/<TT
 
975
CLASS="LITERAL"
 
976
>ENABLE [ REPLICA | ALWAYS ] RULE</TT
 
977
></DT
 
978
><DD
 
979
><P
 
980
>      These forms configure the firing of rewrite rules belonging to the table.
 
981
      A disabled rule is still known to the system, but is not applied
 
982
      during query rewriting. The semantics are as for disabled/enabled
 
983
      triggers. This configuration is ignored for <TT
 
984
CLASS="LITERAL"
 
985
>ON SELECT</TT
 
986
> rules, which
 
987
      are always applied in order to keep views working even if the current
 
988
      session is in a non-default replication role.
 
989
     </P
 
990
></DD
 
991
><DT
 
992
><TT
 
993
CLASS="LITERAL"
 
994
>CLUSTER</TT
 
995
></DT
 
996
><DD
 
997
><P
 
998
>      This form selects the default index for future
 
999
      <A
 
1000
HREF="sql-cluster.html"
 
1001
>CLUSTER</A
 
1002
>
 
1003
      operations.  It does not actually re-cluster the table.
 
1004
     </P
 
1005
></DD
 
1006
><DT
 
1007
><TT
 
1008
CLASS="LITERAL"
 
1009
>SET WITHOUT CLUSTER</TT
 
1010
></DT
 
1011
><DD
 
1012
><P
 
1013
>      This form removes the most recently used
 
1014
      <A
 
1015
HREF="sql-cluster.html"
 
1016
>CLUSTER</A
 
1017
>
 
1018
      index specification from the table.  This affects
 
1019
      future cluster operations that don't specify an index.
 
1020
     </P
 
1021
></DD
 
1022
><DT
 
1023
><TT
 
1024
CLASS="LITERAL"
 
1025
>SET WITH OIDS</TT
 
1026
></DT
 
1027
><DD
 
1028
><P
 
1029
>      This form adds an <TT
 
1030
CLASS="LITERAL"
 
1031
>oid</TT
 
1032
> system column to the
 
1033
      table (see <A
 
1034
HREF="ddl-system-columns.html"
 
1035
>Section 5.4</A
 
1036
>).
 
1037
      It does nothing if the table already has OIDs.
 
1038
     </P
 
1039
><P
 
1040
>      Note that this is not equivalent to <TT
 
1041
CLASS="LITERAL"
 
1042
>ADD COLUMN oid oid</TT
 
1043
>;
 
1044
      that would add a normal column that happened to be named
 
1045
      <TT
 
1046
CLASS="LITERAL"
 
1047
>oid</TT
 
1048
>, not a system column.
 
1049
     </P
 
1050
></DD
 
1051
><DT
 
1052
><TT
 
1053
CLASS="LITERAL"
 
1054
>SET WITHOUT OIDS</TT
 
1055
></DT
 
1056
><DD
 
1057
><P
 
1058
>      This form removes the <TT
 
1059
CLASS="LITERAL"
 
1060
>oid</TT
 
1061
> system column from the
 
1062
      table.  This is exactly equivalent to
 
1063
      <TT
 
1064
CLASS="LITERAL"
 
1065
>DROP COLUMN oid RESTRICT</TT
 
1066
>,
 
1067
      except that it will not complain if there is already no
 
1068
      <TT
 
1069
CLASS="LITERAL"
 
1070
>oid</TT
 
1071
> column.
 
1072
     </P
 
1073
></DD
 
1074
><DT
 
1075
><TT
 
1076
CLASS="LITERAL"
 
1077
>SET ( <TT
 
1078
CLASS="REPLACEABLE"
 
1079
><I
 
1080
>storage_parameter</I
 
1081
></TT
 
1082
> = <TT
 
1083
CLASS="REPLACEABLE"
 
1084
><I
 
1085
>value</I
 
1086
></TT
 
1087
> [, ... ] )</TT
 
1088
></DT
 
1089
><DD
 
1090
><P
 
1091
>      This form changes one or more storage parameters for the table.  See
 
1092
      <A
 
1093
HREF="sql-createtable.html#SQL-CREATETABLE-STORAGE-PARAMETERS"
 
1094
><I
 
1095
>Storage Parameters</I
 
1096
></A
 
1097
>
 
1098
      for details on the available parameters.  Note that the table contents
 
1099
      will not be modified immediately by this command; depending on the
 
1100
      parameter you might need to rewrite the table to get the desired effects.
 
1101
      That can be done with <A
 
1102
HREF="sql-vacuum.html"
 
1103
>VACUUM
 
1104
      FULL</A
 
1105
>, <A
 
1106
HREF="sql-cluster.html"
 
1107
>CLUSTER</A
 
1108
> or one of the forms
 
1109
      of <TT
 
1110
CLASS="COMMAND"
 
1111
>ALTER TABLE</TT
 
1112
> that forces a table rewrite.
 
1113
     </P
 
1114
><DIV
 
1115
CLASS="NOTE"
 
1116
><BLOCKQUOTE
 
1117
CLASS="NOTE"
 
1118
><P
 
1119
><B
 
1120
>Note: </B
 
1121
>       While <TT
 
1122
CLASS="COMMAND"
 
1123
>CREATE TABLE</TT
 
1124
> allows <TT
 
1125
CLASS="LITERAL"
 
1126
>OIDS</TT
 
1127
> to be specified
 
1128
       in the <TT
 
1129
CLASS="LITERAL"
 
1130
>WITH (<TT
 
1131
CLASS="REPLACEABLE"
 
1132
><I
 
1133
>storage_parameter</I
 
1134
></TT
 
1135
>)</TT
 
1136
> syntax,
 
1137
       <TT
 
1138
CLASS="COMMAND"
 
1139
>ALTER TABLE</TT
 
1140
> does not treat <TT
 
1141
CLASS="LITERAL"
 
1142
>OIDS</TT
 
1143
> as a
 
1144
       storage parameter.  Instead use the <TT
 
1145
CLASS="LITERAL"
 
1146
>SET WITH OIDS</TT
 
1147
>
 
1148
       and <TT
 
1149
CLASS="LITERAL"
 
1150
>SET WITHOUT OIDS</TT
 
1151
> forms to change OID status.
 
1152
      </P
 
1153
></BLOCKQUOTE
 
1154
></DIV
 
1155
></DD
 
1156
><DT
 
1157
><TT
 
1158
CLASS="LITERAL"
 
1159
>RESET ( <TT
 
1160
CLASS="REPLACEABLE"
 
1161
><I
 
1162
>storage_parameter</I
 
1163
></TT
 
1164
> [, ... ] )</TT
 
1165
></DT
 
1166
><DD
 
1167
><P
 
1168
>      This form resets one or more storage parameters to their
 
1169
      defaults.  As with <TT
 
1170
CLASS="LITERAL"
 
1171
>SET</TT
 
1172
>, a table rewrite might be
 
1173
      needed to update the table entirely.
 
1174
     </P
 
1175
></DD
 
1176
><DT
 
1177
><TT
 
1178
CLASS="LITERAL"
 
1179
>INHERIT <TT
 
1180
CLASS="REPLACEABLE"
 
1181
><I
 
1182
>parent_table</I
 
1183
></TT
 
1184
></TT
 
1185
></DT
 
1186
><DD
 
1187
><P
 
1188
>      This form adds the target table as a new child of the specified parent
 
1189
      table.  Subsequently, queries against the parent will include records
 
1190
      of the target table.  To be added as a child, the target table must
 
1191
      already contain all the same columns as the parent (it could have
 
1192
      additional columns, too).  The columns must have matching data types,
 
1193
      and if they have <TT
 
1194
CLASS="LITERAL"
 
1195
>NOT NULL</TT
 
1196
> constraints in the parent
 
1197
      then they must also have <TT
 
1198
CLASS="LITERAL"
 
1199
>NOT NULL</TT
 
1200
> constraints in the
 
1201
      child.
 
1202
     </P
 
1203
><P
 
1204
>      There must also be matching child-table constraints for all
 
1205
      <TT
 
1206
CLASS="LITERAL"
 
1207
>CHECK</TT
 
1208
> constraints of the parent. Currently
 
1209
      <TT
 
1210
CLASS="LITERAL"
 
1211
>UNIQUE</TT
 
1212
>, <TT
 
1213
CLASS="LITERAL"
 
1214
>PRIMARY KEY</TT
 
1215
>, and
 
1216
      <TT
 
1217
CLASS="LITERAL"
 
1218
>FOREIGN KEY</TT
 
1219
> constraints are not considered, but
 
1220
      this might change in the future.
 
1221
     </P
 
1222
></DD
 
1223
><DT
 
1224
><TT
 
1225
CLASS="LITERAL"
 
1226
>NO INHERIT <TT
 
1227
CLASS="REPLACEABLE"
 
1228
><I
 
1229
>parent_table</I
 
1230
></TT
 
1231
></TT
 
1232
></DT
 
1233
><DD
 
1234
><P
 
1235
>      This form removes the target table from the list of children of the
 
1236
      specified parent table.
 
1237
      Queries against the parent table will no longer include records drawn
 
1238
      from the target table.
 
1239
     </P
 
1240
></DD
 
1241
><DT
 
1242
><TT
 
1243
CLASS="LITERAL"
 
1244
>OF <TT
 
1245
CLASS="REPLACEABLE"
 
1246
><I
 
1247
>type_name</I
 
1248
></TT
 
1249
></TT
 
1250
></DT
 
1251
><DD
 
1252
><P
 
1253
>      This form links the table to a composite type as though <TT
 
1254
CLASS="COMMAND"
 
1255
>CREATE
 
1256
      TABLE OF</TT
 
1257
> had formed it.  The table's list of column names and types
 
1258
      must precisely match that of the composite type; the presence of
 
1259
      an <TT
 
1260
CLASS="LITERAL"
 
1261
>oid</TT
 
1262
> system column is permitted to differ.  The table must
 
1263
      not inherit from any other table.  These restrictions ensure
 
1264
      that <TT
 
1265
CLASS="COMMAND"
 
1266
>CREATE TABLE OF</TT
 
1267
> would permit an equivalent table
 
1268
      definition.
 
1269
     </P
 
1270
></DD
 
1271
><DT
 
1272
><TT
 
1273
CLASS="LITERAL"
 
1274
>NOT OF</TT
 
1275
></DT
 
1276
><DD
 
1277
><P
 
1278
>      This form dissociates a typed table from its type.
 
1279
     </P
 
1280
></DD
 
1281
><DT
 
1282
><TT
 
1283
CLASS="LITERAL"
 
1284
>OWNER</TT
 
1285
></DT
 
1286
><DD
 
1287
><P
 
1288
>      This form changes the owner of the table, sequence, or view to the
 
1289
      specified user.
 
1290
     </P
 
1291
></DD
 
1292
><DT
 
1293
><TT
 
1294
CLASS="LITERAL"
 
1295
>SET TABLESPACE</TT
 
1296
></DT
 
1297
><DD
 
1298
><P
 
1299
>      This form changes the table's tablespace to the specified tablespace and
 
1300
      moves the data file(s) associated with the table to the new tablespace.
 
1301
      Indexes on the table, if any, are not moved; but they can be moved
 
1302
      separately with additional <TT
 
1303
CLASS="LITERAL"
 
1304
>SET TABLESPACE</TT
 
1305
> commands.
 
1306
      See also
 
1307
      <A
 
1308
HREF="sql-createtablespace.html"
 
1309
>CREATE TABLESPACE</A
 
1310
>.
 
1311
     </P
 
1312
></DD
 
1313
><DT
 
1314
><TT
 
1315
CLASS="LITERAL"
 
1316
>RENAME</TT
 
1317
></DT
 
1318
><DD
 
1319
><P
 
1320
>      The <TT
 
1321
CLASS="LITERAL"
 
1322
>RENAME</TT
 
1323
> forms change the name of a table
 
1324
      (or an index, sequence, or view) or the name of an individual column in
 
1325
      a table. There is no effect on the stored data.
 
1326
     </P
 
1327
></DD
 
1328
><DT
 
1329
><TT
 
1330
CLASS="LITERAL"
 
1331
>SET SCHEMA</TT
 
1332
></DT
 
1333
><DD
 
1334
><P
 
1335
>      This form moves the table into another schema.  Associated indexes,
 
1336
      constraints, and sequences owned by table columns are moved as well.
 
1337
     </P
 
1338
></DD
 
1339
></DL
 
1340
></DIV
 
1341
><P>
 
1342
  </P
 
1343
><P
 
1344
>   All the actions except <TT
 
1345
CLASS="LITERAL"
 
1346
>RENAME</TT
 
1347
> and <TT
 
1348
CLASS="LITERAL"
 
1349
>SET SCHEMA</TT
 
1350
>
 
1351
   can be combined into
 
1352
   a list of multiple alterations to apply in parallel.  For example, it
 
1353
   is possible to add several columns and/or alter the type of several
 
1354
   columns in a single command.  This is particularly useful with large
 
1355
   tables, since only one pass over the table need be made.
 
1356
  </P
 
1357
><P
 
1358
>   You must own the table to use <TT
 
1359
CLASS="COMMAND"
 
1360
>ALTER TABLE</TT
 
1361
>.
 
1362
   To change the schema of a table, you must also have
 
1363
   <TT
 
1364
CLASS="LITERAL"
 
1365
>CREATE</TT
 
1366
> privilege on the new schema.
 
1367
   To add the table as a new child of a parent table, you must own the
 
1368
   parent table as well.
 
1369
   To alter the owner, you must also be a direct or indirect member of the new
 
1370
   owning role, and that role must have <TT
 
1371
CLASS="LITERAL"
 
1372
>CREATE</TT
 
1373
> privilege on
 
1374
   the table's schema.  (These restrictions enforce that altering the owner
 
1375
   doesn't do anything you couldn't do by dropping and recreating the table.
 
1376
   However, a superuser can alter ownership of any table anyway.)
 
1377
  </P
 
1378
></DIV
 
1379
><DIV
 
1380
CLASS="REFSECT1"
 
1381
><A
 
1382
NAME="AEN61971"
 
1383
></A
 
1384
><H2
 
1385
>Parameters</H2
 
1386
><P
 
1387
></P
 
1388
><DIV
 
1389
CLASS="VARIABLELIST"
 
1390
><DL
 
1391
><DT
 
1392
><TT
 
1393
CLASS="REPLACEABLE"
 
1394
><I
 
1395
>name</I
 
1396
></TT
 
1397
></DT
 
1398
><DD
 
1399
><P
 
1400
>        The name (possibly schema-qualified) of an existing table to
 
1401
        alter. If <TT
 
1402
CLASS="LITERAL"
 
1403
>ONLY</TT
 
1404
> is specified, only that table is
 
1405
        altered. If <TT
 
1406
CLASS="LITERAL"
 
1407
>ONLY</TT
 
1408
> is not specified, the table and any
 
1409
        descendant tables are altered.
 
1410
       </P
 
1411
></DD
 
1412
><DT
 
1413
><TT
 
1414
CLASS="REPLACEABLE"
 
1415
><I
 
1416
>column</I
 
1417
></TT
 
1418
></DT
 
1419
><DD
 
1420
><P
 
1421
>        Name of a new or existing column.
 
1422
       </P
 
1423
></DD
 
1424
><DT
 
1425
><TT
 
1426
CLASS="REPLACEABLE"
 
1427
><I
 
1428
>new_column</I
 
1429
></TT
 
1430
></DT
 
1431
><DD
 
1432
><P
 
1433
>        New name for an existing column.
 
1434
       </P
 
1435
></DD
 
1436
><DT
 
1437
><TT
 
1438
CLASS="REPLACEABLE"
 
1439
><I
 
1440
>new_name</I
 
1441
></TT
 
1442
></DT
 
1443
><DD
 
1444
><P
 
1445
>        New name for the table.
 
1446
       </P
 
1447
></DD
 
1448
><DT
 
1449
><TT
 
1450
CLASS="REPLACEABLE"
 
1451
><I
 
1452
>type</I
 
1453
></TT
 
1454
></DT
 
1455
><DD
 
1456
><P
 
1457
>        Data type of the new column, or new data type for an existing
 
1458
        column.
 
1459
       </P
 
1460
></DD
 
1461
><DT
 
1462
><TT
 
1463
CLASS="REPLACEABLE"
 
1464
><I
 
1465
>table_constraint</I
 
1466
></TT
 
1467
></DT
 
1468
><DD
 
1469
><P
 
1470
>        New table constraint for the table.
 
1471
       </P
 
1472
></DD
 
1473
><DT
 
1474
><TT
 
1475
CLASS="REPLACEABLE"
 
1476
><I
 
1477
>constraint_name</I
 
1478
></TT
 
1479
></DT
 
1480
><DD
 
1481
><P
 
1482
>        Name of an existing constraint to drop.
 
1483
       </P
 
1484
></DD
 
1485
><DT
 
1486
><TT
 
1487
CLASS="LITERAL"
 
1488
>CASCADE</TT
 
1489
></DT
 
1490
><DD
 
1491
><P
 
1492
>        Automatically drop objects that depend on the dropped column
 
1493
        or constraint (for example, views referencing the column).
 
1494
       </P
 
1495
></DD
 
1496
><DT
 
1497
><TT
 
1498
CLASS="LITERAL"
 
1499
>RESTRICT</TT
 
1500
></DT
 
1501
><DD
 
1502
><P
 
1503
>        Refuse to drop the column or constraint if there are any dependent
 
1504
        objects. This is the default behavior.
 
1505
       </P
 
1506
></DD
 
1507
><DT
 
1508
><TT
 
1509
CLASS="REPLACEABLE"
 
1510
><I
 
1511
>trigger_name</I
 
1512
></TT
 
1513
></DT
 
1514
><DD
 
1515
><P
 
1516
>        Name of a single trigger to disable or enable.
 
1517
       </P
 
1518
></DD
 
1519
><DT
 
1520
><TT
 
1521
CLASS="LITERAL"
 
1522
>ALL</TT
 
1523
></DT
 
1524
><DD
 
1525
><P
 
1526
>        Disable or enable all triggers belonging to the table.
 
1527
        (This requires superuser privilege if any of the triggers are
 
1528
        internally generated constraint triggers such as those that are used
 
1529
        to implement foreign key constraints or deferrable uniqueness and
 
1530
        exclusion constraints.)
 
1531
       </P
 
1532
></DD
 
1533
><DT
 
1534
><TT
 
1535
CLASS="LITERAL"
 
1536
>USER</TT
 
1537
></DT
 
1538
><DD
 
1539
><P
 
1540
>        Disable or enable all triggers belonging to the table except for
 
1541
        internally generated constraint triggers such as those that are used
 
1542
        to implement foreign key constraints or deferrable uniqueness and
 
1543
        exclusion constraints.
 
1544
       </P
 
1545
></DD
 
1546
><DT
 
1547
><TT
 
1548
CLASS="REPLACEABLE"
 
1549
><I
 
1550
>index_name</I
 
1551
></TT
 
1552
></DT
 
1553
><DD
 
1554
><P
 
1555
>        The index name on which the table should be marked for clustering.
 
1556
       </P
 
1557
></DD
 
1558
><DT
 
1559
><TT
 
1560
CLASS="REPLACEABLE"
 
1561
><I
 
1562
>storage_parameter</I
 
1563
></TT
 
1564
></DT
 
1565
><DD
 
1566
><P
 
1567
>        The name of a table storage parameter.
 
1568
       </P
 
1569
></DD
 
1570
><DT
 
1571
><TT
 
1572
CLASS="REPLACEABLE"
 
1573
><I
 
1574
>value</I
 
1575
></TT
 
1576
></DT
 
1577
><DD
 
1578
><P
 
1579
>        The new value for a table storage parameter.
 
1580
        This might be a number or a word depending on the parameter.
 
1581
       </P
 
1582
></DD
 
1583
><DT
 
1584
><TT
 
1585
CLASS="REPLACEABLE"
 
1586
><I
 
1587
>parent_table</I
 
1588
></TT
 
1589
></DT
 
1590
><DD
 
1591
><P
 
1592
>        A parent table to associate or de-associate with this table.
 
1593
       </P
 
1594
></DD
 
1595
><DT
 
1596
><TT
 
1597
CLASS="REPLACEABLE"
 
1598
><I
 
1599
>new_owner</I
 
1600
></TT
 
1601
></DT
 
1602
><DD
 
1603
><P
 
1604
>        The user name of the new owner of the table.
 
1605
       </P
 
1606
></DD
 
1607
><DT
 
1608
><TT
 
1609
CLASS="REPLACEABLE"
 
1610
><I
 
1611
>new_tablespace</I
 
1612
></TT
 
1613
></DT
 
1614
><DD
 
1615
><P
 
1616
>        The name of the tablespace to which the table will be moved.
 
1617
       </P
 
1618
></DD
 
1619
><DT
 
1620
><TT
 
1621
CLASS="REPLACEABLE"
 
1622
><I
 
1623
>new_schema</I
 
1624
></TT
 
1625
></DT
 
1626
><DD
 
1627
><P
 
1628
>        The name of the schema to which the table will be moved.
 
1629
       </P
 
1630
></DD
 
1631
></DL
 
1632
></DIV
 
1633
></DIV
 
1634
><DIV
 
1635
CLASS="REFSECT1"
 
1636
><A
 
1637
NAME="AEN62071"
 
1638
></A
 
1639
><H2
 
1640
>Notes</H2
 
1641
><P
 
1642
>    The key word <TT
 
1643
CLASS="LITERAL"
 
1644
>COLUMN</TT
 
1645
> is noise and can be omitted.
 
1646
   </P
 
1647
><P
 
1648
>    When a column is added with <TT
 
1649
CLASS="LITERAL"
 
1650
>ADD COLUMN</TT
 
1651
>, all existing
 
1652
    rows in the table are initialized with the column's default value
 
1653
    (NULL if no <TT
 
1654
CLASS="LITERAL"
 
1655
>DEFAULT</TT
 
1656
> clause is specified).
 
1657
   </P
 
1658
><P
 
1659
>    Adding a column with a non-null default or changing the type of an
 
1660
    existing column will require the entire table and indexes to be rewritten.
 
1661
    As an exception, if the <TT
 
1662
CLASS="LITERAL"
 
1663
>USING</TT
 
1664
> clause does not change the column
 
1665
    contents and the old type is either binary coercible to the new type or
 
1666
    an unconstrained domain over the new type, a table rewrite is not needed,
 
1667
    but any indexes on the affected columns must still be rebuilt.  Adding or
 
1668
    removing a system <TT
 
1669
CLASS="LITERAL"
 
1670
>oid</TT
 
1671
> column also requires rewriting the entire
 
1672
    table.  Table and/or index rebuilds may take a significant amount of time
 
1673
    for a large table; and will temporarily require as much as double the disk
 
1674
    space.
 
1675
   </P
 
1676
><P
 
1677
>    Adding a <TT
 
1678
CLASS="LITERAL"
 
1679
>CHECK</TT
 
1680
> or <TT
 
1681
CLASS="LITERAL"
 
1682
>NOT NULL</TT
 
1683
> constraint requires
 
1684
    scanning the table to verify that existing rows meet the constraint.
 
1685
   </P
 
1686
><P
 
1687
>    The main reason for providing the option to specify multiple changes
 
1688
    in a single <TT
 
1689
CLASS="COMMAND"
 
1690
>ALTER TABLE</TT
 
1691
> is that multiple table scans or
 
1692
    rewrites can thereby be combined into a single pass over the table.
 
1693
   </P
 
1694
><P
 
1695
>    The <TT
 
1696
CLASS="LITERAL"
 
1697
>DROP COLUMN</TT
 
1698
> form does not physically remove
 
1699
    the column, but simply makes it invisible to SQL operations.  Subsequent
 
1700
    insert and update operations in the table will store a null value for the
 
1701
    column. Thus, dropping a column is quick but it will not immediately
 
1702
    reduce the on-disk size of your table, as the space occupied
 
1703
    by the dropped column is not reclaimed.  The space will be
 
1704
    reclaimed over time as existing rows are updated.  (These statements do
 
1705
    not apply when dropping the system <TT
 
1706
CLASS="LITERAL"
 
1707
>oid</TT
 
1708
> column; that is done
 
1709
    with an immediate rewrite.)
 
1710
   </P
 
1711
><P
 
1712
>    To force an immediate rewrite of the table, you can use
 
1713
    <A
 
1714
HREF="sql-vacuum.html"
 
1715
>VACUUM FULL</A
 
1716
>, <A
 
1717
HREF="sql-cluster.html"
 
1718
>CLUSTER</A
 
1719
>
 
1720
    or one of the forms of ALTER TABLE that forces a rewrite.  This results in
 
1721
    no semantically-visible change in the table, but gets rid of
 
1722
    no-longer-useful data.
 
1723
   </P
 
1724
><P
 
1725
>    The <TT
 
1726
CLASS="LITERAL"
 
1727
>USING</TT
 
1728
> option of <TT
 
1729
CLASS="LITERAL"
 
1730
>SET DATA TYPE</TT
 
1731
> can actually
 
1732
    specify any expression involving the old values of the row; that is, it
 
1733
    can refer to other columns as well as the one being converted.  This allows
 
1734
    very general conversions to be done with the <TT
 
1735
CLASS="LITERAL"
 
1736
>SET DATA TYPE</TT
 
1737
>
 
1738
    syntax.  Because of this flexibility, the <TT
 
1739
CLASS="LITERAL"
 
1740
>USING</TT
 
1741
>
 
1742
    expression is not applied to the column's default value (if any); the
 
1743
    result might not be a constant expression as required for a default.
 
1744
    This means that when there is no implicit or assignment cast from old to
 
1745
    new type, <TT
 
1746
CLASS="LITERAL"
 
1747
>SET DATA TYPE</TT
 
1748
> might fail to convert the default even
 
1749
    though a <TT
 
1750
CLASS="LITERAL"
 
1751
>USING</TT
 
1752
> clause is supplied.  In such cases,
 
1753
    drop the default with <TT
 
1754
CLASS="LITERAL"
 
1755
>DROP DEFAULT</TT
 
1756
>, perform the <TT
 
1757
CLASS="LITERAL"
 
1758
>ALTER
 
1759
    TYPE</TT
 
1760
>, and then use <TT
 
1761
CLASS="LITERAL"
 
1762
>SET DEFAULT</TT
 
1763
> to add a suitable new
 
1764
    default.  Similar considerations apply to indexes and constraints involving
 
1765
    the column.
 
1766
   </P
 
1767
><P
 
1768
>    If a table has any descendant tables, it is not permitted to add,
 
1769
    rename, or change the type of a column in the parent table without doing
 
1770
    the same to the descendants.  That is, <TT
 
1771
CLASS="COMMAND"
 
1772
>ALTER TABLE ONLY</TT
 
1773
>
 
1774
    will be rejected.  This ensures that the descendants always have
 
1775
    columns matching the parent.
 
1776
   </P
 
1777
><P
 
1778
>    A recursive <TT
 
1779
CLASS="LITERAL"
 
1780
>DROP COLUMN</TT
 
1781
> operation will remove a
 
1782
    descendant table's column only if the descendant does not inherit
 
1783
    that column from any other parents and never had an independent
 
1784
    definition of the column.  A nonrecursive <TT
 
1785
CLASS="LITERAL"
 
1786
>DROP
 
1787
    COLUMN</TT
 
1788
> (i.e., <TT
 
1789
CLASS="COMMAND"
 
1790
>ALTER TABLE ONLY ... DROP
 
1791
    COLUMN</TT
 
1792
>) never removes any descendant columns, but
 
1793
    instead marks them as independently defined rather than inherited.
 
1794
   </P
 
1795
><P
 
1796
>    The <TT
 
1797
CLASS="LITERAL"
 
1798
>TRIGGER</TT
 
1799
>, <TT
 
1800
CLASS="LITERAL"
 
1801
>CLUSTER</TT
 
1802
>, <TT
 
1803
CLASS="LITERAL"
 
1804
>OWNER</TT
 
1805
>,
 
1806
    and <TT
 
1807
CLASS="LITERAL"
 
1808
>TABLESPACE</TT
 
1809
> actions never recurse to descendant tables;
 
1810
    that is, they always act as though <TT
 
1811
CLASS="LITERAL"
 
1812
>ONLY</TT
 
1813
> were specified.
 
1814
    Adding a constraint can recurse only for <TT
 
1815
CLASS="LITERAL"
 
1816
>CHECK</TT
 
1817
> constraints,
 
1818
    and is required to do so for such constraints.
 
1819
   </P
 
1820
><P
 
1821
>    Changing any part of a system catalog table is not permitted.
 
1822
   </P
 
1823
><P
 
1824
>    Refer to <A
 
1825
HREF="sql-createtable.html"
 
1826
>CREATE TABLE</A
 
1827
> for a further description of valid
 
1828
    parameters. <A
 
1829
HREF="ddl.html"
 
1830
>Chapter 5</A
 
1831
> has further information on
 
1832
    inheritance.
 
1833
   </P
 
1834
></DIV
 
1835
><DIV
 
1836
CLASS="REFSECT1"
 
1837
><A
 
1838
NAME="AEN62119"
 
1839
></A
 
1840
><H2
 
1841
>Examples</H2
 
1842
><P
 
1843
>   To add a column of type <TT
 
1844
CLASS="TYPE"
 
1845
>varchar</TT
 
1846
> to a table:
 
1847
</P><PRE
 
1848
CLASS="PROGRAMLISTING"
 
1849
>ALTER TABLE distributors ADD COLUMN address varchar(30);</PRE
 
1850
><P>
 
1851
  </P
 
1852
><P
 
1853
>   To drop a column from a table:
 
1854
</P><PRE
 
1855
CLASS="PROGRAMLISTING"
 
1856
>ALTER TABLE distributors DROP COLUMN address RESTRICT;</PRE
 
1857
><P>
 
1858
  </P
 
1859
><P
 
1860
>   To change the types of two existing columns in one operation:
 
1861
</P><PRE
 
1862
CLASS="PROGRAMLISTING"
 
1863
>ALTER TABLE distributors
 
1864
    ALTER COLUMN address TYPE varchar(80),
 
1865
    ALTER COLUMN name TYPE varchar(100);</PRE
 
1866
><P>
 
1867
  </P
 
1868
><P
 
1869
>   To change an integer column containing UNIX timestamps to <TT
 
1870
CLASS="TYPE"
 
1871
>timestamp
 
1872
   with time zone</TT
 
1873
> via a <TT
 
1874
CLASS="LITERAL"
 
1875
>USING</TT
 
1876
> clause:
 
1877
</P><PRE
 
1878
CLASS="PROGRAMLISTING"
 
1879
>ALTER TABLE foo
 
1880
    ALTER COLUMN foo_timestamp SET DATA TYPE timestamp with time zone
 
1881
    USING
 
1882
        timestamp with time zone 'epoch' + foo_timestamp * interval '1 second';</PRE
 
1883
><P>
 
1884
  </P
 
1885
><P
 
1886
>   The same, when the column has a default expression that won't automatically
 
1887
   cast to the new data type:
 
1888
</P><PRE
 
1889
CLASS="PROGRAMLISTING"
 
1890
>ALTER TABLE foo
 
1891
    ALTER COLUMN foo_timestamp DROP DEFAULT,
 
1892
    ALTER COLUMN foo_timestamp TYPE timestamp with time zone
 
1893
    USING
 
1894
        timestamp with time zone 'epoch' + foo_timestamp * interval '1 second',
 
1895
    ALTER COLUMN foo_timestamp SET DEFAULT now();</PRE
 
1896
><P>
 
1897
  </P
 
1898
><P
 
1899
>   To rename an existing column:
 
1900
</P><PRE
 
1901
CLASS="PROGRAMLISTING"
 
1902
>ALTER TABLE distributors RENAME COLUMN address TO city;</PRE
 
1903
><P>
 
1904
  </P
 
1905
><P
 
1906
>   To rename an existing table:
 
1907
</P><PRE
 
1908
CLASS="PROGRAMLISTING"
 
1909
>ALTER TABLE distributors RENAME TO suppliers;</PRE
 
1910
><P>
 
1911
  </P
 
1912
><P
 
1913
>   To add a not-null constraint to a column:
 
1914
</P><PRE
 
1915
CLASS="PROGRAMLISTING"
 
1916
>ALTER TABLE distributors ALTER COLUMN street SET NOT NULL;</PRE
 
1917
><P>
 
1918
   To remove a not-null constraint from a column:
 
1919
</P><PRE
 
1920
CLASS="PROGRAMLISTING"
 
1921
>ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;</PRE
 
1922
><P>
 
1923
  </P
 
1924
><P
 
1925
>   To add a check constraint to a table and all its children:
 
1926
</P><PRE
 
1927
CLASS="PROGRAMLISTING"
 
1928
>ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);</PRE
 
1929
><P>
 
1930
  </P
 
1931
><P
 
1932
>   To remove a check constraint from a table and all its children:
 
1933
</P><PRE
 
1934
CLASS="PROGRAMLISTING"
 
1935
>ALTER TABLE distributors DROP CONSTRAINT zipchk;</PRE
 
1936
><P>
 
1937
  </P
 
1938
><P
 
1939
>   To remove a check constraint from a table only:
 
1940
</P><PRE
 
1941
CLASS="PROGRAMLISTING"
 
1942
>ALTER TABLE ONLY distributors DROP CONSTRAINT zipchk;</PRE
 
1943
><P>
 
1944
   (The check constraint remains in place for any child tables.)
 
1945
  </P
 
1946
><P
 
1947
>   To add a foreign key constraint to a table:
 
1948
</P><PRE
 
1949
CLASS="PROGRAMLISTING"
 
1950
>ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) MATCH FULL;</PRE
 
1951
><P>
 
1952
  </P
 
1953
><P
 
1954
>   To add a (multicolumn) unique constraint to a table:
 
1955
</P><PRE
 
1956
CLASS="PROGRAMLISTING"
 
1957
>ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);</PRE
 
1958
><P>
 
1959
  </P
 
1960
><P
 
1961
>   To add an automatically named primary key constraint to a table, noting
 
1962
   that a table can only ever have one primary key:
 
1963
</P><PRE
 
1964
CLASS="PROGRAMLISTING"
 
1965
>ALTER TABLE distributors ADD PRIMARY KEY (dist_id);</PRE
 
1966
><P>
 
1967
  </P
 
1968
><P
 
1969
>   To move a table to a different tablespace:
 
1970
</P><PRE
 
1971
CLASS="PROGRAMLISTING"
 
1972
>ALTER TABLE distributors SET TABLESPACE fasttablespace;</PRE
 
1973
><P>
 
1974
  </P
 
1975
><P
 
1976
>   To move a table to a different schema:
 
1977
</P><PRE
 
1978
CLASS="PROGRAMLISTING"
 
1979
>ALTER TABLE myschema.distributors SET SCHEMA yourschema;</PRE
 
1980
><P>
 
1981
  </P
 
1982
><P
 
1983
>   To recreate a primary key constraint, without blocking updates while the
 
1984
   index is rebuilt:
 
1985
</P><PRE
 
1986
CLASS="PROGRAMLISTING"
 
1987
>CREATE UNIQUE INDEX CONCURRENTLY dist_id_temp_idx ON distributors (dist_id);
 
1988
ALTER TABLE distributors DROP CONSTRAINT distributors_pkey,
 
1989
    ADD CONSTRAINT distributors_pkey PRIMARY KEY USING INDEX dist_id_temp_idx;</PRE
 
1990
><P>
 
1991
  </P
 
1992
></DIV
 
1993
><DIV
 
1994
CLASS="REFSECT1"
 
1995
><A
 
1996
NAME="AEN62159"
 
1997
></A
 
1998
><H2
 
1999
>Compatibility</H2
 
2000
><P
 
2001
>   The forms <TT
 
2002
CLASS="LITERAL"
 
2003
>ADD</TT
 
2004
> (without <TT
 
2005
CLASS="LITERAL"
 
2006
>USING INDEX</TT
 
2007
>),
 
2008
   <TT
 
2009
CLASS="LITERAL"
 
2010
>DROP</TT
 
2011
>, <TT
 
2012
CLASS="LITERAL"
 
2013
>SET DEFAULT</TT
 
2014
>,
 
2015
   and <TT
 
2016
CLASS="LITERAL"
 
2017
>SET DATA TYPE</TT
 
2018
> (without <TT
 
2019
CLASS="LITERAL"
 
2020
>USING</TT
 
2021
>)
 
2022
   conform with the SQL standard.  The other forms are
 
2023
   <SPAN
 
2024
CLASS="PRODUCTNAME"
 
2025
>PostgreSQL</SPAN
 
2026
> extensions of the SQL standard.
 
2027
   Also, the ability to specify more than one manipulation in a single
 
2028
   <TT
 
2029
CLASS="COMMAND"
 
2030
>ALTER TABLE</TT
 
2031
> command is an extension.
 
2032
  </P
 
2033
><P
 
2034
>   <TT
 
2035
CLASS="COMMAND"
 
2036
>ALTER TABLE DROP COLUMN</TT
 
2037
> can be used to drop the only
 
2038
   column of a table, leaving a zero-column table.  This is an
 
2039
   extension of SQL, which disallows zero-column tables.
 
2040
  </P
 
2041
></DIV
 
2042
><DIV
 
2043
CLASS="REFSECT1"
 
2044
><A
 
2045
NAME="AEN62172"
 
2046
></A
 
2047
><H2
 
2048
>See Also</H2
 
2049
><A
 
2050
HREF="sql-createtable.html"
 
2051
>CREATE TABLE</A
 
2052
></DIV
 
2053
><DIV
 
2054
CLASS="NAVFOOTER"
 
2055
><HR
 
2056
ALIGN="LEFT"
 
2057
WIDTH="100%"><TABLE
 
2058
SUMMARY="Footer navigation table"
 
2059
WIDTH="100%"
 
2060
BORDER="0"
 
2061
CELLPADDING="0"
 
2062
CELLSPACING="0"
 
2063
><TR
 
2064
><TD
 
2065
WIDTH="33%"
 
2066
ALIGN="left"
 
2067
VALIGN="top"
 
2068
><A
 
2069
HREF="sql-alterserver.html"
 
2070
ACCESSKEY="P"
 
2071
>Prev</A
 
2072
></TD
 
2073
><TD
 
2074
WIDTH="34%"
 
2075
ALIGN="center"
 
2076
VALIGN="top"
 
2077
><A
 
2078
HREF="index.html"
 
2079
ACCESSKEY="H"
 
2080
>Home</A
 
2081
></TD
 
2082
><TD
 
2083
WIDTH="33%"
 
2084
ALIGN="right"
 
2085
VALIGN="top"
 
2086
><A
 
2087
HREF="sql-altertablespace.html"
 
2088
ACCESSKEY="N"
 
2089
>Next</A
 
2090
></TD
 
2091
></TR
 
2092
><TR
 
2093
><TD
 
2094
WIDTH="33%"
 
2095
ALIGN="left"
 
2096
VALIGN="top"
 
2097
>ALTER SERVER</TD
 
2098
><TD
 
2099
WIDTH="34%"
 
2100
ALIGN="center"
 
2101
VALIGN="top"
 
2102
><A
 
2103
HREF="sql-commands.html"
 
2104
ACCESSKEY="U"
 
2105
>Up</A
 
2106
></TD
 
2107
><TD
 
2108
WIDTH="33%"
 
2109
ALIGN="right"
 
2110
VALIGN="top"
 
2111
>ALTER TABLESPACE</TD
 
2112
></TR
 
2113
></TABLE
 
2114
></DIV
 
2115
></BODY
 
2116
></HTML
 
2117
>
 
 
b'\\ No newline at end of file'