1ALTER TABLE(7)          PostgreSQL 9.2.24 Documentation         ALTER TABLE(7)
2
3
4

NAME

6       ALTER_TABLE - change the definition of a table
7

SYNOPSIS

9       ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]
10           action [, ... ]
11       ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]
12           RENAME [ COLUMN ] column_name TO new_column_name
13       ALTER TABLE [ IF EXISTS ] [ ONLY ] name [ * ]
14           RENAME CONSTRAINT constraint_name TO new_constraint_name
15       ALTER TABLE [ IF EXISTS ] name
16           RENAME TO new_name
17       ALTER TABLE [ IF EXISTS ] name
18           SET SCHEMA new_schema
19
20       where action is one of:
21
22           ADD [ COLUMN ] column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ]
23           DROP [ COLUMN ] [ IF EXISTS ] column_name [ RESTRICT | CASCADE ]
24           ALTER [ COLUMN ] column_name [ SET DATA ] TYPE data_type [ COLLATE collation ] [ USING expression ]
25           ALTER [ COLUMN ] column_name SET DEFAULT expression
26           ALTER [ COLUMN ] column_name DROP DEFAULT
27           ALTER [ COLUMN ] column_name { SET | DROP } NOT NULL
28           ALTER [ COLUMN ] column_name SET STATISTICS integer
29           ALTER [ COLUMN ] column_name SET ( attribute_option = value [, ... ] )
30           ALTER [ COLUMN ] column_name RESET ( attribute_option [, ... ] )
31           ALTER [ COLUMN ] column_name SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
32           ADD table_constraint [ NOT VALID ]
33           ADD table_constraint_using_index
34           VALIDATE CONSTRAINT constraint_name
35           DROP CONSTRAINT [ IF EXISTS ]  constraint_name [ RESTRICT | CASCADE ]
36           DISABLE TRIGGER [ trigger_name | ALL | USER ]
37           ENABLE TRIGGER [ trigger_name | ALL | USER ]
38           ENABLE REPLICA TRIGGER trigger_name
39           ENABLE ALWAYS TRIGGER trigger_name
40           DISABLE RULE rewrite_rule_name
41           ENABLE RULE rewrite_rule_name
42           ENABLE REPLICA RULE rewrite_rule_name
43           ENABLE ALWAYS RULE rewrite_rule_name
44           CLUSTER ON index_name
45           SET WITHOUT CLUSTER
46           SET WITH OIDS
47           SET WITHOUT OIDS
48           SET ( storage_parameter = value [, ... ] )
49           RESET ( storage_parameter [, ... ] )
50           INHERIT parent_table
51           NO INHERIT parent_table
52           OF type_name
53           NOT OF
54           OWNER TO new_owner
55           SET TABLESPACE new_tablespace
56
57       and table_constraint_using_index is:
58
59           [ CONSTRAINT constraint_name ]
60           { UNIQUE | PRIMARY KEY } USING INDEX index_name
61           [ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
62

DESCRIPTION

64       ALTER TABLE changes the definition of an existing table. There are
65       several subforms:
66
67       ADD COLUMN
68           This form adds a new column to the table, using the same syntax as
69           CREATE TABLE (CREATE_TABLE(7)).
70
71       DROP COLUMN [ IF EXISTS ]
72           This form drops a column from a table. Indexes and table
73           constraints involving the column will be automatically dropped as
74           well. You will need to say CASCADE if anything outside the table
75           depends on the column, for example, foreign key references or
76           views. If IF EXISTS is specified and the column does not exist, no
77           error is thrown. In this case a notice is issued instead.
78
79       IF EXISTS
80           Do not throw an error if the table does not exist. A notice is
81           issued in this case.
82
83       SET DATA TYPE
84           This form changes the type of a column of a table. Indexes and
85           simple table constraints involving the column will be automatically
86           converted to use the new column type by reparsing the originally
87           supplied expression. The optional COLLATE clause specifies a
88           collation for the new column; if omitted, the collation is the
89           default for the new column type. The optional USING clause
90           specifies how to compute the new column value from the old; if
91           omitted, the default conversion is the same as an assignment cast
92           from old data type to new. A USING clause must be provided if there
93           is no implicit or assignment cast from old to new type.
94
95       SET/DROP DEFAULT
96           These forms set or remove the default value for a column. The
97           default values only apply to subsequent INSERT commands; they do
98           not cause rows already in the table to change. Defaults can also be
99           created for views, in which case they are inserted into INSERT
100           statements on the view before the view's ON INSERT rule is applied.
101
102       SET/DROP NOT NULL
103           These forms change whether a column is marked to allow null values
104           or to reject null values. You can only use SET NOT NULL when the
105           column contains no null values.
106
107       SET STATISTICS
108           This form sets the per-column statistics-gathering target for
109           subsequent ANALYZE(7) operations. The target can be set in the
110           range 0 to 10000; alternatively, set it to -1 to revert to using
111           the system default statistics target (default_statistics_target).
112           For more information on the use of statistics by the PostgreSQL
113           query planner, refer to Section 14.2, “Statistics Used by the
114           Planner”, in the documentation.
115
116       SET ( attribute_option = value [, ... ] ), RESET ( attribute_option [,
117       ... ] )
118           This form sets or resets per-attribute options. Currently, the only
119           defined per-attribute options are n_distinct and
120           n_distinct_inherited, which override the number-of-distinct-values
121           estimates made by subsequent ANALYZE(7) operations.  n_distinct
122           affects the statistics for the table itself, while
123           n_distinct_inherited affects the statistics gathered for the table
124           plus its inheritance children. When set to a positive value,
125           ANALYZE will assume that the column contains exactly the specified
126           number of distinct nonnull values. When set to a negative value,
127           which must be greater than or equal to -1, ANALYZE will assume that
128           the number of distinct nonnull values in the column is linear in
129           the size of the table; the exact count is to be computed by
130           multiplying the estimated table size by the absolute value of the
131           given number. For example, a value of -1 implies that all values in
132           the column are distinct, while a value of -0.5 implies that each
133           value appears twice on the average. This can be useful when the
134           size of the table changes over time, since the multiplication by
135           the number of rows in the table is not performed until query
136           planning time. Specify a value of 0 to revert to estimating the
137           number of distinct values normally. For more information on the use
138           of statistics by the PostgreSQL query planner, refer to Section
139           14.2, “Statistics Used by the Planner”, in the documentation.
140
141       SET STORAGE
142           This form sets the storage mode for a column. This controls whether
143           this column is held inline or in a secondary TOAST table, and
144           whether the data should be compressed or not.  PLAIN must be used
145           for fixed-length values such as integer and is inline,
146           uncompressed.  MAIN is for inline, compressible data.  EXTERNAL is
147           for external, uncompressed data, and EXTENDED is for external,
148           compressed data.  EXTENDED is the default for most data types that
149           support non-PLAIN storage. Use of EXTERNAL will make substring
150           operations on very large text and bytea values run faster, at the
151           penalty of increased storage space. Note that SET STORAGE doesn't
152           itself change anything in the table, it just sets the strategy to
153           be pursued during future table updates. See Section 56.2, “TOAST”,
154           in the documentation for more information.
155
156       ADD table_constraint [ NOT VALID ]
157           This form adds a new constraint to a table using the same syntax as
158           CREATE TABLE (CREATE_TABLE(7)), plus the option NOT VALID, which is
159           currently only allowed for foreign key and CHECK constraints. If
160           the constraint is marked NOT VALID, the potentially-lengthy initial
161           check to verify that all rows in the table satisfy the constraint
162           is skipped. The constraint will still be enforced against
163           subsequent inserts or updates (that is, they'll fail unless there
164           is a matching row in the referenced table, in the case of foreign
165           keys; and they'll fail unless the new row matches the specified
166           check constraints). But the database will not assume that the
167           constraint holds for all rows in the table, until it is validated
168           by using the VALIDATE CONSTRAINT option.
169
170       ADD table_constraint_using_index
171           This form adds a new PRIMARY KEY or UNIQUE constraint to a table
172           based on an existing unique index. All the columns of the index
173           will be included in the constraint.
174
175           The index cannot have expression columns nor be a partial index.
176           Also, it must be a b-tree index with default sort ordering. These
177           restrictions ensure that the index is equivalent to one that would
178           be built by a regular ADD PRIMARY KEY or ADD UNIQUE command.
179
180           If PRIMARY KEY is specified, and the index's columns are not
181           already marked NOT NULL, then this command will attempt to do ALTER
182           COLUMN SET NOT NULL against each such column. That requires a full
183           table scan to verify the column(s) contain no nulls. In all other
184           cases, this is a fast operation.
185
186           If a constraint name is provided then the index will be renamed to
187           match the constraint name. Otherwise the constraint will be named
188           the same as the index.
189
190           After this command is executed, the index is “owned” by the
191           constraint, in the same way as if the index had been built by a
192           regular ADD PRIMARY KEY or ADD UNIQUE command. In particular,
193           dropping the constraint will make the index disappear too.
194
195               Note
196               Adding a constraint using an existing index can be helpful in
197               situations where a new constraint needs to be added without
198               blocking table updates for a long time. To do that, create the
199               index using CREATE INDEX CONCURRENTLY, and then install it as
200               an official constraint using this syntax. See the example
201               below.
202
203       VALIDATE CONSTRAINT
204           This form validates a foreign key or check constraint that was
205           previously created as NOT VALID, by scanning the table to ensure
206           there are no rows for which the constraint is not satisfied.
207           Nothing happens if the constraint is already marked valid.
208
209           Validation can be a long process on larger tables and currently
210           requires an ACCESS EXCLUSIVE lock. The value of separating
211           validation from initial creation is that you can defer validation
212           to less busy times, or can be used to give additional time to
213           correct pre-existing errors while preventing new errors.
214
215       DROP CONSTRAINT [ IF EXISTS ]
216           This form drops the specified constraint on a table. If IF EXISTS
217           is specified and the constraint does not exist, no error is thrown.
218           In this case a notice is issued instead.
219
220       DISABLE/ENABLE [ REPLICA | ALWAYS ] TRIGGER
221           These forms configure the firing of trigger(s) belonging to the
222           table. A disabled trigger is still known to the system, but is not
223           executed when its triggering event occurs. For a deferred trigger,
224           the enable status is checked when the event occurs, not when the
225           trigger function is actually executed. One can disable or enable a
226           single trigger specified by name, or all triggers on the table, or
227           only user triggers (this option excludes internally generated
228           constraint triggers such as those that are used to implement
229           foreign key constraints or deferrable uniqueness and exclusion
230           constraints). Disabling or enabling internally generated constraint
231           triggers requires superuser privileges; it should be done with
232           caution since of course the integrity of the constraint cannot be
233           guaranteed if the triggers are not executed. The trigger firing
234           mechanism is also affected by the configuration variable
235           session_replication_role. Simply enabled triggers will fire when
236           the replication role is “origin” (the default) or “local”. Triggers
237           configured as ENABLE REPLICA will only fire if the session is in
238           “replica” mode, and triggers configured as ENABLE ALWAYS will fire
239           regardless of the current replication mode.
240
241       DISABLE/ENABLE [ REPLICA | ALWAYS ] RULE
242           These forms configure the firing of rewrite rules belonging to the
243           table. A disabled rule is still known to the system, but is not
244           applied during query rewriting. The semantics are as for
245           disabled/enabled triggers. This configuration is ignored for ON
246           SELECT rules, which are always applied in order to keep views
247           working even if the current session is in a non-default replication
248           role.
249
250       CLUSTER ON
251           This form selects the default index for future CLUSTER(7)
252           operations. It does not actually re-cluster the table.
253
254       SET WITHOUT CLUSTER
255           This form removes the most recently used CLUSTER(7) index
256           specification from the table. This affects future cluster
257           operations that don't specify an index.
258
259       SET WITH OIDS
260           This form adds an oid system column to the table (see Section 5.4,
261           “System Columns”, in the documentation). It does nothing if the
262           table already has OIDs.
263
264           Note that this is not equivalent to ADD COLUMN oid oid; that would
265           add a normal column that happened to be named oid, not a system
266           column.
267
268       SET WITHOUT OIDS
269           This form removes the oid system column from the table. This is
270           exactly equivalent to DROP COLUMN oid RESTRICT, except that it will
271           not complain if there is already no oid column.
272
273       SET ( storage_parameter = value [, ... ] )
274           This form changes one or more storage parameters for the table. See
275           Storage Parameters for details on the available parameters. Note
276           that the table contents will not be modified immediately by this
277           command; depending on the parameter you might need to rewrite the
278           table to get the desired effects. That can be done with VACUUM
279           FULL, CLUSTER(7) or one of the forms of ALTER TABLE that forces a
280           table rewrite.
281
282               Note
283               While CREATE TABLE allows OIDS to be specified in the WITH
284               (storage_parameter) syntax, ALTER TABLE does not treat OIDS as
285               a storage parameter. Instead use the SET WITH OIDS and SET
286               WITHOUT OIDS forms to change OID status.
287
288       RESET ( storage_parameter [, ... ] )
289           This form resets one or more storage parameters to their defaults.
290           As with SET, a table rewrite might be needed to update the table
291           entirely.
292
293       INHERIT parent_table
294           This form adds the target table as a new child of the specified
295           parent table. Subsequently, queries against the parent will include
296           records of the target table. To be added as a child, the target
297           table must already contain all the same columns as the parent (it
298           could have additional columns, too). The columns must have matching
299           data types, and if they have NOT NULL constraints in the parent
300           then they must also have NOT NULL constraints in the child.
301
302           There must also be matching child-table constraints for all CHECK
303           constraints of the parent, except those marked non-inheritable
304           (that is, created with ALTER TABLE ... ADD CONSTRAINT ... NO
305           INHERIT) in the parent, which are ignored; all child-table
306           constraints matched must not be marked non-inheritable. Currently
307           UNIQUE, PRIMARY KEY, and FOREIGN KEY constraints are not
308           considered, but this might change in the future.
309
310       NO INHERIT parent_table
311           This form removes the target table from the list of children of the
312           specified parent table. Queries against the parent table will no
313           longer include records drawn from the target table.
314
315       OF type_name
316           This form links the table to a composite type as though CREATE
317           TABLE OF had formed it. The table's list of column names and types
318           must precisely match that of the composite type; the presence of an
319           oid system column is permitted to differ. The table must not
320           inherit from any other table. These restrictions ensure that CREATE
321           TABLE OF would permit an equivalent table definition.
322
323       NOT OF
324           This form dissociates a typed table from its type.
325
326       OWNER
327           This form changes the owner of the table, sequence, or view to the
328           specified user.
329
330       SET TABLESPACE
331           This form changes the table's tablespace to the specified
332           tablespace and moves the data file(s) associated with the table to
333           the new tablespace. Indexes on the table, if any, are not moved;
334           but they can be moved separately with additional SET TABLESPACE
335           commands. See also CREATE TABLESPACE (CREATE_TABLESPACE(7)).
336
337       RENAME
338           The RENAME forms change the name of a table (or an index, sequence,
339           or view), the name of an individual column in a table, or the name
340           of a constraint of the table. There is no effect on the stored
341           data.
342
343       SET SCHEMA
344           This form moves the table into another schema. Associated indexes,
345           constraints, and sequences owned by table columns are moved as
346           well.
347
348       All the actions except RENAME and SET SCHEMA can be combined into a
349       list of multiple alterations to apply in parallel. For example, it is
350       possible to add several columns and/or alter the type of several
351       columns in a single command. This is particularly useful with large
352       tables, since only one pass over the table need be made.
353
354       You must own the table to use ALTER TABLE. To change the schema of a
355       table, you must also have CREATE privilege on the new schema. To add
356       the table as a new child of a parent table, you must own the parent
357       table as well. To alter the owner, you must also be a direct or
358       indirect member of the new owning role, and that role must have CREATE
359       privilege on the table's schema. (These restrictions enforce that
360       altering the owner doesn't do anything you couldn't do by dropping and
361       recreating the table. However, a superuser can alter ownership of any
362       table anyway.) To add a column or alter a column type or use the OF
363       clause, you must also have USAGE privilege on the data type.
364

