如何使用INFORMATION_SCHEMA查找数据库中所有外键的主键

时间:2012-06-29 21:27:31

标签: sql sql-server information-schema

我想在表中找到所有外键,并且对于每个外键找到主键表&它指向的专栏。我需要使用INFORMATION_SCHEMA执行此操作,因为我们希望对所有数据库供应商(或至少所有正确实现INFORMATION_SCHEMA)使用此功能。

我想出的最好的是:

"SELECT k.COLUMN_NAME, ccu.TABLE_NAME AS 'references_table', ccu.COLUMN_NAME AS 'references_field' " +
"FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS k LEFT OUTER JOIN " +
"INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS c ON k.TABLE_NAME = c.TABLE_NAME AND k.TABLE_SCHEMA = c.TABLE_SCHEMA AND " +
"k.TABLE_CATALOG = c.TABLE_CATALOG AND k.CONSTRAINT_CATALOG = c.CONSTRAINT_CATALOG AND " +
"k.CONSTRAINT_NAME = c.CONSTRAINT_NAME LEFT OUTER JOIN " +
"INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS rc ON rc.CONSTRAINT_SCHEMA = c.CONSTRAINT_SCHEMA AND " +
"rc.CONSTRAINT_CATALOG = c.CONSTRAINT_CATALOG AND rc.CONSTRAINT_NAME = c.CONSTRAINT_NAME LEFT OUTER JOIN " +
"INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS ccu ON rc.UNIQUE_CONSTRAINT_SCHEMA = ccu.CONSTRAINT_SCHEMA AND " +
"rc.UNIQUE_CONSTRAINT_CATALOG = ccu.CONSTRAINT_CATALOG AND rc.UNIQUE_CONSTRAINT_NAME = ccu.CONSTRAINT_NAME " +
"WHERE(k.CONSTRAINT_CATALOG = DB_NAME()) AND (k.TABLE_NAME = '" + table.Name + "') AND (c.CONSTRAINT_TYPE = 'FOREIGN KEY')";

这让我觉得过于复杂。有更好的选择吗?

谢谢 - 戴夫

3 个答案:

答案 0 :(得分:2)

SELECT  ccu1.TABLE_NAME as fkTable, ccu1.COLUMN_NAME as fkColumn, 
        ccu2.TABLE_NAME as referencedTable, ccu2.COLUMN_NAME as referencedColumn
FROM    INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu1,
        INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu2,
        INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
WHERE   rc.CONSTRAINT_NAME = ccu1.CONSTRAINT_NAME
AND     rc.UNIQUE_CONSTRAINT_NAME = ccu2.CONSTRAINT_NAME

我针对SQL Server 2008数据库对此进行了测试,并且在我的用户数据库的上下文中运行时,它返回了所有带有外键的已定义表以及相关的表和列。

当然,你也可以进一步过滤掉它。

答案 1 :(得分:2)

最初接受的答案未正确考虑使用复合主键的数据库。

请改为尝试:

select  fku.CONSTRAINT_NAME, fku.ORDINAL_POSITION
        , fku.TABLE_SCHEMA as FK_TABLE_SCHEMA
        , fku.TABLE_NAME as FK_TABLE_NAME
        , fku.COLUMN_NAME as FK_COLUMN_NAME
        , pku.TABLE_SCHEMA as PK_TABLE_SCHEMA
        , pku.TABLE_NAME as PK_TABLE_NAME
        , pku.COLUMN_NAME as PK_COLUMN_NAME

from    INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
        INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE pku
            ON rc.UNIQUE_CONSTRAINT_NAME = pku.CONSTRAINT_NAME
        INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE fku
            ON rc.CONSTRAINT_NAME = fku.CONSTRAINT_NAME AND pku.ORDINAL_POSITION = fku.ORDINAL_POSITION
order by fku.TABLE_SCHEMA, fku.TABLE_NAME, fku.ORDINAL_POSITION

答案 2 :(得分:0)

如果引用的表(外键正在查看的表)具有唯一键,则Jim的答案无法正确返回所有外键,因为它使用UNIQUE_CONSTRAINT_NAME列。我建议使用:

SELECT 
    FK = fk.name, 
    FKTable = QUOTENAME(OBJECT_SCHEMA_NAME(fkcol.[object_id])) 
        + '.' + QUOTENAME(OBJECT_NAME(fkcol.[object_id])),
    FKCol = fkcol.name,
    ' references => ',
    PKTable = QUOTENAME(OBJECT_SCHEMA_NAME(pkcol.[object_id])) 
        + '.' + QUOTENAME(OBJECT_NAME(pkcol.[object_id])),
    PKCol = pkcol.name
FROM sys.foreign_keys AS fk
INNER JOIN sys.foreign_key_columns AS fkc
ON fk.[object_id] = fkc.constraint_object_id
INNER JOIN sys.columns AS fkcol
ON fkc.parent_object_id = fkcol.[object_id]
AND fkc.parent_column_id = fkcol.column_id
INNER JOIN sys.columns AS pkcol
ON fkc.referenced_object_id = pkcol.[object_id]
AND fkc.referenced_column_id = pkcol.column_id
ORDER BY fkc.constraint_column_id;

来源: