Foreign key with check?
Foreign key with check?
От:
Jeff Boes <jeff@endpoint.com>
Дата:
Given a table like this: create table primary ( a integer primary key, b boolean ); And another like this: create table secondary ( a integer, some_other_fields ); I would like a foreign key constraint on the "secondary" table that looks something like: foreign key (a, true) references primary (a, b) That is, a row in "secondary" is allowed to reference a row in "primary" if and only if that referenced row has (b = true). I think I have to do this with an additional trigger. Any other options?
Re: Foreign key with check?
От:
Bruno Wolff III <bruno@wolff.to>
Дата:
On Wed, Jul 27, 2005 at 16:08:19 -0400, Jeff Boes wrote: > Given a table like this: > > create table primary ( > a integer primary key, > b boolean > ); > > And another like this: > > create table secondary ( > a integer, > some_other_fields > ); > > > I would like a foreign key constraint on the "secondary" table that > looks something like: > > foreign key (a, true) references primary (a, b) > > That is, a row in "secondary" is allowed to reference a row in "primary" > if and only if that referenced row has (b = true). If what you are saying is that all entries in secondary need to reference a row in primary and in addition the referenced row must have b true, then you can do this fairly simply using a bit of extra space. Add a row b to secondary with a constraint that b = true. Then make the foreign key reference on both a and b.