LearnItFirst User Forum

New SQL Server 2008 DBA Course
Welcome Guest Search | New Posts | Members | Log In | Register

Return Number of Foreign Keys Per Table Options
Scott Whigham
Posted: Tuesday, September 05, 2006 12:17:53 PM


Rank: Super Mod

Joined: 3/20/2006
Posts: 460
Points: 1,002
Where do you live?: Dallas, TX
Code:
/*
    Author: Scott Whigham from http://www.LearnSqlServer.com/

    Description: This script tells you the number of foreign keys per table for SQL Server
   
    Versions: SQL Server 2005
   
    Creation Date: September 5, 2006

    For more scripts like this one, visit http://forums.learnsqlserver.com/codesamples.aspx
*/

SELECT SCHEMA_NAME(t.schema_id) + '.' + t.Name AS TableName
    , COUNT(tc.TABLE_NAME) AS NumberOfFKs
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS tc RIGHT JOIN sys.tables t
    ON TABLE_NAME = t.name
    AND tc.CONSTRAINT_TYPE = 'FOREIGN KEY'
WHERE t.is_ms_shipped = 0
GROUP BY SCHEMA_NAME(t.schema_id) + '.' + t.Name
ORDER BY NumberOfFKs
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.