11<!--
2- $Header: /cvsroot/pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.9 2000/01/09 17:35 :27momjian Exp $
2+ $Header: /cvsroot/pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.10 2000/01/29 16:58 :27petere Exp $
33Postgres documentation
44-->
55
@@ -23,11 +23,14 @@ Postgres documentation
2323 <date>1999-07-20</date>
2424 </refsynopsisdivinfo>
2525 <synopsis>
26- ALTER TABLE <replaceable class="PARAMETER">table</replaceable>
27- [ * ] ADD [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> <replaceable
26+ ALTER TABLE <replaceable class="PARAMETER">table</replaceable> [ * ]
27+ ADD [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> <replaceable
2828 class="PARAMETER">type</replaceable>
29- ALTER TABLE <replaceable class="PARAMETER">table</replaceable>
30- [ * ] RENAME [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> TO <replaceable
29+ ALTER TABLE <replaceable class="PARAMETER">table</replaceable> [ * ]
30+ ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> { SET DEFAULT <replaceable
31+ class="PARAMETER">value</replaceable> | DROP DEFAULT }
32+ ALTER TABLE <replaceable class="PARAMETER">table</replaceable> [ * ]
33+ RENAME [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> TO <replaceable
3134 class="PARAMETER">newcolumn</replaceable>
3235ALTER TABLE <replaceable class="PARAMETER">table</replaceable>
3336 RENAME TO <replaceable class="PARAMETER">newtable</replaceable>
@@ -82,7 +85,7 @@ ALTER TABLE <replaceable class="PARAMETER">table</replaceable>
8285 <term><replaceable class="PARAMETER"> newtable </replaceable></term>
8386 <listitem>
8487 <para>
85- New name foran existing column .
88+ New name forthe table .
8689 </para>
8790 </listitem>
8891 </varlistentry>
@@ -101,9 +104,7 @@ ALTER TABLE <replaceable class="PARAMETER">table</replaceable>
101104
102105 <variablelist>
103106 <varlistentry>
104- <term><computeroutput>
105- ALTER
106- </computeroutput></term>
107+ <term><computeroutput>ALTER</computeroutput></term>
107108 <listitem>
108109 <para>
109110Message returned from column or table renaming.
@@ -112,20 +113,7 @@ ALTER
112113 </varlistentry>
113114
114115 <varlistentry>
115- <term><computeroutput>
116- NEW
117- </computeroutput></term>
118- <listitem>
119- <para>
120- Message returned from column addition.
121- </para>
122- </listitem>
123- </varlistentry>
124-
125- <varlistentry>
126- <term><computeroutput>
127- ERROR
128- </computeroutput></term>
116+ <term><computeroutput>ERROR</computeroutput></term>
129117 <listitem>
130118 <para>
131119Message returned if table or column is not available.
@@ -146,9 +134,12 @@ ERROR
146134 </title>
147135 <para>
148136 <command>ALTER TABLE</command> changes the definition of an existing table.
149- The new columns and their types are specified in the same style
150- and with the the same restrictions as in <command>CREATE TABLE</command>.
151- The RENAME clause causes the name of a table or column
137+ The <literal>ADD COLUMN</literal> form adds a new column to the table
138+ using the same syntax as <xref linkend="SQL-CREATETABLE"
139+ endterm="SQL-CREATETABLE-title">. The <literal>ALTER COLUMN</literal> form
140+ allows you to set or remove the default for the column. Note that defaults
141+ only apply to newly inserted rows.
142+ The <literal>RENAME</literal> clause causes the name of a table or column
152143 to change without changing any of the data contained in
153144 the affected table. Thus, the table or column will
154145 remain of the same type and size after this command is
@@ -189,13 +180,12 @@ SELECT <replaceable>NewColumn</replaceable> FROM <replaceable>SuperClass</replac
189180 </para>
190181
191182 <para>
192- For efficiency reasons, default values for added attributes are
193- not placed in existing instances of a class.
194- That is, existing instances will have NULL values in the new
195- attributes. If non-NULL values are desired, a subsequent
196- <command>UPDATE</command> query
197- (<xref linkend="sql-update-title" endterm="sql-update-title">)
198- should be run.
183+ In the current implementation, default and constraint clauses for the
184+ new column will be ignored. You can use the <literal>SET DEFAULT</literal>
185+ form of <command>ALTER TABLE</command> to set the default later.
186+ (You will also have to update the already existing rows to the
187+ new default value, using <xref linkend="sql-update-title"
188+ endterm="sql-update-title">.)
199189 </para>
200190
201191 <para>
@@ -248,87 +238,38 @@ ALTER TABLE distributors RENAME TO suppliers;
248238 <refsect2info>
249239 <date>1998-04-15</date>
250240 </refsect2info>
251- <title>
252- SQL92
253- </title>
254- <para>
255- <command>ALTER TABLE/RENAME</command>
256- is a <productname>Postgres</productname> language extension.
257- </para>
241+ <title>SQL92</title>
242+ <para>
243+ The <literal>ADD COLUMN</literal> form is compliant with the exception that
244+ it does not support defaults and constraints, as explained above.
245+ The <literal>ALTER COLUMN</literal> form is in full compliance.
246+ </para>
258247
259- <para>
260- SQL92 specifies some additional capabilities for <command>ALTER TABLE</command>
261- statement which are not yet directly supported by
262- <productname>Postgres</productname>:
263- </para>
264-
265- <variablelist>
266- <varlistentry>
267- <term>
268- <synopsis>
269- ALTER TABLE <replaceable class="PARAMETER">table</replaceable> ALTER [
270- COLUMN ] <replaceable class="PARAMETER">column</replaceable>
271- SET DEFAULT <replaceable class="PARAMETER">default</replaceable>
272- ALTER TABLE <replaceable class="PARAMETER">table</replaceable> ALTER [
273- COLUMN ] <replaceable class="PARAMETER">column</replaceable>
274- ADD [ CONSTRAINT <replaceable class="PARAMETER">>constrain</replaceable>> ] <replaceable
275- class="PARAMETER">table-constraint</replaceable>
276- </synopsis>
277- </term>
278- <listitem>
279- <para>
280- Puts the default value or constraint specified into the
281- definition of column in the table.
282- See <command>CREATE TABLE</command> for the
283- syntax of the default and table-constraint clauses.
284- If a default clause already exists, it will be replaced by
285- the new definition. If any constraints on this column already
286- exist, they will be retained using a boolean AND with the new
287- constraint.
288- </para>
289-
290- <para>
291- Currently, to set new default constraints on an existing column
292- the table must be recreated and reloaded:
293-
294- <programlisting>
295- CREATE TABLE temp AS SELECT * FROM distributors;
296- DROP TABLE distributors;
297- CREATE TABLE distributors (
298- did DECIMAL(3) DEFAULT 1,
299- name VARCHAR(40) NOT NULL,
300- city VARCHAR(30)
301- );
302- INSERT INTO distributors SELECT * FROM temp;
303- DROP TABLE temp;
304- </programlisting>
305- </para>
306- </listitem>
307- </varlistentry>
248+ <para>
249+ SQL92 specifies some additional capabilities for <command>ALTER TABLE</command>
250+ statement which are not yet directly supported by <productname>PostgreSQL</productname>:
308251
309- <varlistentry>
310- <term>
311- <synopsis>
312- ALTER TABLE <replaceable class="PARAMETER">table</replaceable>
313- DROP DEFAULT <replaceable class="PARAMETER">default</replaceable>
314- ALTER TABLE <replaceable class="PARAMETER">table</replaceable>
315- DROP CONSTRAINT <replaceable class="PARAMETER">constraint</replaceable> { RESTRICT | CASCADE }
252+ <variablelist>
253+ <varlistentry>
254+ <term>
255+ <synopsis>
256+ ALTER TABLE <replaceable class="PARAMETER">table</replaceable> ADD <replaceable class="PARAMETER">table constraint definition</replaceable>
257+ ALTER TABLE <replaceable class="PARAMETER">table</replaceable> DROP CONSTRAINT <replaceable class="PARAMETER">constraint</replaceable> { RESTRICT | CASCADE }
316258 </synopsis>
317259 </term>
318260 <listitem>
319261 <para>
320- Removes the default value specified by default or the rule
321- specified by constraint from the definition of a table.
322- If RESTRICT is specified only a constraint with no dependent
323- constraints can be destroyed.
324- If CASCADE is specified, Any constraints that are dependent on
325- this constraint are also dropped.
262+ Adds or removes a table constraint (such as a check constraint,
263+ unique constraint, or foreign key constraint). To create
264+ or remove a unique constraint, create or drop a unique index,
265+ respectively (see <xref linkend="SQL-CREATEINDEX" endterm="SQL-CREATEINDEX-title">).
266+ To change other kinds of constraints you need to recreate
267+ and reload the table, using other parameters to the
268+ <xref linkend="SQL-CREATETABLE" endterm="SQL-CREATETABLE-title">
269+ command.
326270 </para>
327-
328271 <para>
329- Currently, to remove a default value or constraints on an
330- existing column the table must be recreated and reloaded:
331-
272+ For example, to drop any constraints on a table <literal>distributors</literal>:
332273 <programlisting>
333274CREATE TABLE temp AS SELECT * FROM distributors;
334275DROP TABLE distributors;
@@ -342,23 +283,14 @@ DROP TABLE temp;
342283 <varlistentry>
343284 <term>
344285 <synopsis>
345- ALTER TABLE <replaceable class="PARAMETER">table</replaceable>
346- DROP [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> { RESTRICT | CASCADE }
286+ ALTER TABLE <replaceable class="PARAMETER">table</replaceable> DROP [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> { RESTRICT | CASCADE }
347287 </synopsis>
348288 </term>
349289 <listitem>
350290 <para>
351291 Removes a column from a table.
352- If RESTRICT is specified only a column with no dependent
353- objects can be destroyed.
354- If CASCADE is specified, all objects that are dependent on
355- this column are also dropped.
356- </para>
357-
358- <para>
359292 Currently, to remove an existing column the table must be
360293 recreated and reloaded:
361-
362294 <programlisting>
363295CREATE TABLE temp AS SELECT did, city FROM distributors;
364296DROP TABLE distributors;
@@ -373,6 +305,13 @@ DROP TABLE temp;
373305 </listitem>
374306 </varlistentry>
375307 </variablelist>
308+ </para>
309+
310+ <para>
311+ The clauses to rename columns and tables are <productname>PostgreSQL</productname>
312+ extensions. SQL92 does not provide for them.
313+ </para>
314+
376315 </refsect2>
377316 </refsect1>
378317</refentry>