Solved

Query to list table's foreign key constraints

Posted on 2008-06-16
4
6,599 Views
Last Modified: 2012-05-05
Is there a query that I can run to list all tables that depend on a certain table

e.g.  all tables that depend on tblData

They currently have a foreign key defined and I would like a way to quickly list these tables.
0
Comment
Question by:mock5c
  • 2
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
Zippit earned 100 total points
Comment Utility
credit to here:
http://blog.sqlauthority.com/2006/11/01/sql-server-query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/
SELECT

       K_Table  = FK.TABLE_NAME,

       FK_Column = CU.COLUMN_NAME,

       PK_Table  = PK.TABLE_NAME,

       PK_Column = PT.COLUMN_NAME,

       Constraint_Name = C.CONSTRAINT_NAME

FROM       INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C

INNER JOIN  INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME

INNER JOIN      INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME

INNER JOIN      INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME

INNER JOIN  (

       SELECT      i1.TABLE_NAME, i2.COLUMN_NAME

       FROM        INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1

           INNER JOIN      INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME

               WHERE       i1.CONSTRAINT_TYPE = 'PRIMARY KEY'

       ) PT ON PT.TABLE_NAME = PK.TABLE_NAME

-- optional:

--ORDER BY

--       1,2,3,4

WHERE      PK.TABLE_NAME='tbl_MyTable' 

Open in new window

0
 
LVL 7

Expert Comment

by:Zippit
Comment Utility
oh..sorry...that was for MSSQL...I just noticed your question is in regards to Postgresql...my appologies
0
 

Author Comment

by:mock5c
Comment Utility
They query can be run under postgres.  However, many of my constraint names are simply $1, $2, etc.  I take it that it would be impossible to figure out using that query?
0
 

Author Closing Comment

by:mock5c
Comment Utility
The query only required a minor modification to work with Postgres.  The beginning section

K_Table  = FK.TABLE_NAME,
FK_Column = CU.COLUMN_NAME,
PK_Table  = PK.TABLE_NAME,
PK_Column = PT.COLUMN_NAME,
Constraint_Name = C.CONSTRAINT_NAME

had to be changed to:

FK.TABLE_NAME as K_Table,
CU.COLUMN_NAME as FK_Column,
PK.TABLE_NAME as PK_Table,
PT.COLUMN_NAME as PK_Column,
C.CONSTRAINT_NAME as Constraint_Name
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Best database to use for Maps is PostgreSQL. This is an open source database. Comes as a package with most Linux OS. For more info visit the following site: http://www.postgresql.org/ (http://www.postgresql.org/) This requires some add-o…
Many developers have database experience, but are new to PostgreSQL. It has some truly inspiring capabilities. I have several years' experience with Microsoft's SQL Server. When I began working with MySQL, I wanted a quick-reference to MySQL (htt…
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now