Re: How to create unique constraint on NULL columns

From: Richard Huxton <dev(at)archonet(dot)com>
To: Andrus <eetasoft(at)online(dot)ee>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: How to create unique constraint on NULL columns
Date: 2005-07-18 17:29:47
Message-ID: 42DBE70B.6020203@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Andrus wrote:
>>I was faced with a similar issue. One suggestion I got from the Internet
>>was to create a shadow column that contains the values used in the Index,
>>with a dummy entry (in my case, the string <NULL>) for those records in
>>which the primary column is NULL. It works well for my app.
>
>
> Michael,
>
> thank you.
> This is not working in my case. Uniquenes sis not sufficient: I need
> referential integrity with uniqueness. If I add <NULL> department, this must
> be added into department table also and pollutes this table.

Although others have suggested that you're going to need an additional
table, I've not seen anyone explicitly state why this is causing you
problems.

Clearly, NULL should not be used to mean "Any". However, the only reason
you're doing this is because you want a FK to the "department" table.
Nothing wrong with that, except that you're not storing a department-ID.
How do I know? Because you want to store "Any" and that isn't a
department-ID.

So - the obvious solution is a "department_access" table that you can
refer to, and which in turn refers to various departments.

Of course, that's a fair bit of work and you'd like a short-cut. I'm
afraid there isn't one. Half the problems I have with systems I design
are where I've tried to save effort now and end up paying twice over
later on.

--
Richard Huxton
Archonet Ltd

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Scott Marlowe 2005-07-18 17:31:49 Re: How to find the number of rows deleted
Previous Message Andrus 2005-07-18 17:02:30 Re: How to create unique constraint on NULL columns