0

我想在一个表中找到所有外键,并为每个外键找到它指向的主键表和列。我需要使用 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')";

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

谢谢 - 戴夫

4

3 回答 3

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 数据库对此进行了测试,当在我的用户数据库的上下文中运行时,它返回了我定义的所有带有外键的表,以及相关的表和列。

当然,您也可以进一步过滤。

于 2012-06-30T02:49:53.543 回答
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
于 2014-04-03T21:55:00.833 回答
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;

来源:

于 2013-01-11T16:43:31.813 回答