Solved

Query to list table's foreign key constraints

Posted on 2008-06-16
4
6,603 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
ID: 21796762
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
ID: 21796804
oh..sorry...that was for MSSQL...I just noticed your question is in regards to Postgresql...my appologies
0
 

Author Comment

by:mock5c
ID: 21804251
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
ID: 31467732
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

749 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