pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Randall Lucas <rlucas@tercent.net>
To: Michael Teter <michael_teter@yahoo.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: UNIQUE boolean: only one row may be "true"
Date: Tue, 6 May 2003 14:46:51 -0400
Message-ID: <19ACA479-7FF3-11D7-A71E-000A957653D6@tercent.net> (raw)
In-Reply-To: <20030506171349.3277.qmail@web10001.mail.yahoo.com>


Hi Michael,

Per Bruno's suggestion, I think I will go with a partial index.  But 
your question brings up a good point and I am happy to hear what you 
have to say on the design.

The problem being solved is essentially replicating a very limited 
subset of SQL DDL to describe an external tabular dataset.  For a given 
dataset (thing_group) consisting of a number of columns (thing), there 
must be no more than one column which is a primary key 
(is_main_thing_p).  Do you have any suggestions on how this might 
better be implemented?  It seems to me that with a partial index, there 
is not too much overhead and it doesn't seem too offensively 
un-boycecoddean.

Best,

Randall

On Tuesday, May 6, 2003, at 01:13 PM, Michael Teter wrote:

> Can you elaborate a little on what you want to do with this (as in, 
> example
> data, problem domain, etc.)?
>
> This is probably not the answer you're looking for, but without 
> knowing more,
> I would guess that you need a different design, not a 1-true,many 
> false field
> constraint.
>
> Perhaps you want something like this?
>
> Table A:
> thing_groupid (primary key)
> ...other group info...
>
> Table B:
> thing_groupid (reference to A.thing_groupid, many to 1)
> thing_id
> ...details about thing...
>
> ?
>
> MT
>
> --- Randall Lucas <rlucas@tercent.net> wrote:
>>
>> Hi folks,
>>
>> After a late night of SQL hacking, my brain fizzed out:
>>
>> How shall I create a constraint such that only one row may be 'true'?
>> Rephrased, may I place a WHERE clause in a UNIQUE constraint, or
>> alternatively, may I use a CHECK constraint with an aggregate?
>>
>> Example:
>>
>> CREATE TABLE thing (
>>   thing_id 			serial primary key,
>>   thing_group_id 	int not null references thing_group(thing_group_id),
>>   is_main_thing_p 	boolean not null default 'f',
>> -- there may be only one main thing per group:
>>   unique (thing_group_id, is_main_thing_p='t')
>> -- or else something like:
>> -- check (count (*) from thing where 
>> thing_group_id=NEW.thing_group_id 
>> and is_main_thing_p = 't' <2)
>> );
>>
>> Best,
>>
>> Randall
>>
>>
>> ---------------------------(end of 
>> broadcast)---------------------------
>> TIP 3: if posting/reading through Usenet, please send an appropriate
>> subscribe-nomail command to majordomo@postgresql.org so that your
>> message can get through to the mailing list cleanly
>
>
> __________________________________
> Do you Yahoo!?
> The New Yahoo! Search - Faster. Easier. Bingo.
> http://search.yahoo.com
>




view thread (7+ messages)  latest in thread

Message-ID: <19ACA479-7FF3-11D7-A71E-000A957653D6@tercent.net>
Permalink:  ../19ACA479-7FF3-11D7-A71E-000A957653D6@tercent.net/
Also on:    postgresql.org/message-id/19ACA479-7FF3-11D7-A71E-000A957653D6@tercent.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: rlucas@tercent.net, michael_teter@yahoo.com
  Subject: Re: UNIQUE boolean: only one row may be "true"
  In-Reply-To: <19ACA479-7FF3-11D7-A71E-000A957653D6@tercent.net>
  References: <20030506171349.3277.qmail@web10001.mail.yahoo.com> <19ACA479-7FF3-11D7-A71E-000A957653D6@tercent.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox