Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8165007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:26:25+00:00 2026-06-06T19:26:25+00:00

I want to find all foreign keys in a table, and for each foreign

  • 0

I want to find all foreign keys in a table, and for each foreign key find the primary key table & column it points to. I need to do this using INFORMATION_SCHEMA because we want to use this against all DB vendors (or at least all that properly implement INFORMATION_SCHEMA).

The best I have come up with is:

"SELECT k.COLUMN_NAME, ccu.TABLE_NAME AS 'references_table', ccu.COLUMN_NAME AS 'references_field' " +
"FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS k LEFT OUTER JOIN " +
"INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS c ON k.TABLE_NAME = c.TABLE_NAME AND k.TABLE_SCHEMA = c.TABLE_SCHEMA AND " +
"k.TABLE_CATALOG = c.TABLE_CATALOG AND k.CONSTRAINT_CATALOG = c.CONSTRAINT_CATALOG AND " +
"k.CONSTRAINT_NAME = c.CONSTRAINT_NAME LEFT OUTER JOIN " +
"INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS rc ON rc.CONSTRAINT_SCHEMA = c.CONSTRAINT_SCHEMA AND " +
"rc.CONSTRAINT_CATALOG = c.CONSTRAINT_CATALOG AND rc.CONSTRAINT_NAME = c.CONSTRAINT_NAME LEFT OUTER JOIN " +
"INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS ccu ON rc.UNIQUE_CONSTRAINT_SCHEMA = ccu.CONSTRAINT_SCHEMA AND " +
"rc.UNIQUE_CONSTRAINT_CATALOG = ccu.CONSTRAINT_CATALOG AND rc.UNIQUE_CONSTRAINT_NAME = ccu.CONSTRAINT_NAME " +
"WHERE(k.CONSTRAINT_CATALOG = DB_NAME()) AND (k.TABLE_NAME = '" + table.Name + "') AND (c.CONSTRAINT_TYPE = 'FOREIGN KEY')";

This strikes me as over-complicated. Is there a better select for this?

thanks – dave

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:26:27+00:00Added an answer on June 6, 2026 at 7:26 pm
    SELECT  ccu1.TABLE_NAME as fkTable, ccu1.COLUMN_NAME as fkColumn, 
            ccu2.TABLE_NAME as referencedTable, ccu2.COLUMN_NAME as referencedColumn
    FROM    INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu1,
            INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu2,
            INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
    WHERE   rc.CONSTRAINT_NAME = ccu1.CONSTRAINT_NAME
    AND     rc.UNIQUE_CONSTRAINT_NAME = ccu2.CONSTRAINT_NAME
    

    I tested this against a SQL Server 2008 database and when run under context of my user database, it returned all my defined tables with foreign keys, and the related table and column.

    You can also filter this down further of course.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I´ve got a question similar to this one: How to find all foreign keys?
I want to find all the foreign keys of an entity in my entity
I want to find all the demo words using PHP and regEx. $input ='demo';
Given different data sources I would like to find out all foreign keys inside
I want find all Saturdays and Sundays in A given month. How can I
I want to find all items in one collection that do not match another
I want to find all stylesheet definitions in a XHTML file with lxml.etree.findall .
I want to find all img tags in a string of text and put
I want to find all files/dirs that are not equal to .git* , so
I want to find all posts that are tagged with tags that are passed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.