Re: Reference to multiple cols

From: "Josh Berkus" <josh(at)agliodbs(dot)com>
To: "Ville Jungman" <ville_jungman(at)hotmail(dot)com>, pgsql-novice(at)postgresql(dot)org
Subject: Re: Reference to multiple cols
Date: 2003-01-11 22:00:14
Message-ID: web-2311056@davinci.ethosmedia.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Ville,

> I want to make a table with a column that references to multiple
> tables. Is that possible? Look at the 3rd row:
>
> 1. create table dog(barking_volume int,slobber_amount int);
> 2. create table cat(laziness int);
> 3. create table animals(name text,ref_animal oid references cat(oid)
> and dog(oid) );

First off, I reccommend against using the OID as your keying system.
The OID is used for specific system purposes, some of which may
interfere with using is as a primary and foriegn key. Use SERIAL
columns instead.

Second, the normal relational way to do the above would be:

create table animal( animal_id SERIAL PRIMARY KEY, name TEXT NOT NULL
);
create table dog( animal_id INT PRIMARY KEY REFERENCES animals
(animal_id), barking_volume INT, slobber INT );
create table cat( animal_id INT PRIMARY KEY REFERENCES animals
(animal_id), lazyness INT, shedding_amount INT );

This should give you a system in which animal_id is the primary key for
each table, and therefore there is a one-for-one relationship between
the animal table and each of the dog and cat tables, and would prevent
you from deleting a referenced record from the animal table.

You would need an additional trigger to prevent duplication *between*
the dog and cat tables.

-Josh Berkus

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Ville Jungman 2003-01-11 23:20:06 Re: Reference to multiple cols
Previous Message p. matthew knox 2003-01-11 17:12:26 Re: Stalled post to pgsql-novice