Re: two primairy key in one table ?

From: Manfred Koizar <mkoi-pg(at)aon(dot)at>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: two primairy key in one table ?
Date: 2002-07-04 16:48:04
Message-ID: 1eu8iu0lltlr1t2ccqro8h4fko2j34lmev@4ax.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, 04 Jul 2002 15:33:11 +0200, Angela Luyf <a(dot)c(dot)luyf(at)amc(dot)uva(dot)nl>
wrote:
>Hello,
>I have a database model where a many to many relation is used, so i need
>to create a table with two primary key. Don't know how to solve this in
>postgress, can't find it in the tutorials, so can anybody help me with
>this ?
>
Angela,

you most probably need a primary key with two columns. Does this come
near to what you want?

CREATE TABLE a (
id INT NOT NULL PRIMARY KEY
/* , anything else ... */
);

CREATE TABLE b (
id INT NOT NULL PRIMARY KEY
/* , anything else ... */
);

CREATE TABLE atob (
a_id INT NOT NULL REFERENCES a,
b_id INT NOT NULL REFERENCES b,
PRIMARY KEY (a_id, b_id)
);

For performance reasons you might want to
CREATE UNIQUE INDEX idx_atob ON atob (b_id, a_id);

HTH.
Servus
Manfred

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Do Not Spam 2002-07-04 16:58:04 Time bug fixed?
Previous Message Bruce Momjian 2002-07-04 16:17:58 Re: I am being interviewed by OReilly