dbTalk Databases Forums  

How do you find the foreign keys in a table?

comp.databases.mysql comp.databases.mysql


Discuss How do you find the foreign keys in a table? in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Loony2nz
 
Posts: n/a

Default How do you find the foreign keys in a table? - 05-13-2008 , 12:20 PM






I found a query on how to find the primary key of a table:

SELECT COLUMN_NAME
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = Database()
AND TABLE_NAME = "table_name"
AND COLUMN_KEY = "PRI"

Now..the question is, how do i find the columns that are foreign keys
in another table.

If this table has 4 columns that are foreign keys in other tables..how
do I find those via a query?

Thanks!
Chris

Reply With Quote
  #2  
Old   
Rik Wasmus
 
Posts: n/a

Default Re: How do you find the foreign keys in a table? - 05-13-2008 , 01:12 PM






On Tue, 13 May 2008 19:20:23 +0200, Loony2nz <Loony2nz (AT) gmail (DOT) com> wrote:

Quote:
I found a query on how to find the primary key of a table:

SELECT COLUMN_NAME
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = Database()
AND TABLE_NAME = "table_name"
AND COLUMN_KEY = "PRI"

Now..the question is, how do i find the columns that are foreign keys
in another table.

If this table has 4 columns that are foreign keys in other tables..how
do I find those via a query?
I have no idea wether this is always true, as I could not find the
documentation I wanted quickly enough, and this holds true enough for my
debugging purposes:

SELECT TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,REFERENCED_COL UMN_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE REFERENCED_TABLE_NAME = 'table_name'
AND REFERENCED_TABLE_SCHEMA = DATABASE();
--
Rik Wasmus
[SPAM] Now temporarily looking for some smaller PHP/MySQL projects/work to
fund a self developed bigger project, mail me at rik at rwasmus.nl. [/SPAM]


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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.