Re: adding 'not-null' and 'unique'-constraint for existing

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: <pilsl(at)goldfisch(dot)at>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: adding 'not-null' and 'unique'-constraint for existing
Date: 2002-01-28 17:26:38
Message-ID: 20020128092004.B76102-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Mon, 28 Jan 2002 pilsl(at)goldfisch(dot)at wrote:

> I've an existing column that should be set to 'not-null' and 'unique'
> but I dont find out the corresponding constraint for the
> "ALTER TABLE tablename ADD CONSTRAINT .... "

Assuming you're using 7.1, add constraint will only let you add foreign
key constraint (iirc). In many cases a dump and restore of the table is
easiest (editing the dump), however, you can get the constraints if you
don't mind a little work:

The unique constraint can be effectively added by making a unique index
on the columns. Not null requires a little hack to a system table.
You'll want to set attnotnull for the column in pg_attribute, something
like UPDATE pg_attribute set attnotnull=true where attrelid=(select oid
from pg_class where relname='<table name>') and attname='<column name>';

7.2 is friendlier since you can directly add unique and you can add a
check constraint to prevent nulls.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Johnson, Shaunn 2002-01-28 18:52:22 Postgres 7.1.3 and new kernel
Previous Message Trond Eivind =?iso-8859-1?q?Glomsr=F8d?= 2002-01-28 16:57:59 Re: NEWBIE: Can't unpack RPMs