Search

2015년 11월 20일 금요일

[Database] [MSSQL] 데이터베이스 내 모든 외래키(FK) 찾기

* 데이터베이스에서 사용하는 모든 FK를 찾는 쿼리는 다음과 같다.

SELECT
    f.name AS foreign_key_name,
    OBJECT_NAME(f.parent_object_id) AS table_name,
    COL_NAME(fc.parent_object_id, fc.parent_column_id) AS constraint_column_name,
    OBJECT_NAME (f.referenced_object_id) AS referenced_object,
    COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS referenced_column_name,
    is_disabled,
    delete_referential_action_desc,
    update_referential_action_desc
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc 
   ON f.object_id = fc.constraint_object_id 
JOIN (
    SELECT object_id
    FROM   sys.objects 
    WHERE  type_desc = 'USER_TABLE'
)tsp
ON f.parent_object_id = tsp.object_id


댓글 없음:

댓글 쓰기