Re: Foreign keys?

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: "Dr(dot) Evil" <drevil(at)sidereal(dot)kz>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Foreign keys?
Date: 2001-07-13 01:19:44
Message-ID: Pine.BSF.4.21.0107121817390.7293-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 13 Jul 2001, Dr. Evil wrote:

>
> There doesn't seem to be much documentation on foreign keys and how to
> use them properly.
>
> I am setting up a DB with two tables: users, and a table of objects
> where are always owned by users. I want to make sure that there's no
> way to have an object which isn't owned. Let's say these are the
> tables:
>
> The constraint is that there should never be a row in the object table
> where the owner column has a number which doesn't have a corresponding
> owner in the user table.
>
> I'm sure I can do something with foreign keys to implement this
> constraint, but I can't figure it out.

You need to define user.number as unique and add the constraint, something
like:

create table user (
number int4 unique,
name varchar(400)
);

create table object (
owner int4 references user(number),
description varchar(200)
)

However, there's different options based on what you want it to do in the
case you modify or delete rows out of user.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Joseph Shraibman 2001-07-13 01:54:52 Re: Postgresql revisited. Some questions about the product
Previous Message Jason Earl 2001-07-13 01:17:41 Re: Foreign keys?