two primairy key in one table ?

Lists: pgsql-general
From: Angela Luyf <a(dot)c(dot)luyf(at)amc(dot)uva(dot)nl>
To: pgsql-general(at)postgresql(dot)org
Subject: two primairy key in one table ?
Date: 2002-07-04 13:33:11
Message-ID: 3D244E96.DFDA5F1D@amc.uva.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

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 ?

A. Luyf


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


From: Josh Jore <josh(at)greentechnologist(dot)org>
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 15:13:49
Message-ID: Pine.BSO.4.44.0207051012440.812-100000@kitten.greentechnologist.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Oh sure, that's easy. Make the primary key be a table constraint instead
of just a single column.

CREATE TABLE foo (
a int,
b int,
PRIMARY KEY (a,b)
);

Joshua b. Jore ; http://www.greentechnologist.org

On Thu, 4 Jul 2002, Angela Luyf 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 ?
>
> A. Luyf
>
>
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
> message can get through to the mailing list cleanly
>
>
>


From: David A Dickson <davidd(at)saraswati(dot)wcg(dot)mcgill(dot)ca>
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 15:19:33
Message-ID: Pine.LNX.4.33.0207051117150.9778-100000@blues.wcg.mcgill.ca
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

A tabe defined as
CREATE TABLE foobar (
foo int,
bar int,
PRIMARY KEY (foo, bar) );

will allow many elements of type foo to be related to many elements of
type bar and vice versa. Each combiniation of foo and bar will be forced
unique.

On Thu, 4 Jul 2002, Angela Luyf 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 ?
>
> A. Luyf

--
David A Dickson
david(dot)dickson(at)mail(dot)mcgill(dot)ca


From: Darren Ferguson <darren(at)crystalballinc(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 15:28:09
Message-ID: Pine.LNX.4.44.0207051127240.2982-100000@thread.crystalballinc.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

CREATE TABLE foo (
id1 INTEGER NOT NULL,
id2 INTEGER NOT NULL,
CONSTRAINT foo_pk PRIMARY KEY(id1,id2)
);

HTH

On Thu, 4 Jul 2002, Angela Luyf 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 ?
>
> A. Luyf
>
>
>
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo(at)postgresql(dot)org so that your
> message can get through to the mailing list cleanly
>
>

--
Darren Ferguson


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


From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(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 17:07:09
Message-ID: 20020705100233.X27855-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


On Thu, 4 Jul 2002, Angela Luyf 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 ?

You can't have multiple primary keys in a table (per SQL spec) but are
you sure that's what you want as opposed to a single key made of two
columns?


From: Curt Sampson <cjs(at)cynic(dot)net>
To: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
Cc: Angela Luyf <a(dot)c(dot)luyf(at)amc(dot)uva(dot)nl>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: two primairy key in one table ?
Date: 2002-07-08 02:59:04
Message-ID: Pine.NEB.4.44.0207081152380.476-100000@angelic.cynic.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Fri, 5 Jul 2002, Stephan Szabo wrote:

> On Thu, 4 Jul 2002, Angela Luyf wrote:
>
> > I have a database model where a many to many relation is used, so i need
> > to create a table with two primary key.
>
> You can't have multiple primary keys in a table (per SQL spec) but are
> you sure that's what you want as opposed to a single key made of two
> columns?

I certainly read this as "two primary keys" in the sense of "two
candidate keys," and I was quite suprised that everybody else
interpreted this as "one primary key consisting of data from two
columns."

However, I don't see any problem here at all. That's because,
relationally speaking, I am of the opinion that the concept of a PRIMARY
KEY is entirely useless. If you've got two candidate keys on a table,
and unique constraints on both of them, there's nothing at all that
makes one better than the other.

Given that, you don't need to worry about having two primary, one or any
primary keys for a table; just make sure you have appropriate unique and
not null constraints for all of your candidate keys and you're set.

However, if you're using a lot of REFERENCES constraints, you might
declare the most frequent candidate key to be a PRIMARY KEY solely
becuase you then need declare only the table being referenced, not the
columns, in integrity constraints you're using in other tables. In other
words, PRIMARY KEY is a bit of syntatic sugar that can save you a bit of
typing.

cjs
--
Curt Sampson <cjs(at)cynic(dot)net> +81 90 7737 2974 http://www.netbsd.org
Don't you know, in this new Dark Age, we're all light. --XTC


From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Curt Sampson <cjs(at)cynic(dot)net>
Cc: Angela Luyf <a(dot)c(dot)luyf(at)amc(dot)uva(dot)nl>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: two primairy key in one table ?
Date: 2002-07-08 05:21:26
Message-ID: 20020707221726.A22659-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Mon, 8 Jul 2002, Curt Sampson wrote:

> On Fri, 5 Jul 2002, Stephan Szabo wrote:
>
> > On Thu, 4 Jul 2002, Angela Luyf wrote:
> >
> > > I have a database model where a many to many relation is used, so i need
> > > to create a table with two primary key.
> >
> > You can't have multiple primary keys in a table (per SQL spec) but are
> > you sure that's what you want as opposed to a single key made of two
> > columns?
>
> I certainly read this as "two primary keys" in the sense of "two
> candidate keys," and I was quite suprised that everybody else
> interpreted this as "one primary key consisting of data from two
> columns."
>
> However, I don't see any problem here at all. That's because,
> relationally speaking, I am of the opinion that the concept of a PRIMARY
> KEY is entirely useless. If you've got two candidate keys on a table,
> and unique constraints on both of them, there's nothing at all that
> makes one better than the other.

True, but, trying to use two separate candidate keys on each of the
linking columns alone won't really make a many to many relationship.
I'm assuming that the linkage is what is supposed to be unique here
not the individual parts.