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

From Alvaro Herrera
Subject Re: two primairy key in one table ?
Date
Msg-id Pine.LNX.4.44.0207051204540.2200-100000@cm-lcon-46-187.cm.vtr.net
Whole thread Raw
In response to two primairy key in one table ?  (Angela Luyf <a.c.luyf@amc.uva.nl>)
List pgsql-general
Angela Luyf dijo:

> 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 ?

CREATE TABLE twos (one INT, two INT, PRIMARY KEY (one, two));

But I don't think that's the solution for many-to-many relations.
Should be more like:

CREATE TABLE first (one SERIAL PRIMARY KEY);
CREATE TABLE second (two SERIAL PRIMARY KEY);

and the table with foreign constraints should be:

CREATE TABLE third (one INT REFERENCES first NOT NULL,
    two INT REFERENCES first NOT NULL,
    PRIMARY KEY (one, two)
    );

BTW, this is in the "table_constraint" part of the grammar.

HTH,

--
Alvaro Herrera (<alvherre[a]atentus.com>)
"The ability to monopolize a planet is insignificant
next to the power of the source"




pgsql-general by date:

Previous
From: "Nigel J. Andrews"
Date:
Subject: Re: Time bug fixed?
Next
From: Bruce Momjian
Date:
Subject: Re: plpgsql: PERFORM SELECT id INTO TEMP TABLE ..