Unix Technical Forum

Looking up table names by REFERENCES

This is a discussion on Looking up table names by REFERENCES within the pgsql Sql forums, part of the PostgreSQL category; --> Hello all. I'm trying to write a recursive procedure to allow me to handle some data backup routines and ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Sql

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008, 10:53 AM
Steve Castellotti
 
Posts: n/a
Default Looking up table names by REFERENCES


Hello all. I'm trying to write a recursive procedure to allow me to
handle some data backup routines and deal with conflicts when restoring
from within the program.

Essentially, what I'd like to be able to do is if a table called
"image" has a column called "file_id" which references a column called
"file_id" in another table called "file" I want to be able to determine
that pragmatically. If I wanted to backup all of the information about
images in the database, I would need to backup all of the information
about the file(s) each image corresponds to.

For instance, I can get a list of all (user) table names with:

SELECT relname AS table_name, oid
FROM pg_class
WHERE NOT relname ~ 'pg_.*'
AND NOT relname ~ 'pga_.*'
AND NOT relname ~ '.*_pkey'
AND NOT relname ~ '.*_id_key'
ORDER BY relname;

and I can get a list of column names and their types (for the "image"
table) with:

SELECT a.attname AS field, t.typname AS type
FROM pg_class c, pg_attribute a, pg_type t
WHERE c.relname = 'image' and a.attnum > 0
and a.attrelid = c.oid and a.atttypid = t.oid
ORDER BY a.attnum;


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


Any help would be most appreciated, especially if I could be cc'd
directly.


Cheers

Steve Castellotti

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-19-2008, 10:53 AM
Michael Fuhr
 
Posts: n/a
Default Re: Looking up table names by REFERENCES

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/

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:21 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com