Thursday, November 21, 2013

How to find Foreign Keys from the database

--      Find all Foreign key on the database with TableName and Referenced TableName
  
    SELECT Name AS ForeignKeyName, OBJECT_NAME(parent_object_id) AS BaseTableName,
            OBJECT_NAME(referenced_object_id) AS ReferecedTableName --, *
    FROM sys.foreign_keys order by BaseTableName

--     TableName And Number of Foreign keys on each table on the database.
    SELECT OBJECT_NAME(parent_object_id) AS TableName ,COUNT(*) AS foreingKey_Count
    FROM sys.foreign_keys
    GROUP BY OBJECT_NAME(parent_object_id)
    ORDER BY TableName

No comments:

Post a Comment