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

后端 未结 26 2633
梦毁少年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 08:03

    I have been using this on 2008 and up. It's similar to some other solutions listed but, the field names are proper cased to handle case specific (LatBin) collations. Additionally, you can feed it a single table name and retrieve just the info for that table.

    -->>SPECIFY THE DESIRED DB
    USE ???
    GO
    
    /*********************************************************************************************
    
        LIST OUT ALL PRIMARY AND FOREIGN KEY CONSTRAINTS IN A DB OR FOR A SPECIFIED TABLE
    
    *********************************************************************************************/
    DECLARE @tblName VARCHAR(255) 
    
    /*******************/
    
        SET @tblName = NULL-->NULL will return all PK/FK constraints for every table in the database
    
    /*******************/
    
    SELECT PKTABLE_QUALIFIER = CONVERT(SYSNAME,DB_NAME()), 
           PKTABLE_OWNER = CONVERT(SYSNAME,SCHEMA_NAME(O1.schema_id)), 
           PKTABLE_NAME = CONVERT(SYSNAME,O1.name), 
           PKCOLUMN_NAME = CONVERT(SYSNAME,C1.name), 
           FKTABLE_QUALIFIER = CONVERT(SYSNAME,DB_NAME()), 
           FKTABLE_OWNER = CONVERT(SYSNAME,SCHEMA_NAME(O2.schema_id)), 
           FKTABLE_NAME = CONVERT(SYSNAME,O2.name), 
           FKCOLUMN_NAME = CONVERT(SYSNAME,C2.name), 
           -- Force the column to be non-nullable (see SQL BU 325751) 
           KEY_SEQ             = isnull(convert(smallint,K.constraint_column_id),0), 
           UPDATE_RULE = CONVERT(SMALLINT,CASE OBJECTPROPERTY(F.object_id,'CnstIsUpdateCascade')  
                                            WHEN 1 THEN 0 
                                            ELSE 1 
                                          END), 
           DELETE_RULE = CONVERT(SMALLINT,CASE OBJECTPROPERTY(F.object_id,'CnstIsDeleteCascade')  
                                            WHEN 1 THEN 0 
                                            ELSE 1 
                                          END), 
           FK_NAME = CONVERT(SYSNAME,OBJECT_NAME(F.object_id)), 
           PK_NAME = CONVERT(SYSNAME,I.name), 
           DEFERRABILITY = CONVERT(SMALLINT,7)   -- SQL_NOT_DEFERRABLE 
    FROM   sys.all_objects O1, 
           sys.all_objects O2, 
           sys.all_columns C1, 
           sys.all_columns C2, 
           sys.foreign_keys F 
           INNER JOIN sys.foreign_key_columns K 
             ON (K.constraint_object_id = F.object_id) 
           INNER JOIN sys.indexes I 
             ON (F.referenced_object_id = I.object_id 
                 AND F.key_index_id = I.index_id) 
    WHERE  O1.object_id = F.referenced_object_id 
           AND O2.object_id = F.parent_object_id 
           AND C1.object_id = F.referenced_object_id 
           AND C2.object_id = F.parent_object_id 
           AND C1.column_id = K.referenced_column_id
           AND C2.column_id = K.parent_column_id
           AND (   O1.name = @tblName 
                OR O2.name = @tblName
                OR @tblName IS null)
    ORDER BY PKTABLE_NAME,FKTABLE_NAME
    

提交回复
热议问题