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

后端 未结 26 3045
梦毁少年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:06

    I am using this script to find all details related to foreign key. I am using INFORMATION.SCHEMA. Below is a SQL Script:

    SELECT 
        ccu.table_name AS SourceTable
        ,ccu.constraint_name AS SourceConstraint
        ,ccu.column_name AS SourceColumn
        ,kcu.table_name AS TargetTable
        ,kcu.column_name AS TargetColumn
    FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu
        INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
            ON ccu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME 
        INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu 
            ON kcu.CONSTRAINT_NAME = rc.UNIQUE_CONSTRAINT_NAME  
    ORDER BY ccu.table_name
    

提交回复
热议问题