Re: Multiple foreign keys - Mailing list pgsql-novice

From Oliver Elphick
Subject Re: Multiple foreign keys
Date
Msg-id 1043404329.3068.9.camel@linda.lfix.co.uk
Whole thread Raw
In response to Multiple foreign keys  (Prokopis Prokopidis <prokopis@ilsp.gr>)
List pgsql-novice
On Fri, 2003-01-24 at 08:25, Prokopis Prokopidis wrote:
> I was thinking of creating three foreign keys in the "comment" table,
> say "fk_student", "fk_teacher" and "fk_staff". Now, is there a way to
> impose a constraint that a comment row must have one and only one NOT
> NULL fk value?

Add this table constraint:
  CONSTRAINT "only one not null"
    CHECK ((fk_student IS NOT NULL AND fk_teacher IS NULL AND fk_student IS NULL)
       OR  (fk_student IS NULL AND fk_teacher IS NOT NULL AND fk_student IS NULL)
       OR  (fk_student IS NULL AND fk_teacher IS NULL AND fk_student IS NOT NULL)
          )

--
Oliver Elphick                                Oliver.Elphick@lfix.co.uk
Isle of Wight, UK                             http://www.lfix.co.uk/oliver
GPG: 1024D/3E1D0C1C: CA12 09E0 E8D5 8870 5839  932A 614D 4C34 3E1D 0C1C
                 ========================================
     "If anyone has material possessions and sees his
      brother in need but has no pity on him, how can the
      love of God be in him?"
                                    I John 3:17


pgsql-novice by date:

Previous
From: Rory Campbell-Lange
Date:
Subject: Re: Learning Plpgsql ??
Next
From: Prokopis Prokopidis
Date:
Subject: Re: Multiple foreign keys