PARAMETERS

366       name
367           The name (optionally schema-qualified) of an existing table to
368           alter. If ONLY is specified before the table name, only that table
369           is altered. If ONLY is not specified, the table and all its
370           descendant tables (if any) are altered. Optionally, * can be
371           specified after the table name to explicitly indicate that
372           descendant tables are included.
373
374       column_name
375           Name of a new or existing column.
376
377       new_column_name
378           New name for an existing column.
379
380       new_name
381           New name for the table.
382
383       type
384           Data type of the new column, or new data type for an existing
385           column.
386
387       table_constraint
388           New table constraint for the table.
389
390       constraint_name
391           Name of an existing constraint to drop.
392
393       CASCADE
394           Automatically drop objects that depend on the dropped column or
395           constraint (for example, views referencing the column).
396
397       RESTRICT
398           Refuse to drop the column or constraint if there are any dependent
399           objects. This is the default behavior.
400
401       trigger_name
402           Name of a single trigger to disable or enable.
403
404       ALL
405           Disable or enable all triggers belonging to the table. (This
406           requires superuser privilege if any of the triggers are internally
407           generated constraint triggers such as those that are used to
408           implement foreign key constraints or deferrable uniqueness and
409           exclusion constraints.)
410
411       USER
412           Disable or enable all triggers belonging to the table except for
413           internally generated constraint triggers such as those that are
414           used to implement foreign key constraints or deferrable uniqueness
415           and exclusion constraints.
416
417       index_name
418           The index name on which the table should be marked for clustering.
419
420       storage_parameter
421           The name of a table storage parameter.
422
423       value
424           The new value for a table storage parameter. This might be a number
425           or a word depending on the parameter.
426
427       parent_table
428           A parent table to associate or de-associate with this table.
429
430       new_owner
431           The user name of the new owner of the table.
432
433       new_tablespace
434           The name of the tablespace to which the table will be moved.
435
436       new_schema
437           The name of the schema to which the table will be moved.
438

