Re: Adding a NOT NULL column?

From: "Gregory Wood" <gregw(at)com-stock(dot)com>
To: "Philip Molter" <philip(at)datafoundry(dot)net>
Cc: "PostgreSQL-General" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Adding a NOT NULL column?
Date: 2001-06-26 19:10:33
Message-ID: 002801c0fe73$aeb33c50$7889ffcc@comstock.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> but how do set that new column to be NOT NULL? The postgres docs say
> do something like:
>
> ALTER TABLE t ADD (table constraint definition)
>
> so I would do that like:
>
> ALTER TABLE ADD c NOT NULL
> ALTER TABLE ADD CONSTRAINT c NOT NULL
>
> each time it fails on the 'NOT'. How do I add that constraint?

I believe you can either ALTER the column (much like you did when you added
the default value: ALTER TABLE t ALTER c SET DEFAULT 0), or ADD a CHECK
constraint which checks that 'c IS NOT NULL'. I don't think you can simply
*add* a 'NOT NULL' constraint to a column after the fact.

Both of these should work fine, although I'm more comfortable with the
syntax of the first:

ALTER TABLE ALTER COLUMN c NOT NULL;
-OR-
ALTER TABLE ADD CONSTRAINT CHECK (c IS NOT NULL);

Greg

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Thalis A. Kalfigopoulos 2001-06-26 19:12:55 Re: Large objects in web applications
Previous Message Shaun Thomas 2001-06-26 19:00:16 Re: More Red Hat information