Thursday, July 24, 2008

Re: [GENERAL] Sequence

Alex Cheshev wrote:
> Hello.
> A table has two primary keys:

It appears that you really meant "a table has a two-field composite
primary key". There can only be one primary key for a table, that's the
whole point - but the primary key can be composed of more than one field.

[Note: if you format your SQL when you post, more people will bother to
read it and try to help you out. For example, your CREATE TABLE could've
been better written as:]

> CREATE TABLE example (
> pk1 integer,
> pk2 integer,
> PRIMARY KEY (pk1, pk2)
> );

> To add a new record I use command:

> INSERT INTO example (pk1, pk2) VALUES (0, 0).

> Before adding the new
> record I have to find out the last value of pk2. How can I use something
> like this:

> INSERT INTO example (pk1, pk2) VALUES (0, nextval('pk2'))

> ?

> If a table just has one primary key I can use sequence (CREATE
> SEQUENCE). What about two primary keys?

You can still use a SERIAL type or manually use CREATE SEQUENCE and
nextval() .

I suspect I'm missing the point of your question, though. Perhaps if you
gave a real-world example of what you are trying to do, with meaningful
field names?

--
Craig Ringer

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

No comments: