returned the next time
the row is accessed, and applied when the table is rewritten, making
the <command>ALTER TABLE</command> very fast even on large tables.
</para>
<para>
If the default value is volatile (e.g., <function>clock_timestamp()</function>)
each row will need to be updated with the value calculated at the time
<command>ALTER TABLE</command> is executed. To avoid a potentially
lengthy update operation, particularly if you intend to fill the column
with mostly nondefault values anyway, it may be preferable to add the
column with no default, insert the correct values using
<command>UPDATE</command>, and then add any desired default as described
below.
</para>
</tip>
<para>
You can also define constraints on the column at the same time,
using the usual syntax:
<programlisting>
ALTER TABLE products ADD COLUMN description text CHECK (description <> '');
</programlisting>
In fact all the options that can be applied to a column description
in <command>CREATE TABLE</command> can be used here. Keep in mind however
that the default value must satisfy the given constraints, or the
<literal>ADD</literal> will fail. Alternatively, you can add
constraints later (see below) after you've filled in the new column
correctly.
</para>
</sect2>
<sect2 id="ddl-alter-removing-a-column">
<title>Removing a Column</title>
<indexterm>
<primary>column</primary>
<secondary>removing</secondary>
</indexterm>
<para>
To remove a column, use a command like:
<programlisting>
ALTER TABLE products DROP COLUMN description;
</programlisting>
Whatever data was in the column disappears. Table constraints involving
the column are dropped, too. However, if the column is referenced by a
foreign key constraint of another table,
<productname>PostgreSQL</productname> will not silently drop that
constraint. You can authorize dropping everything that depends on
the column by adding <literal>CASCADE</literal>:
<programlisting>
ALTER TABLE products DROP COLUMN description CASCADE;
</programlisting>
See <xref linkend="ddl-depend"/> for a description of the general
mechanism behind this.
</para>
</sect2>
<sect2 id="ddl-alter-adding-a-constraint">
<title>Adding a Constraint</title>
<indexterm>
<primary>constraint</primary>
<secondary>adding</secondary>
</indexterm>
<para>
To add a constraint, the table constraint syntax is used. For example:
<programlisting>
ALTER TABLE products ADD CHECK (name <> '');
ALTER TABLE products ADD CONSTRAINT some_name UNIQUE (product_no);
ALTER TABLE products ADD FOREIGN KEY (product_group_id) REFERENCES product_groups;
</programlisting>
</para>
<para>
To add a not-null constraint, which is normally not written as a table
constraint, this special syntax is available:
<programlisting>
ALTER TABLE products ALTER COLUMN product_no SET NOT NULL;
</programlisting>
This command silently does nothing if the column already has a
not-null constraint.
</para>
<para>
The constraint will be checked immediately, so the table data must
satisfy the constraint before it can be added.
</para>
</sect2>
<sect2 id="ddl-alter-removing-a-constraint">
<title>Removing a Constraint</title>
<indexterm>
<primary>constraint</primary>
<secondary>removing</secondary>
</indexterm>
<para>
To remove a constraint you need to know its name. If you gave it
a name then that's easy. Otherwise the system assigned a
generated name, which you need to find out. The
<application>psql</application> command <literal>\d
<replaceable>tablename</replaceable></literal> can be helpful
here; other interfaces might also provide a way to inspect table
details. Then the command is:
<programlisting>
ALTER TABLE products DROP CONSTRAINT some_name;
</programlisting>
</para>