Re: Multiple foreign keys

From: Prokopis Prokopidis <prokopis(at)ilsp(dot)gr>
To: Oliver Elphick <olly(at)lfix(dot)co(dot)uk>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Multiple foreign keys
Date: 2003-01-24 10:54:54
Message-ID: 3E311B7E.7010901@ilsp.gr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Yes, but "3 tables" was just an example. What if I have 10 or 1000
tables that need a comment? This CHECK solution becomes "ugly" very
quickly. There must be another way ...

Thanks Oliver.

P.

Oliver Elphick wrote:
> 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)
> )
>

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Oliver Elphick 2003-01-24 10:56:02 Re: Stored Procedure Question
Previous Message Oliver Elphick 2003-01-24 10:32:09 Re: Multiple foreign keys