OSDN Git Service

FIRST REPOSITORY
[eos/hostdependOTHERS.git] / I386LINUX / util / I386LINUX / man / man7 / alter_table.7
1 .\\" auto-generated by docbook2man-spec $Revision: 1.25 $
2 .TH "ALTER TABLE" "7" "2003-11-02" "SQL - Language Statements" "SQL Commands"
3 .SH NAME
4 ALTER TABLE \- change the definition of a table
5
6 .SH SYNOPSIS
7 .sp
8 .nf
9 ALTER TABLE [ ONLY ] \fIname\fR [ * ]
10     ADD [ COLUMN ] \fIcolumn\fR \fItype\fR [ \fIcolumn_constraint\fR [ ... ] ]
11 ALTER TABLE [ ONLY ] \fIname\fR [ * ]
12     DROP [ COLUMN ] \fIcolumn\fR [ RESTRICT | CASCADE ]
13 ALTER TABLE [ ONLY ] \fIname\fR [ * ]
14     ALTER [ COLUMN ] \fIcolumn\fR { SET DEFAULT \fIexpression\fR | DROP DEFAULT }
15 ALTER TABLE [ ONLY ] \fIname\fR [ * ]
16     ALTER [ COLUMN ] \fIcolumn\fR { SET | DROP } NOT NULL
17 ALTER TABLE [ ONLY ] \fIname\fR [ * ]
18     ALTER [ COLUMN ] \fIcolumn\fR SET STATISTICS \fIinteger\fR
19 ALTER TABLE [ ONLY ] \fIname\fR [ * ]
20     ALTER [ COLUMN ] \fIcolumn\fR SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
21 ALTER TABLE [ ONLY ] \fIname\fR [ * ]
22     SET WITHOUT OIDS
23 ALTER TABLE [ ONLY ] \fIname\fR [ * ]
24     RENAME [ COLUMN ] \fIcolumn\fR TO \fInew_column\fR
25 ALTER TABLE \fIname\fR
26     RENAME TO \fInew_name\fR
27 ALTER TABLE [ ONLY ] \fIname\fR [ * ]
28     ADD \fItable_constraint\fR
29 ALTER TABLE [ ONLY ] \fIname\fR [ * ]
30     DROP CONSTRAINT \fIconstraint_name\fR [ RESTRICT | CASCADE ]
31 ALTER TABLE \fIname\fR
32     OWNER TO \fInew_owner\fR
33 ALTER TABLE \fIname\fR
34     CLUSTER ON \fIindex_name\fR
35 .sp
36 .fi
37 .SH "DESCRIPTION"
38 .PP
39 \fBALTER TABLE\fR changes the definition of an existing table.
40 There are several subforms:
41 .TP
42 \fBADD COLUMN\fR
43 This form adds a new column to the table using the same syntax as
44 CREATE TABLE [\fBcreate_table\fR(7)].
45 .TP
46 \fBDROP COLUMN\fR
47 This form drops a column from a table. Indexes and
48 table constraints involving the column will be automatically
49 dropped as well. You will need to say CASCADE if
50 anything outside the table depends on the column, for example,
51 foreign key references or views.
52 .TP
53 \fBSET/DROP DEFAULT\fR
54 These forms set or remove the default value for a column.
55 The default values only apply to subsequent \fBINSERT\fR
56 commands; they do not cause rows already in the table to change.
57 Defaults may also be created for views, in which case they are
58 inserted into \fBINSERT\fR statements on the view before
59 the view's ON INSERT rule is applied.
60 .TP
61 \fBSET/DROP NOT NULL\fR
62 These forms change whether a column is marked to allow null
63 values or to reject null values. You can only use SET
64 NOT NULL when the column contains no null values.
65 .TP
66 \fBSET STATISTICS\fR
67 This form
68 sets the per-column statistics-gathering target for subsequent
69 ANALYZE [\fBanalyze\fR(7)] operations.
70 The target can be set in the range 0 to 1000; alternatively, set it
71 to -1 to revert to using the system default statistics target.
72 .TP
73 \fBSET STORAGE\fR
74 This form sets the storage mode for a column. This controls whether this
75 column is held inline or in a supplementary table, and whether the data
76 should be compressed or not. PLAIN must be used
77 for fixed-length values such as \fBinteger\fR and is
78 inline, uncompressed. MAIN is for inline,
79 compressible data. EXTERNAL is for external,
80 uncompressed data, and EXTENDED is for external,
81 compressed data. EXTENDED is the default for all
82 data types that support it. The use of EXTERNAL will, for example,
83 make substring operations on a \fBtext\fR column faster, at the penalty of
84 increased storage space.
85 .TP
86 \fBSET WITHOUT OIDS\fR
87 This form removes the oid column from the
88 table. Removing OIDs from a table does not occur immediately.
89 The space that the OID uses will be reclaimed when the row is
90 updated. Without updating the row, both the space and the value
91 of the OID are kept indefinitely. This is semantically similar
92 to the DROP COLUMN process.
93 .TP
94 \fBRENAME\fR
95 The RENAME forms change the name of a table
96 (or an index, sequence, or view) or the name of an individual column in
97 a table. There is no effect on the stored data.
98 .TP
99 \fBADD \fItable_constraint\fB\fR
100 This form adds a new constraint to a table using the same syntax as
101 CREATE TABLE [\fBcreate_table\fR(7)]. 
102 .TP
103 \fBDROP CONSTRAINT\fR
104 This form drops constraints on a table.
105 Currently, constraints on tables are not required to have unique
106 names, so there may be more than one constraint matching the specified
107 name. All such constraints will be dropped.
108 .TP
109 \fBOWNER\fR
110 This form changes the owner of the table, index, sequence, or view to the
111 specified user.
112 .TP
113 \fBCLUSTER\fR
114 This form marks a table for future CLUSTER [\fBcluster\fR(7)]
115 operations.
116 .PP
117 .PP
118 You must own the table to use \fBALTER TABLE\fR; except for
119 \fBALTER TABLE OWNER\fR, which may only be executed by a superuser.
120 .SH "PARAMETERS"
121 .TP
122 \fB\fIname\fB\fR
123 The name (possibly schema-qualified) of an existing table to
124 alter. If ONLY is specified, only that table is
125 altered. If ONLY is not specified, the table and all
126 its descendant tables (if any) are updated. * can be
127 appended to the table name to indicate that descendant tables are
128 to be altered, but in the current version, this is the default
129 behavior. (In releases before 7.1, ONLY was the
130 default behavior. The default can be altered by changing the
131 configuration parameter sql_inheritance.)
132 .TP
133 \fB\fIcolumn\fB\fR
134 Name of a new or existing column.
135 .TP
136 \fB\fItype\fB\fR
137 Data type of the new column.
138 .TP
139 \fB\fInew_column\fB\fR
140 New name for an existing column.
141 .TP
142 \fB\fInew_name\fB\fR
143 New name for the table.
144 .TP
145 \fB\fItable_constraint\fB\fR
146 New table constraint for the table.
147 .TP
148 \fB\fIconstraint_name\fB\fR
149 Name of an existing constraint to drop.
150 .TP
151 \fB\fInew_owner\fB\fR
152 The user name of the new owner of the table.
153 .TP
154 \fB\fIindex_name\fB\fR
155 The index name on which the table should be marked for clustering.
156 .TP
157 \fBCASCADE\fR
158 Automatically drop objects that depend on the dropped column
159 or constraint (for example, views referencing the column).
160 .TP
161 \fBRESTRICT\fR
162 Refuse to drop the column or constraint if there are any dependent
163 objects. This is the default behavior.
164 .SH "NOTES"
165 .PP
166 The key word COLUMN is noise and can be omitted.
167 .PP
168 In the current implementation of ADD COLUMN,
169 default and NOT NULL clauses for the new column are not supported.
170 The new column always comes into being with all values null.
171 You can use the SET DEFAULT form
172 of \fBALTER TABLE\fR to set the default afterward.
173 (You may also want to update the already existing rows to the
174 new default value, using
175 UPDATE [\fBupdate\fR(7)].)
176 If you want to mark the column non-null, use the SET NOT NULL
177 form after you've entered non-null values for the column in all rows.
178 .PP
179 The DROP COLUMN form does not physically remove
180 the column, but simply makes it invisible to SQL operations. Subsequent
181 insert and update operations in the table will store a null value for the column.
182 Thus, dropping a column is quick but it will not immediately reduce the
183 on-disk size of your table, as the space occupied 
184 by the dropped column is not reclaimed. The space will be
185 reclaimed over time as existing rows are updated.
186 To reclaim the space at once, do a dummy \fBUPDATE\fR of all rows
187 and then vacuum, as in:
188 .sp
189 .nf
190 UPDATE table SET col = col;
191 VACUUM FULL table;
192 .sp
193 .fi
194 .PP
195 If a table has any descendant tables, it is not permitted to add
196 or rename a column in the parent table without doing the same to
197 the descendants. That is, \fBALTER TABLE ONLY\fR
198 will be rejected. This ensures that the descendants always have
199 columns matching the parent.
200 .PP
201 A recursive DROP COLUMN operation will remove a
202 descendant table's column only if the descendant does not inherit
203 that column from any other parents and never had an independent
204 definition of the column. A nonrecursive DROP
205 COLUMN (i.e., \fBALTER TABLE ONLY ... DROP
206 COLUMN\fR) never removes any descendant columns, but
207 instead marks them as independently defined rather than inherited.
208 .PP
209 Changing any part of a system catalog table is not permitted.
210 .PP
211 Refer to \fBCREATE TABLE\fR for a further description
212 of valid parameters. The chapter called ``Data Definition'' in the documentation has further information on
213 inheritance.
214 .SH "EXAMPLES"
215 .PP
216 To add a column of type \fBvarchar\fR to a table:
217 .sp
218 .nf
219 ALTER TABLE distributors ADD COLUMN address varchar(30);
220 .sp
221 .fi
222 .PP
223 To drop a column from a table:
224 .sp
225 .nf
226 ALTER TABLE distributors DROP COLUMN address RESTRICT;
227 .sp
228 .fi
229 .PP
230 To rename an existing column:
231 .sp
232 .nf
233 ALTER TABLE distributors RENAME COLUMN address TO city;
234 .sp
235 .fi
236 .PP
237 To rename an existing table:
238 .sp
239 .nf
240 ALTER TABLE distributors RENAME TO suppliers;
241 .sp
242 .fi
243 .PP
244 To add a not-null constraint to a column:
245 .sp
246 .nf
247 ALTER TABLE distributors ALTER COLUMN street SET NOT NULL;
248 .sp
249 .fi
250 To remove a not-null constraint from a column:
251 .sp
252 .nf
253 ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;
254 .sp
255 .fi
256 .PP
257 To add a check constraint to a table:
258 .sp
259 .nf
260 ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (char_length(zipcode) = 5);
261 .sp
262 .fi
263 .PP
264 To remove a check constraint from a table and all its children:
265 .sp
266 .nf
267 ALTER TABLE distributors DROP CONSTRAINT zipchk;
268 .sp
269 .fi
270 .PP
271 To add a foreign key constraint to a table:
272 .sp
273 .nf
274 ALTER TABLE distributors ADD CONSTRAINT distfk FOREIGN KEY (address) REFERENCES addresses (address) MATCH FULL;
275 .sp
276 .fi
277 .PP
278 To add a (multicolumn) unique constraint to a table:
279 .sp
280 .nf
281 ALTER TABLE distributors ADD CONSTRAINT dist_id_zipcode_key UNIQUE (dist_id, zipcode);
282 .sp
283 .fi
284 .PP
285 To add an automatically named primary key constraint to a table, noting
286 that a table can only ever have one primary key:
287 .sp
288 .nf
289 ALTER TABLE distributors ADD PRIMARY KEY (dist_id);
290 .sp
291 .fi
292 .SH "COMPATIBILITY"
293 .PP
294 The ADD COLUMN form conforms with the SQL
295 standard, with the exception that it does not support defaults and
296 not-null constraints, as explained above. The ALTER
297 COLUMN form is in full conformance.
298 .PP
299 The clauses to rename tables, columns, indexes, views, and sequences are
300 PostgreSQL extensions of the SQL standard.
301 .PP
302 \fBALTER TABLE DROP COLUMN\fR can be used to drop the only
303 column of a table, leaving a zero-column table. This is an
304 extension of SQL, which disallows zero-column tables.