Re: Database design question

From: David Fetter <david_at_fetter.org>
Date: Tue, 27 Jun 2006 19:38:28 -0500
Message-ID: <mbCdncQ2irAZUjzZnZ2dnUVZ_q-dnZ2d_at_speakeasy.net>


sreedhardasi_at_gmail.com wrote:
> I have a database design questions. How can we specify one of two
> columns in a table should be not null? In other words, if one
> column is null then another column should not be null.

In general, you should be able to use a CHECK constraint, something like this:

CREATE TABLE foo (

    bar INTEGER,
    baz INTEGER,
    CHECK (

        (CASE WHEN bar IS NULL THEN 0 ELSE 1 END) +
        (CASE WHEN baz IS NULL THEN 0 ELSE 1 END) 
        = 1

    )
);

I can't really speak to the particular design, and very likely somebody will take me to task for failure to scream about how NULLs don't belong in any design. ;)

Cheers,
D

-- 
David Fetter <david_at_fetter.org> http://fetter.org/
phone: +1 415 235 3778        AIM: dfetter666
                              Skype: davidfetter

Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it.
                                       Kernighan
Received on Wed Jun 28 2006 - 02:38:28 CEST

Original text of this message