sql 查询所有外键SQL Server

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 查询所有外键SQL Server相关的知识,希望对你有一定的参考价值。

SELECT  obj.name AS FK_NAME,
    sch.name AS [schema_name],
    tab1.name AS [table],
    col1.name AS [column],
    tab2.name AS [referenced_table],
    col2.name AS [referenced_column]
FROM sys.foreign_key_columns fkc
INNER JOIN sys.objects obj
    ON obj.object_id = fkc.constraint_object_id
INNER JOIN sys.tables tab1
    ON tab1.object_id = fkc.parent_object_id
INNER JOIN sys.schemas sch
    ON tab1.schema_id = sch.schema_id
INNER JOIN sys.columns col1
    ON col1.column_id = parent_column_id AND col1.object_id = tab1.object_id
INNER JOIN sys.tables tab2
    ON tab2.object_id = fkc.referenced_object_id
INNER JOIN sys.columns col2
    ON col2.column_id = referenced_column_id AND col2.object_id = tab2.object_id

以上是关于sql 查询所有外键SQL Server的主要内容,如果未能解决你的问题,请参考以下文章

Sql server - 动态更改所有外键约束Update_Rule

如何在 SQL Server 中查找外键依赖项?

如何在 SQL Server 中列出所有带有“WITH NOCHECK”的外键

Sql Server有主外键关系时添加删除数据

sql 关联查询外键为空的情况

sql查询未被外键关联的数据