Re: Multiple foreign keys

From: Oliver Elphick <olly(at)lfix(dot)co(dot)uk>
To: Prokopis Prokopidis <prokopis(at)ilsp(dot)gr>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Multiple foreign keys
Date: 2003-01-24 10:32:09
Message-ID: 1043404329.3068.9.camel@linda.lfix.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(dot)Elphick(at)lfix(dot)co(dot)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

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Prokopis Prokopidis 2003-01-24 10:54:54 Re: Multiple foreign keys
Previous Message Prokopis Prokopidis 2003-01-24 08:25:18 Multiple foreign keys