NOTES

440       The key word COLUMN is noise and can be omitted.
441
442       When a column is added with ADD COLUMN, all existing rows in the table
443       are initialized with the column's default value (NULL if no DEFAULT
444       clause is specified).
445
446       Adding a column with a non-null default or changing the type of an
447       existing column will require the entire table and indexes to be
448       rewritten. As an exception, if the USING clause does not change the
449       column contents and the old type is either binary coercible to the new
450       type or an unconstrained domain over the new type, a table rewrite is
451       not needed, but any indexes on the affected columns must still be
452       rebuilt. Adding or removing a system oid column also requires rewriting
453       the entire table. Table and/or index rebuilds may take a significant
454       amount of time for a large table; and will temporarily require as much
455       as double the disk space.
456
457       Adding a CHECK or NOT NULL constraint requires scanning the table to
458       verify that existing rows meet the constraint, but does not require a
459       table rewrite.
460
461       The main reason for providing the option to specify multiple changes in
462       a single ALTER TABLE is that multiple table scans or rewrites can
463       thereby be combined into a single pass over the table.
464
465       The DROP COLUMN form does not physically remove the column, but simply
466       makes it invisible to SQL operations. Subsequent insert and update
467       operations in the table will store a null value for the column. Thus,
468       dropping a column is quick but it will not immediately reduce the
469       on-disk size of your table, as the space occupied by the dropped column
470       is not reclaimed. The space will be reclaimed over time as existing
471       rows are updated. (These statements do not apply when dropping the
472       system oid column; that is done with an immediate rewrite.)
473
474       To force immediate reclamation of space occupied by a dropped column,
475       you can execute one of the forms of ALTER TABLE that performs a rewrite
476       of the whole table. This results in reconstructing each row with the
477       dropped column replaced by a null value.
478
479       The rewriting forms of ALTER TABLE are not MVCC-safe. After a table
480       rewrite, the table will appear empty to concurrent transactions, if
481       they are using a snapshot taken before the rewrite occurred. See
482       Section 13.5, “Caveats”, in the documentation for more details.
483
484       The USING option of SET DATA TYPE can actually specify any expression
485       involving the old values of the row; that is, it can refer to other
486       columns as well as the one being converted. This allows very general
487       conversions to be done with the SET DATA TYPE syntax. Because of this
488       flexibility, the USING expression is not applied to the column's
489       default value (if any); the result might not be a constant expression
490       as required for a default. This means that when there is no implicit or
491       assignment cast from old to new type, SET DATA TYPE might fail to
492       convert the default even though a USING clause is supplied. In such
493       cases, drop the default with DROP DEFAULT, perform the ALTER TYPE, and
494       then use SET DEFAULT to add a suitable new default. Similar
495       considerations apply to indexes and constraints involving the column.
496
497       If a table has any descendant tables, it is not permitted to add,
498       rename, or change the type of a column, or rename an inherited
499       constraint in the parent table without doing the same to the
500       descendants. That is, ALTER TABLE ONLY will be rejected. This ensures
501       that the descendants always have columns matching the parent.
502
503       A recursive DROP COLUMN operation will remove a descendant table's
504       column only if the descendant does not inherit that column from any
505       other parents and never had an independent definition of the column. A
506       nonrecursive DROP COLUMN (i.e., ALTER TABLE ONLY ... DROP COLUMN) never
507       removes any descendant columns, but instead marks them as independently
508       defined rather than inherited.
509
510       The TRIGGER, CLUSTER, OWNER, and TABLESPACE actions never recurse to
511       descendant tables; that is, they always act as though ONLY were
512       specified. Adding a constraint recurses only for CHECK constraints that
513       are not marked NO INHERIT.
514
515       Changing any part of a system catalog table is not permitted.
516
517       Refer to CREATE TABLE (CREATE_TABLE(7)) for a further description of
518       valid parameters.  Chapter 5, Data Definition, in the documentation has
519       further information on inheritance.
520

