Skip to content

Commit 7bbdb07

Browse files
committed
Update discussion of ALTER TABLE ADD COLUMN, per Michael Fuhr.
1 parent 0549de0 commit 7bbdb07

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

doc/src/sgml/ddl.sgml

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/ddl.sgml,v 1.36 2005/01/08 01:44:05 tgl Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/ddl.sgml,v 1.37 2005/01/09 17:47:30 tgl Exp $ -->
22

33
<chapter id="ddl">
44
<title>Data Definition</title>
@@ -1279,23 +1279,22 @@ WHERE c.altitude &gt; 500 and c.tableoid = p.oid;
12791279
<programlisting>
12801280
ALTER TABLE products ADD COLUMN description text;
12811281
</programlisting>
1282-
The new column will initially be filled with null values in the
1283-
existing rows of the table.
1282+
The new column is initially filled with whatever default
1283+
value is given (null if you don't specify a <literal>DEFAULT</> clause).
12841284
</para>
12851285

12861286
<para>
1287-
You can also define a constraint on the column at the same time,
1287+
You can also define constraints on the column at the same time,
12881288
using the usual syntax:
12891289
<programlisting>
12901290
ALTER TABLE products ADD COLUMN description text CHECK (description &lt;&gt; '');
12911291
</programlisting>
1292-
A new column cannot have a not-null constraint since the column
1293-
initially has to contain null values. But you can add a not-null
1294-
constraint later. Also, you cannot define a default value on a
1295-
new column. According to the SQL standard, this would have to
1296-
fill the new columns in the existing rows with the default value,
1297-
which is not implemented yet. But you can adjust the column
1298-
default later on.
1292+
In fact all the options that can be applied to a column description
1293+
in <command>CREATE TABLE</> can be used here. Keep in mind however
1294+
that the default value must satisfy the given constraints, or the
1295+
<literal>ADD</> will fail. Alternatively, you can add
1296+
constraints later (see below) after you've filled in the new column
1297+
correctly.
12991298
</para>
13001299
</sect2>
13011300

@@ -1390,12 +1389,17 @@ ALTER TABLE products ALTER COLUMN product_no DROP NOT NULL;
13901389
<programlisting>
13911390
ALTER TABLE products ALTER COLUMN price SET DEFAULT 7.77;
13921391
</programlisting>
1392+
Note that this doesn't affect any existing rows in the table, it
1393+
just changes the default for future <command>INSERT</> commands.
1394+
</para>
1395+
1396+
<para>
13931397
To remove any default value, use
13941398
<programlisting>
13951399
ALTER TABLE products ALTER COLUMN price DROP DEFAULT;
13961400
</programlisting>
1397-
This is equivalent to setting the default to null, at least in
1398-
<productname>PostgreSQL</>. As a consequence, it is not an error
1401+
This is equivalent to setting the default to null.
1402+
As a consequence, it is not an error
13991403
to drop a default where one hadn't been defined, because the
14001404
default is implicitly the null value.
14011405
</para>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy