用于输出外键信息的脚本

时间:2017-06-04 12:09:35

标签: sql-server sql-server-2008

我想知道有关我的外键的详细信息。

我试过这个

SELECT * FROM sys.foreign_keys;

但我需要的不仅仅是我现在可以得到的信息。 有没有办法通过使用这样的脚本来了解他们所引用的列?

1 个答案:

答案 0 :(得分:1)

要获取所有表及其列名的列表,请使用下一个代码: -

select 
    t.name as TableWithForeignKey, 
    c.name as ForeignKeyColumn 
from 
    sys.foreign_key_columns as fk
inner join 
    sys.tables as t on fk.parent_object_id = t.object_id
inner join 
    sys.columns as c on fk.parent_object_id = c.object_id and fk.parent_column_id = c.column_id
where 
    fk.referenced_object_id in (select object_id 
                               from sys.tables )
order by 
    TableWithForeignKey

参考: - How can I list all foreign keys referencing a given table in SQL Server?