How can I list all foreign keys referencing a given table in SQL Server?

后端 未结 26 3046
梦毁少年i
梦毁少年i 2020-11-22 07:13

I need to remove a highly referenced table in a SQL Server database. How can I get a list of all the foreign key constraints I will need to remove in order to drop the tabl

26条回答
  •  余生分开走
    2020-11-22 07:51

    You can find through below query :

     SELECT OBJECT_NAME (FK.referenced_object_id) 'Referenced Table', 
          OBJECT_NAME(FK.parent_object_id) 'Referring Table', FK.name 'Foreign Key', 
          COL_NAME(FK.referenced_object_id, FKC.referenced_column_id) 'Referenced Column',
          COL_NAME(FK.parent_object_id,FKC.parent_column_id) 'Referring Column'
         FROM sys.foreign_keys AS FK
                 INNER JOIN sys.foreign_key_columns AS FKC 
                     ON FKC.constraint_object_id = FK.OBJECT_ID
         WHERE OBJECT_NAME (FK.referenced_object_id) = 'YourTableName'
         AND COL_NAME(FK.referenced_object_id, FKC.referenced_column_id) = 'YourColumnName'
         order by  OBJECT_NAME(FK.parent_object_id)
    

提交回复
热议问题