Search for restricting foreign keys

From: Benjamin Smith <lists(at)benjamindsmith(dot)com>
To: PostgreSQL General <pgsql-general(at)postgresql(dot)org>
Subject: Search for restricting foreign keys
Date: 2005-01-25 04:35:45
Message-ID: 200501242035.45649.lists@benjamindsmith.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Is there a way in PG 7.3, given a field, to find out what other tables &
records are linked to it via a foreign key? EG:

create table cities (id serial primary key,
title varchar not null);
insert into cities(title) values ('San Fransisco');
insert into cities(title) values ('Los Angeles');

create table stores (id serial primary key,
city integer not null references cities(id),
title varchar);
insert into stores(city, title) values (1, 'North City');
insert into stores(city, title) values (2, 'Central District');
insert into stores (city, title) values (1, 'Beachfront");

Given the above, and I wanted to know all the tables/records that relate to id
1, San Fransisco, and get a result something like:

table | primary key
stores | 1
stores | 3

Does such functionality exist in PG? Isn't it already doing essentially this
when attempting to delete a record with other records linked to it?

Currently, I do this by attempting to delete a record in a transaction, and
trap the error - it's a terrible way to do this, and sometimes I'm already in
a transaction.

-Ben
--
"The best way to predict the future is to invent it."
- XEROX PARC slogan, circa 1978

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Dave Bodenstab 2005-01-25 06:00:26 My problem upgrading to 8.0.0
Previous Message Neil Conway 2005-01-25 04:34:17 Re: Tablespaces and primary keys