home
training courses
why choose us?
solutions
support
company
LearnItFirst User Forum
Welcome Guest
Search
|
New Posts
|
Members
|
Log In
|
Register
SQL Server Forum - LearnItFirst.com
»
SQL Server Scripts, Code Samples and SSMS Custom Reports
»
SQL Server 2005/2008
»
SQL Server Script to Find Duplicate Foreign Keys
SQL Server Script to Find Duplicate Foreign Keys
Options
Previous Topic
·
Next Topic
Scott Whigham
Posted:
Monday, June 04, 2007 9:06:42 AM
Rank: Super Mod
Joined: 3/20/2006
Posts: 476
Points: 1,053
Where do you live?: 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
)
Back to top
Users browsing this topic
Guest
Forum Jump
SQL Server Database Administration
- General SQL Database Question & Answer
- Backup, Recovery and Disaster Recovery
- SQL Server Security
- Integration Services (SSIS) and DTS
Transact-SQL Programming
- DML (SELECT, INSERT, UPDATE, DELETE) Questions
- Stored Procedures, Triggers, & Functions
SQL Server Scripts, Code Samples and SSMS Custom Reports
- All SQL Server Versions
- SQL Server 2005/2008
- SQL Server Management Studio Custom Reports
Customer Service
- Video Requests
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.
SQL Server 2005 DBA Training Videos
SQL Server 2008 DBA Training Videos
Email this topic
RSS Feed
Watch this topic
Print this topic
Normal
Threaded