Обсуждение: Sequence
Hello. A table has two primary keys: 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?
am Fri, dem 25.07.2008, um 15:54:23 +1100 mailte Alex Cheshev folgendes: > Hello. > A table has two primary keys: 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 also use a sequence. Regards, Andreas -- Andreas Kretschmer Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header) GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net
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