Re:

From: Jason Earl <jdearl(at)yahoo(dot)com>
To: Steve SAUTETNER <ssa(at)informactis(dot)com>, Pgsql-General <pgsql-general(at)postgresql(dot)org>
Subject: Re:
Date: 2001-08-10 17:36:23
Message-ID: 20010810173623.72111.qmail@web10007.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

This is basically what foreign keys are designed to
do.

If you haven't used them before Bruce Momjian's online
book covers the details, and it is PostgreSQL
specific.

http://www.ca.postgresql.org/docs/awbook.html

As an example let's say that you had the following
simple tables:

CREATE TABLE customers (
id SERIAL PRIMARY KEY,
name varchar(50)
);

CREATE TABLE products (
id SERIAL PRIMARY KEY,
name varchar(50),
owner_id int
);

adding a simple constraint will do what you want:

ALTER TABLE products ADD CONSTRAINT owner_id_fk
FOREIGN KEY (owner_id) REFERENCES customers(id);

I tend to write that a little differently, in that I
include it in the table definition like so:

CREATE TABLE products (
id SERIAL PRIMARY KEY,
name varchar(50),
owner_id int REFERENCES customers(id)
);

There's a lot more that you can do with this nifty
tool, but Bruce tells the story much better than I
would.

Hope this is helpful,
Jason
--- Steve SAUTETNER <ssa(at)informactis(dot)com> wrote:
> Hello !
>
> I've got 2 databases. In the first i've got users
> described in a table
> (name, adress ..).
> In the second, i've got a table with products
> informations (id, price, owner
> ...).
> I must create a trigger in the second table on
> insert that verify that the
> product's owner exists in the first database.
>
> Anyone has an idea on how to do it ?
> (i can't merge the two databases)
>
> have a nice day !
>
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please
> send an appropriate
> subscribe-nomail command to majordomo(at)postgresql(dot)org
> so that your
> message can get through to the mailing list cleanly

__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/

In response to

  • at 2001-08-10 15:39:26 from Steve SAUTETNER

Browse pgsql-general by date

  From Date Subject
Next Message wsheldah 2001-08-10 17:43:15 Re: Re: Returned mail: Service unavailable
Previous Message Ryan Mahoney 2001-08-10 17:31:17 Re: Just Sports Usa, Supply Chain Management