On Sun, Sep 10, 2000 at 12:43:08AM -0400, Neil Conway wrote:
> 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
> /* ... */
> );
Something like
CREATE TABLE messages (
/* ... */
poster INT4 NOT NULL REFERENCES users,
thread INT4 NOT NULL REFERENCES threads,
);
CREATE TABLE users (
id serial PRIMARY KEY
/* ... */
);
CREATE TABLE threads (
id serial PRIMARY KEY
/* ... */
);
cf. Bruce's book:
http://www.postgresql.org/docs/aw_pgsql_book/node156.html
Cheers,
Patrick