EXAMPLES

522       To add a column of type varchar to a table:
523
524           ALTER TABLE distributors ADD COLUMN address varchar(30);
525
526       To drop a column from a table:
527
528           ALTER TABLE distributors DROP COLUMN address RESTRICT;
529
530       To change the types of two existing columns in one operation:
531
532           ALTER TABLE distributors
533               ALTER COLUMN address TYPE varchar(80),
534               ALTER COLUMN name TYPE varchar(100);
535
536       To change an integer column containing UNIX timestamps to timestamp
537       with time zone via a USING clause:
538
539           ALTER TABLE foo
540               ALTER COLUMN foo_timestamp SET DATA TYPE timestamp with time zone
541               USING
542                   timestamp with time zone 'epoch' + foo_timestamp * interval '1 second';
543
544       The same, when the column has a default expression that won't
545       automatically cast to the new data type:
546
547           ALTER TABLE foo
548               ALTER COLUMN foo_timestamp DROP DEFAULT,
549               ALTER COLUMN foo_timestamp TYPE timestamp with time zone
550               USING
551                   timestamp with time zone 'epoch' + foo_timestamp * interval '1 second',
552               ALTER COLUMN foo_timestamp SET DEFAULT now();
553
554       To rename an existing column:
555
556           ALTER TABLE distributors RENAME COLUMN address TO city;
557
558       To rename an existing table:
559
560           ALTER TABLE distributors RENAME TO suppliers;
561
562       To rename an existing constraint:
563
564           ALTER TABLE distributors RENAME CONSTRAINT zipchk TO zip_check;
565
566       To add a not-null constraint to a column:
567
568           ALTER TABLE distributors ALTER COLUMN street SET NOT NULL;
569
570       To remove a not-null constraint from a column:
571
572           ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;
573
574       To add a check constraint to a table and all its children:
575
576           ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);
577
578       To add a check constraint only to a table and not to its children:
579
580           ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5) NO INHERIT;
581
582       (The check constraint will not be inherited by future children,
583       either.)
584
585       To remove a check constraint from a table and all its children:
586
587           ALTER TABLE distributors DROP CONSTRAINT zipchk;
588
589       To remove a check constraint from one table only:
590
591           ALTER TABLE ONLY distributors DROP CONSTRAINT zipchk;
592
593       (The check constraint remains in place for any child tables.)
594
595       To add a foreign key constraint to a table:
596
597           ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address);
598
599       To add a (multicolumn) unique constraint to a table:
600
601           ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);
602
603       To add an automatically named primary key constraint to a table, noting
604       that a table can only ever have one primary key:
605
606           ALTER TABLE distributors ADD PRIMARY KEY (dist_id);
607
608       To move a table to a different tablespace:
609
610           ALTER TABLE distributors SET TABLESPACE fasttablespace;
611
612       To move a table to a different schema:
613
614           ALTER TABLE myschema.distributors SET SCHEMA yourschema;
615
616       To recreate a primary key constraint, without blocking updates while
617       the index is rebuilt:
618
619           CREATE UNIQUE INDEX CONCURRENTLY dist_id_temp_idx ON distributors (dist_id);
620           ALTER TABLE distributors DROP CONSTRAINT distributors_pkey,
621               ADD CONSTRAINT distributors_pkey PRIMARY KEY USING INDEX dist_id_temp_idx;
622

COMPATIBILITY

624       The forms ADD (without USING INDEX), DROP, SET DEFAULT, and SET DATA
625       TYPE (without USING) conform with the SQL standard. The other forms are
626       PostgreSQL extensions of the SQL standard. Also, the ability to specify
627       more than one manipulation in a single ALTER TABLE command is an
628       extension.
629
630       ALTER TABLE DROP COLUMN can be used to drop the only column of a table,
631       leaving a zero-column table. This is an extension of SQL, which
632       disallows zero-column tables.
633

SEE ALSO

635       CREATE TABLE (CREATE_TABLE(7))
636
637
638
639PostgreSQL 9.2.24                 2017-11-06                    ALTER TABLE(7)
Impressum