2016-09-16 42 views
-1

如何获取引用特定列A作为外键的其他表/模式中的所有列的列表?将列引用为外键的所有列

+1

右键单击主表并选择“查看依赖关系”,这将列出链接到主键的所有相关表。 – Tanner

+1

[如何列出引用SQL Server中给定表的所有外键?](http://stackoverflow.com/questions/483193/how-can-i-list-all-foreign-keys-referencing-一个赋予的表型-SQL服务器) –

回答

0

这样的事情?

SELECT 
    fk.Name, 
    'Referenced table' = refTbl.Name, 
    'Parent table' = parentTbl.Name, 
    'Parent column' = c.name 
FROM 
    -- FK constraint 
    sys.foreign_keys fk 
INNER JOIN 
    -- Referenced table (where the PK resides) 
    sys.tables refTbl ON fk.referenced_object_id = refTbl.object_id 
INNER JOIN 
    -- Parent table (which has the foreign key column) 
    sys.tables parentTbl ON fk.parent_object_id = parentTbl .object_id 
INNER JOIN 
    -- link to the columns involved in the FK contraint 
    sys.foreign_key_columns fkc ON fkc.constraint_object_id = fk.object_id 
INNER JOIN 
    -- column in the parent table that's part of the FK constraint 
    sys.columns c ON c.object_id = parentTbl.object_id AND c.column_id = fkc.parent_column_id 
WHERE 
    refTbl.name = 'YourTableNameHere' 
ORDER BY 
    fk.Name, parentTbl.name