Re: two primairy key in one table ? - Mailing list pgsql-general

From Manfred Koizar
Subject Re: two primairy key in one table ?
Date
Msg-id 1eu8iu0lltlr1t2ccqro8h4fko2j34lmev@4ax.com
Whole thread Raw
List pgsql-general
On Thu, 04 Jul 2002 15:33:11 +0200, Angela Luyf <a.c.luyf@amc.uva.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

pgsql-general by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: I am being interviewed by OReilly
Next
From: Tom Lane
Date:
Subject: Re: repeatable crash generating two column index