Unix Technical Forum

Re: FK relationships

This is a discussion on Re: FK relationships within the pgsql Novice forums, part of the PostgreSQL category; --> On Wed, Jan 12, 2005 at 10:56:00AM -0800, sarlav kumar wrote: > Is there an easy way to find ...


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

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-17-2008, 08:20 PM
Michael Fuhr
 
Posts: n/a
Default Re: FK relationships

On Wed, Jan 12, 2005 at 10:56:00AM -0800, sarlav kumar wrote:

> Is there an easy way to find out the list of tables that have a
> FK relation on a particular table?


You could query the system catalogs, either directly or via the
Information Schema (the latter is available in 7.4 and later).
See the "System Catalogs" and "The Information Schema" chapters
in the documentation.

> Say, if I have a 'customer' table, I would like to get the tables
> that depend on customer and not the tables on which customer depends.


I think this'll work, although I did only trivial testing with it:

SELECT conrelid::regclass, conname
FROM pg_constraint
WHERE confrelid = 'customer'::regclass AND contype = 'f';

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

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-17-2008, 08:20 PM
sarlav kumar
 
Posts: n/a
Default Re: FK relationships

Thank you very much. When I use the query, this is the result I get:


SELECT conrelid::regclass, conname FROM pg_constraint WHERE confrelid = 'customer'::regclass AND contype = 'f';
conrelid | conname
--------------------------+-----------------
customer_identity | $1
customer_facts | $1
customerdata | customer_uid_fk

And when I looked up the \d for the tables, I get this:

$1 FOREIGN KEY (uid) REFERENCES customer(id) ON UPDATE NO ACTION ON DELETE NO ACTION

Since $1 is not very clear about the FK relationship that exists between the tables, I would like to rename these, or be able to get the names of the colums that are FK on customer table.

Is it possible to rename the foreign key constraints, by changing them directly in the pg_constraint table?

Thanks again.
Saranya

Michael Fuhr <mike@fuhr.org> wrote:
On Wed, Jan 12, 2005 at 10:56:00AM -0800, sarlav kumar wrote:

> Is there an easy way to find out the list of tables that have a
> FK relation on a particular table?


You could query the system catalogs, either directly or via the
Information Schema (the latter is available in 7.4 and later).
See the "System Catalogs" and "The Information Schema" chapters
in the documentation.

> Say, if I have a 'customer' table, I would like to get the tables
> that depend on customer and not the tables on which customer depends.


I think this'll work, although I did only trivial testing with it:

SELECT conrelid::regclass, conname
FROM pg_constraint
WHERE confrelid = 'customer'::regclass AND contype = 'f';

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


---------------------------------
Do you Yahoo!?
Yahoo! Mail - now with 250MB free storage. Learn more.
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 01:24 AM.


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