Re: Looking up table names by REFERENCES

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Steve Castellotti <SteveC(at)innocent(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Looking up table names by REFERENCES
Date: 2005-01-25 19:10:45
Message-ID: 20050125191045.GA78147@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Tue, Jan 25, 2005 at 04:09:09AM +1300, Steve Castellotti wrote:

> Surely there's a simple way I can trace REFERENCES in a particular
> column across tables?

The pg_constraint table contains foreign key constraints. Here's
an example query that appears to work in trivial tests:

SELECT c.conname,
c.conrelid::regclass, a1.attname,
c.confrelid::regclass, a2.attname AS fattname
FROM pg_constraint AS c
JOIN pg_attribute AS a1 ON a1.attrelid = c.conrelid AND a1.attnum = ANY (c.conkey)
JOIN pg_attribute AS a2 ON a2.attrelid = c.confrelid AND a2.attnum = ANY (c.confkey)
WHERE c.contype = 'f';

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Michael Fuhr 2005-01-25 19:19:38 Re: Moving from Transact SQL to PL/pgSQL
Previous Message Oleg Bartunov 2005-01-25 19:03:21 Re: datediff is there something like it?