FOREIGN KEY questions

From: Neil Conway <nconway(at)klamath(dot)dyndns(dot)org>
To: PostgreSQL General <pgsql-general(at)postgresql(dot)org>
Subject: FOREIGN KEY questions
Date: 2000-09-10 04:43:08
Message-ID: 20000910004308.A1043@klamath.dyndns.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I'm having a bit of difficulty understanding the correct usage of
foreign keys (I've never used them before, excuse my ignorance).
Here's a situation where I'm trying to figure out how/if they
should be used:

One database has a group of tables with intereferential data.
For example:

CREATE TABLE messages (
/* ... */
poster INT4 NOT NULL,
thread INT4 NOT NULL
);

CREATE TABLE users (
id serial
/* ... */
);

CREATE TABLE threads (
id serial
/* ... */
);

messages.poster should refer to a valid (and unique) users.id - that
is the person who posted the message. If there is no users.id with
the same value as every messages.poster , there is a problem. The
same applies to message.thread and threads.id .

How do I check that this data is being entered correctly? Is this
a situation where I should be using foreign keys? Or can this only
be done with external logic (i.e. the application doing the inserts
should check first that the data it's receiving is valid).

Thanks in advance,

Neil

--
Neil Conway <neilconway(at)home(dot)com>
Get my GnuPG key from: http://klamath.dyndns.org/mykey.asc
Encrypted mail welcomed

We hang the petty thieves and appoint the great ones to public office.
-- Aesop

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Patrick Welche 2000-09-10 11:46:43 Re: FOREIGN KEY questions
Previous Message Christopher M. Jones 2000-09-10 00:55:34 Install on Windows 2000