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 8282153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:16:26+00:00 2026-06-08T10:16:26+00:00

I am looking for a query to help with a cleanup of an old

  • 0

I am looking for a query to help with a cleanup of an old database that doesn’t have many relationships where it should. I don’t need it to be perfect, just to help guide me in beginning cleanup and starting to enforce data integrity.

I am assuming that all tables have the proper primary key and that a column in a not-yet-related table has the same name.

Theoretically speaking I could have three tables, one which has a composite key (I wouldn’t choose to design the db like this, but am limited in the cleanup of it and these types of composite/primary/foreign keys are common):

Case.CaseId (PK)

Workstep.WorkstepId (PK)

Workstep.CaseId (PK,FK)

WorkQueue.CaseId (isn’t related TO Case.CaseId but should be)

What I would like to be able to do is run a query and come up with results that give me something like table name, column name, and foreign key of the table that isn’t related but should be, e.g.:

TABLE NAME, COLUMN NAME, SHOULD BE RELATED TO PRIMARY KEY

WorkQueue, CaseId, Case.CaseId

See the SQL I am using below but it is returning any primary key, even ones that are both a primary key but also are part of a foreign key. Using my example again and the SQL below, instead of returning 1 row I get 2:

TABLE NAME, COLUMN NAME, SHOULD BE RELATED TO PRIMARY KEY

WorkQueue, CaseId, Workstep.CaseId (I don’t want this row since it is also related to the ‘real’ primary key, Case.CaseId)

WorkQueue, CaseId, Case.CaseId

    SELECT 
    SubqueryAllPotentialForeignKeys.TABLE_NAME
    ,SubqueryAllPotentialForeignKeys.COLUMN_NAME
    ,(PrimaryKeys.TABLE_NAME + '.' + PrimaryKeys.COLUMN_NAME) as 'Possible Primary Key'

--all potential foreign keys (column name matches another column name but there is no reference from this column anywhere else)
FROM
    (   
    SELECT
        INFORMATION_SCHEMA.COLUMNS.TABLE_NAME
        ,INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME
    FROM
        INFORMATION_SCHEMA.COLUMNS
    WHERE
        --only get columns that are in multiple tables
        INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME IN 
        (
            SELECT COLUMN_NAME FROM
                (SELECT COLUMN_NAME, COUNT(COLUMN_NAME) AS ColNameCount FROM INFORMATION_SCHEMA.COLUMNS GROUP BY COLUMN_NAME) AS SubQueryColumns
            WHERE ColNameCount > 1
        )

        --only get the table.column if not part of a foreign or primary key
        EXCEPT
        (
            SELECT TABLE_NAME, COLUMN_NAME  FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE 
        )

    ) AS SubqueryAllPotentialForeignKeys

LEFT JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS PrimaryKeys ON 
    SubqueryAllPotentialForeignKeys.COLUMN_NAME = PrimaryKeys.COLUMN_NAME

--when finding possible keys for our columns that don't have references, limit to primary keys
WHERE 
    PrimaryKeys.CONSTRAINT_NAME LIKE '%PK_%'

ORDER BY TABLE_NAME, COLUMN_NAME
  • 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-08T10:16:28+00:00Added an answer on June 8, 2026 at 10:16 am

    This may not be the most beautiful thing in the world but works pretty darn well:

        SELECT * FROM
    (
        SELECT 
            SubqueryAllPotentialForeignKeys.TABLE_NAME
            ,SubqueryAllPotentialForeignKeys.COLUMN_NAME
            ,(PrimaryKeys.TABLE_NAME + '.' + PrimaryKeys.COLUMN_NAME) as 'Possible Primary Key'
    
        --all potential foreign keys (column name matches another column name but there is no reference from this column anywhere else)
        FROM
            (   
            SELECT
                INFORMATION_SCHEMA.COLUMNS.TABLE_NAME
                ,INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME
            FROM
                INFORMATION_SCHEMA.COLUMNS
            WHERE
                --only get columns that are in multiple tables
                INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME IN 
                (
                    SELECT COLUMN_NAME FROM
                        (SELECT COLUMN_NAME, COUNT(COLUMN_NAME) AS ColNameCount FROM INFORMATION_SCHEMA.COLUMNS GROUP BY COLUMN_NAME) AS SubQueryColumns
                    WHERE ColNameCount > 1
                )
    
                --only get the table.column if not part of a foreign or primary key
                EXCEPT
                (
                    SELECT TABLE_NAME, COLUMN_NAME  FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE 
                ) 
    
            ) AS SubqueryAllPotentialForeignKeys
    
        LEFT JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS PrimaryKeys ON 
            SubqueryAllPotentialForeignKeys.COLUMN_NAME = PrimaryKeys.COLUMN_NAME
    
        --when finding possible keys for our columns that don't have references, limit to primary keys
        WHERE 
            PrimaryKeys.CONSTRAINT_NAME LIKE '%PK_%'
    ) AS Subquery
    
    --exclude all keys that are primary but also foreign
    WHERE [Possible Primary Key] NOT IN
        (
            SELECT (TABLE_NAME + '.' + COLUMN_NAME)
            FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
            WHERE CONSTRAINT_NAME LIKE 'FK_%'
        ) 
    
    ORDER BY TABLE_NAME, COLUMN_NAME
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi i'm looking for help with a query. I currently have a table that
I need some help creating a query for my mySQL database. I have recently
LINQ gurus, I am looking for help to write a query... I have a
Looking to have a database query set all the instance variables in a class:
I am looking for some help with a MYSQL query. I have two tables,
I am looking to write an SQL query directly on the Magento database that
I have a rather complex SQL query that I am looking for a little
I am looking for a way to query facebook pages that for example, have
Looking for some help with optimising the query below. Seems to be two bottlenecks
I'm looking for help with my query below. which is never returning anything for

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.