pseudo-serial values in dual primary key?

From: Benjamin Smith <lists(at)benjamindsmith(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: pseudo-serial values in dual primary key?
Date: 2005-03-19 09:43:07
Message-ID: 200503190143.07508.lists@benjamindsmith.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Is it possible to have the equivalent of a serial data type in a table,
sub-categorized?

Assume the following:

create table categories (id serial, title varchar);

Now, I want to create an entries table, and by default, count serially by
category, so that category 1 has entries.sequence of 1, 2, 3, and so does
category 2. (where sequence= 1, 2, 3...) Something like:

create table entries (
categories_id integer not null references categories(id),
sequence default max(entries.sequence WHERE categories_id=this.categories_id),
primary key (categories_id, sequence)
);

I'm not sure about the semantics of this, but i want sequence to start at 1,
and count up, for its category as defined by categories_id. I already know
that I can set enforce the uniqueness of categories_id and sequence with thte
primary key, and I could just write some more app code to do a query to get
the max value of sequence where categories_id=$categories_id, but can this be
done without adding a bunch of xtra application code?

-Ben
--
"The best way to predict the future is to invent it."
- XEROX PARC slogan, circa 1978

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2005-03-19 16:21:49 Re: pg_dump fails with socket_not_open
Previous Message Sarah Ewen 2005-03-19 09:13:57 pg_dump fails with socket_not_open