Naveen's Weblog

Bridge to future

List tables linked to a parent table

Posted by codingsense on December 23, 2008


Many times we require to know which child tables are related to the parent table. The following query gives us the tables are related to the parent table with foreign keys.


select TABLE_NAME from INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE WHERE CONSTRAINT_NAME IN(
SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS WHERE UNIQUE_CONSTRAINT_NAME IN (
select CONSTRAINT_NAME from INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE where table_name ='ParentTableName'))

Just replace the string ParentTableName with the table name for which you want to search the related child tables.

Happy Learning ;)

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.