Re: Constraints for grouping

From: Richard Broersma Jr <rabroersma(at)yahoo(dot)com>
To: PostgreSQL Admin <postgres(at)productivitymedia(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Constraints for grouping
Date: 2007-09-03 20:35:07
Message-ID: 602367.49974.qm@web31801.mail.mud.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

--- PostgreSQL Admin <postgres(at)productivitymedia(dot)com> wrote:
> I have a question that I've thought in my head about using triggers, but
> I figure to ask people that do SQL more than I. So, I have a table that
> I want two columns.
>
> (1) A featured column which is for only 1 row, once it switched to
> another row than all other rows must be false
>
> title | author_id | categories | featured
> --------------------------------+-----------+------+-----
> Thierry Beta Release | 3 | 11 | True
> Different Approach | 3 | 11 |
> Ottawa Does Not Heart Hip-Hop | 3 | 11 |
>
> (2) A featured column by category and only allow category_feature equal
> to the number of categories.
>
> Is SQL the way to go (and how - ideas), or should I use python for the
> heavy lifting?

Are you saying that you have two featured columns? One column for category features and the other
for article feature?

If I understand you correctly, Item 1 can be achieved nicely with partial indexes.
for example, if you only want to allow one row in the entire table to have "feature"= true:

CREATE INDEX Only_one_row_true
ON Your_table ( featured )
WHERE featured = true;

Or if you want to only allow 1 featured article per catagory then:

CREATE INDEX Only_one_row_true_per_catagory
ON Your_table ( catigories, featured )
WHERE featured = true;

This will ensure that the sum( featured = true ) <= sum( unique( catagories )).

Regards,
Richard Broersma Jr.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Richard Broersma Jr 2007-09-03 20:40:06 Re: Constraints for grouping
Previous Message PostgreSQL Admin 2007-09-03 18:31:37 Constraints for grouping