Member of the LearnItFirst.com Video Training Network | LearnSqlServer.com | SQL SSIS Training | SQL Programming Tutorials |
LearnSqlServer.com Forums LearnSqlServer.com
Welcome Guest Search | New Posts | Members | Log In | Register

SQL Server Script to Find Duplicate Foreign Keys Options
Scott Whigham
Posted: Monday, June 04, 2007 9:06:42 AM


Rank: Super Mod

Joined: 3/20/2006
Posts: 345
Points: 748
Location: Dallas, TX
"raja" was asking for this last week and I thought I would post it here. It's a script that finds duplicate forieng keys in your SQL database
Code:
/*
    Author: Scott Whigham from http://www.LearnSqlServer.com/

    Description: This script returns any duplicate foreign keys

    Misc Notes: Special thanks to user "raja" for suggesting this!
   
    Versions: SQL Server 2005
   
    Creation Date: June 1, 2007

    For more scripts like this one, visit http://forums.learnsqlserver.com/codesamples.aspx
*/
-- Some sample tables that cause dupes:
--            NOTE: Don't run this in production; for illustrative purposes only
use tempdb
GO
IF OBJECTPROPERTY(OBJECT_ID('Parent'), 'IsTable') IS NULL
    CREATE TABLE Parent ( ParentId INT NOT NULL IDENTITY(1,1) PRIMARY KEY, ColA INT)
GO
IF OBJECTPROPERTY(OBJECT_ID('Child'), 'IsTable') IS NULL
    CREATE TABLE Child (ChildId INT NOT NULL IDENTITY(1,1) PRIMARY KEY, ParentId INT NOT NULL)
GO
IF OBJECTPROPERTY(OBJECT_ID('Child2'), 'IsTable') IS NULL
    CREATE TABLE Child2 (Child2Id INT NOT NULL IDENTITY(1,1) PRIMARY KEY, ParentId INT NOT NULL, AnotherParentId INT NOT NULL)
GO
-- Dupes:
IF OBJECTPROPERTY(OBJECT_ID('FK_Child_Parent1'), 'IsForeignKey') IS NULL
    ALTER TABLE Child ADD CONSTRAINT FK_Child_Parent1 FOREIGN KEY (ParentId) REFERENCES Parent(ParentId)
IF OBJECTPROPERTY(OBJECT_ID('FK_Child_Parent2'), 'IsForeignKey') IS NULL
    ALTER TABLE Child ADD CONSTRAINT FK_Child_Parent2 FOREIGN KEY (ParentId) REFERENCES Parent(ParentId)
GO
-- Not dupe:
IF OBJECTPROPERTY(OBJECT_ID('FK_Child2_Parent1'), 'IsForeignKey') IS NULL
    ALTER TABLE Child2 ADD CONSTRAINT FK_Child2_Parent1 FOREIGN KEY (ParentId) REFERENCES Parent(ParentId)
IF OBJECTPROPERTY(OBJECT_ID('FK_Child2_Parent2'), 'IsForeignKey') IS NULL
    ALTER TABLE Child2 ADD CONSTRAINT FK_Child2_Parent2 FOREIGN KEY (AnotherParentId) REFERENCES Parent(ParentId)
GO

-- This is the main query that returns the dupes:
WITH TablesWithFKs AS (
    SELECT OBJECT_ID ( '[' + tc.TABLE_SCHEMA + '].[' + tc.TABLE_NAME + ']') AS ObjectId
        , '[' + tc.TABLE_SCHEMA + '].[' + tc.TABLE_NAME + ']' AS FullyQualifedObjectName
        , tc.TABLE_SCHEMA, tc.TABLE_NAME, cu.COLUMN_NAME
        , tc.CONSTRAINT_NAME
    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc -- Returns child table info
    JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE cu
        ON tc.CONSTRAINT_NAME = cu.CONSTRAINT_NAME
    WHERE CONSTRAINT_TYPE = 'FOREIGN KEY'
)
SELECT * FROM TablesWithFKs t
WHERE EXISTS (
        SELECT * FROM TablesWithFKs sub
        WHERE sub.ObjectId=t.ObjectId
            AND sub.COLUMN_NAME = t.COLUMN_NAME
            AND sub.CONSTRAINT_NAME <> t.CONSTRAINT_NAME
)
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.
     
Don't Forget!
LearnItFirst.com
Don't Forget!
LearnWindows2003.com
 
Home | About Us | Support | Contact Us | Privacy | Site Map | Blogs Blogs Refer a Friend and Get a Free Subscription!
© Copyright 2004-2007 LearnItFirst.com LLC. All rights reserved. All trademarks remain the property of their respective owners.
This site is not affiliated in any way with the Microsoft Corporation.