agora inbox for pgsql-novice@postgresql.org  
help / color / mirror / Atom feed
[NOVICE] Primary and Foreign Key?
6+ messages / 4 participants
[nested] [flat]

* [NOVICE] Primary and Foreign Key?
@ 1970-01-01 00:00  
  0 siblings, 0 replies; 6+ messages in thread

From:  @ 1970-01-01 00:00 UTC (permalink / raw)
  To: 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!





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* [NOVICE] Primary and Foreign Key?
@ 1970-01-01 00:00  
  0 siblings, 0 replies; 6+ messages in thread

From:  @ 1970-01-01 00:00 UTC (permalink / raw)
  To: 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 incoming message.
Checked by AVG Anti-Virus.
Version: 7.0.344 / Virus Database: 267.11.5/110 - Release Date: 9/22/2005


--
No virus found in this incoming message.
Checked by AVG Anti-Virus.
Version: 7.0.344 / Virus Database: 267.11.5/110 - Release Date: 9/22/2005




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Primary and Foreign Key?
@ 2005-09-22 00:46  A. Banks <abanks@insightbb.com>
  0 siblings, 2 replies; 6+ messages in thread

From: A. Banks @ 2005-09-22 00:46 UTC (permalink / raw)
  To: 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




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Primary and Foreign Key?
@ 2005-09-24 06:45  Wayne Unruh <waynepunruh@hotmail.com>
  parent: A. Banks <abanks@insightbb.com>
  1 sibling, 0 replies; 6+ messages in thread

From: Wayne Unruh @ 2005-09-24 06:45 UTC (permalink / raw)
  To: abanks@insightbb.com; 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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Primary and Foreign Key?
@ 2005-09-24 11:31  Oliver Elphick <olly@lfix.co.uk>
  parent: A. Banks <abanks@insightbb.com>
  1 sibling, 0 replies; 6+ messages in thread

From: Oliver Elphick @ 2005-09-24 11:31 UTC (permalink / raw)
  To: A. Banks <abanks@insightbb.com>; +Cc: 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@lfix.co.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




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Primary and Foreign Key?
@ 2005-09-24 14:55  Announce <truthhurts@insightbb.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Announce @ 2005-09-24 14:55 UTC (permalink / raw)
  To: pgsql-novice

I think i found what i was looking for in the docs:

5.3.4. Primary Keys
Technically, a primary key constraint is simply a combination of a unique
constraint and a not-null constraint.

So i suppose i suppose i would use PRIKARY KEY(peopleid,groupid) to enforce
the UNIQUE NOT NULL constraint in one expression.

Thanks!
-----Original Message-----
From: Wayne Unruh [mailto:waynepunruh@hotmail.com]
Sent: Saturday, September 24, 2005 1:46 AM
To: abanks@insightbb.com; pgsql-novice@postgresql.org
Subject: RE: [NOVICE] Primary and Foreign Key?


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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2005-09-24 14:55 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1970-01-01 00:00 [NOVICE] Primary and Foreign Key? 
1970-01-01 00:00 [NOVICE] Primary and Foreign Key? 
2005-09-22 00:46 Primary and Foreign Key? A. Banks <abanks@insightbb.com>
2005-09-24 06:45 ` Wayne Unruh <waynepunruh@hotmail.com>
2005-09-24 11:31 ` Oliver Elphick <olly@lfix.co.uk>
2005-09-24 14:55 Re: Primary and Foreign Key? Announce <truthhurts@insightbb.com>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox