Is it possible to list all foreign keys in a database?

后端 未结 4 917
一个人的身影
一个人的身影 2021-01-30 12:41

How do I list all FK\'s in a sqlserver database?

4条回答
  •  失恋的感觉
    2021-01-30 13:03

    Here is a more informative way of presenting it

    SELECT DISTINCT PARENT_TABLE = 
                RIGHT(Replace(TC.constraint_name, 'FK_', ''), 
                Len(Replace(TC.constraint_name, 'FK_', '')) - Charindex('_', Replace(TC.constraint_name, 'FK_', ''))), 
                CHILD_TABLE = TC.table_name, 
                CU.column_name, 
                TC.constraint_name, 
                TC.constraint_type 
    FROM information_schema.table_constraints TC 
    INNER JOIN information_schema.constraint_column_usage CU 
                ON TC.constraint_name = CU.constraint_name 
    WHERE  TC.constraint_type LIKE '%foreign' 
                OR TC.constraint_type LIKE '%foreign%' 
                OR TC.constraint_type LIKE 'foreign%' 
    

提交回复
热议问题