Composed Key and autoincrement

From: fbn <fbn79(at)libero(dot)it>
To: pgsql-novice(at)postgresql(dot)org
Subject: Composed Key and autoincrement
Date: 2007-02-05 16:55:30
Message-ID: 45C76182.9070001@libero.it
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hello,
I want to create a table with a primary key composed of 2 auto increment
(serial) value.
I don't speak english very well so I'gonna write you an example to let
you understand what's
my problem. Hope someone can help me.

For example

CREATE TABLE foo(
keypartone serial not null,
keyparttwo serial not null,
var1 integer not null,
PRIMARY KEY( keypartone , keyparttwo )
);

If I do this inserts

INSERT INTO foo ( var1 ) VALUES ( 7 );
INSERT INTO foo ( var1 ) VALUES ( 9 );
INSERT INTO foo ( keypartone, var1 ) VALUES ( 1, 111 );
INSERT INTO foo ( keypartone, var1 ) VALUES ( 2, 3 );

I get this table

| 1 | 1 | 7 |
| 2 | 2 | 9 |
| 1 | 3 | 111 |
| 2 | 4 | 3 |

There is a way to program the 2 sequences to have the following result
instead???

INSERT INTO foo ( var1 ) VALUES ( 7 );
INSERT INTO foo ( var1 ) VALUES ( 9 );
INSERT INTO foo ( keypartone, var1 ) VALUES ( 1, 111 );
INSERT INTO foo ( keypartone, var1 ) VALUES ( 2, 3 );

| 1 | 1 | 7 |
| 2 | 1 | 9 |
| 1 | 2 | 111 |
| 2 | 2 | 3 |

There is a way to do this?

Thank you a lot
Taioli Fabiano

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Jasbinder Singh Bali 2007-02-06 03:42:33 Re: Stored Procedure to return a result set
Previous Message Phillip Smith 2007-02-04 21:48:47 Re: If Statement