Primary and Foreign Key?

Lists: pgsql-novice
From: "A(dot) Banks" <abanks(at)insightbb(dot)com>
To: <pgsql-novice(at)postgresql(dot)org>
Subject: Primary and Foreign Key?
Date: 2005-09-22 00:46:10
Message-ID: KBEKKNMFLELKGIADDEPEOELICAAA.abanks@insightbb.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

I have the following related tables:

PEOPLE
--------------
peopleid pkey,
name,
etc

GROUPS
-------------
groupid pkey,
description,
etc

PEOPLEGROUPS
-------------------
peopleid pkey/fkey,
groupid pkey/fkey

What is the CORRECT way (in Postgres) to define the PEOPLEGROUPS table so
that it has both the double primary key AND still acts as a foreign key for
people.peopleid and groups.groupid? Can i specify both or is this not
necessary? Any suggestions would be appreciated.

Thanks!
--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.344 / Virus Database: 267.11.3/107 - Release Date: 9/20/2005


From: "Wayne Unruh" <waynepunruh(at)hotmail(dot)com>
To: abanks(at)insightbb(dot)com, pgsql-novice(at)postgresql(dot)org
Subject: Re: Primary and Foreign Key?
Date: 2005-09-24 06:45:45
Message-ID: BAY106-F12E52354080C494806E050AF890@phx.gbl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

In relational databases A table can have at most one primary key. However,
you are not required to have a primary key, so I would just have the two
foreign keys in the table PEOPLEGROUPS.

For more information on Constraints see
http://www.postgresql.org/docs/8.0/interactive/ddl-constraints.html

Wayne Unruh

From : A. Banks <abanks(at)insightbb(dot)com>
Sent : Wednesday, September 21, 2005 8:46 PM
To : <pgsql-novice(at)postgresql(dot)org>
Subject : [NOVICE] Primary and Foreign Key?

I have the following related tables:

PEOPLE
--------------
peopleid pkey,
name,
etc

GROUPS
-------------
groupid pkey,
description,
etc

PEOPLEGROUPS
-------------------
peopleid pkey/fkey,
groupid pkey/fkey

What is the CORRECT way (in Postgres) to define the PEOPLEGROUPS table so
that it has both the double primary key AND still acts as a foreign key for
people.peopleid and groups.groupid? Can i specify both or is this not
necessary? Any suggestions would be appreciated.

Thanks!


From: Oliver Elphick <olly(at)lfix(dot)co(dot)uk>
To: "A(dot) Banks" <abanks(at)insightbb(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Primary and Foreign Key?
Date: 2005-09-24 11:31:48
Message-ID: 1127561508.10691.11.camel@linda.lfix.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

On Wed, 2005-09-21 at 19:46 -0500, A. Banks wrote:
> I have the following related tables:
>
> PEOPLE
> --------------
> peopleid pkey,
> name,
> etc
>
>
> GROUPS
> -------------
> groupid pkey,
> description,
> etc
>
>
> PEOPLEGROUPS
> -------------------
> peopleid pkey/fkey,
> groupid pkey/fkey
>
>
> What is the CORRECT way (in Postgres) to define the PEOPLEGROUPS table so
> that it has both the double primary key AND still acts as a foreign key for
> people.peopleid and groups.groupid? Can i specify both or is this not
> necessary? Any suggestions would be appreciated.

Your terminology is a bit wrong; there can only ever be one primary key
for a table; what you want is probably a single primary key made up of
two columns.

Your definition will look like this:

CREATE TABLE people (
peopleid SERIAL PRIMARY KEY, -- SERIAL is an INTEGER that autoincrements
-- if no value is supplied on insertion
name TEXT NOT NULL,
...
);

CREATE TABLE groups (
groupid SERIAL PRIMARY KEY,
name TEXT NOT NULL,
...
);

CREATE TABLE peoplegroups (
peopleid INTEGER
REFERENCES people (peopleid)
ON UPDATE CASCADE ON DELETE NO ACTION,
groupid INTEGER
REFERENCES people (peopleid)
ON UPDATE CASCADE ON DELETE NO ACTION,
PRIMARY KEY (peopleid, groupid)
);
--
Oliver Elphick olly(at)lfix(dot)co(dot)uk
Isle of Wight http://www.lfix.co.uk/oliver
GPG: 1024D/A54310EA 92C8 39E7 280E 3631 3F0E 1EC0 5664 7A2F A543 10EA
========================================
Do you want to know God? http://www.lfix.co.uk/knowing_god.html