有没有一个查询会让我指向特定表列的外键?例如,说我有这三个表:
__________ |Table A | ---------- |Id | ---------- ___________ |Table B | ----------- |Id | |TableAId | (Foreign Key to TableA.Id) ----------- ___________ |Table C | ----------- |Id | |TableAId | (Foreign Key to TableA.Id) -----------
我需要一个“Select * Foreign Keys directed to TableA.Id”的返回“Table C:TableAId”,“Table B:TableAId”的查询。我正在浏览一些INFORMATION_SCHEMA系统视图,似乎我可以轻松地看到外键属于表A或表B,但我找不到它所在的位置“表C有一个外键表A“具体。我可以弄清楚查询的细节,我找不到我正在寻找的视图(或者我正在掩饰他们)。任何帮助将不胜感激。
解决方法
@L_502_0@
SELECT f.name AS ForeignKey,OBJECT_NAME(f.parent_object_id) AS TableName,COL_NAME(fc.parent_object_id,fc.parent_column_id) AS ColumnName,OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,COL_NAME(fc.referenced_object_id,fc.referenced_column_id) AS ReferenceColumnName FROM sys.foreign_keys AS f INNER JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id