Re: two primairy key in one table ?

From: Alvaro Herrera <alvherre(at)atentus(dot)com>
To: Angela Luyf <a(dot)c(dot)luyf(at)amc(dot)uva(dot)nl>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: two primairy key in one table ?
Date: 2002-07-05 16:14:38
Message-ID: Pine.LNX.4.44.0207051204540.2200-100000@cm-lcon-46-187.cm.vtr.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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"

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Laurette Cisneros 2002-07-05 16:18:21 Re: epoch from date field
Previous Message Leland F. Jackson, CPA 2002-07-05 16:10:40 Re: [INTERFACES] MS Access 2K Inform User on Record changes