0

如果我有一对 SQL 表,其中一个表在一个列上有一个 pk,在另一个列上有一个唯一索引(可能是 guid 和 id 或其他一些唯一键)。

另一个表对唯一索引有一个 fk,而不是 pk。有没有办法可以查询信息架构以找到 fk 所针对的列?

4

4 回答 4

2

如果我猜对了,您想使用元数据库:

SELECT
K_Table = FK.TABLE_NAME,
FK_Column = CU.COLUMN_NAME,
PK_Table = PK.TABLE_NAME,
PK_Column = PT.COLUMN_NAME,
Constraint_Name = C.CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
INNER JOIN (
SELECT i1.TABLE_NAME, i2.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY'
) PT ON PT.TABLE_NAME = PK.TABLE_NAME

资料来源:http: //blog.sqlauthority.com/2006/11/01/sql-server-query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-数据库内/

于 2013-05-10T11:02:28.153 回答
2

试试这个——

SELECT
      column_name = c.name
    , table_name = OBJECT_NAME(k.parent_object_id) 
    , referenced_column_name = rc.name
    , referenced_table_name = OBJECT_NAME(k.referenced_object_id) 
FROM sys.foreign_key_columns k
JOIN sys.columns c ON c.[object_id] = k.parent_object_id AND c.column_id = k.parent_column_id
JOIN sys.columns rc ON rc.[object_id] = k.referenced_object_id AND rc.column_id = k.referenced_column_id
于 2013-05-10T11:02:46.843 回答
2
select object_name(fk.REFERENCED_OBJECT_ID) object_name,
       i.name index_name,
       ic.index_column_id sequence,
       c.name column_name
  from sys.foreign_keys fk
  join sys.indexes i on i.object_id = fk.REFERENCED_OBJECT_ID
                    and i.index_id = fk.KEY_INDEX_ID
  join sys.index_columns ic on ic.object_id = fk.REFERENCED_OBJECT_ID
                           and ic.index_id = fk.KEY_INDEX_ID
  join sys.columns c on c.object_id = fk.REFERENCED_OBJECT_ID
                    and c.column_id = ic.column_id
 where fk.name = 'fk_tbl2_to_tbl1';

例如

设置

create table tbl1 (
  a int constraint pk_tbl1 primary key,
  b int constraint uq_tbl1 unique,
  c int);
create table tbl2 (
  b int constraint fk_tbl2_to_tbl1 references tbl1(b),
  d int);

结果

| OBJECT_NAME | INDEX_NAME | SEQUENCE | COLUMN_NAME |
-----------------------------------------------------
|        tbl1 |    uq_tbl1 |        1 |           b |

SQL 小提琴演示

于 2013-05-10T11:13:39.577 回答
0

--查找主键和外键的列 - SQL SERVER 2008

SELECT
CU.TABLE_NAME, CU.COLUMN_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU
WHERE CU.CONSTRAINT_NAME IN (SELECT CONSTRAINT_NAME FROM         INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'PRIMARY KEY') AND 
COLUMN_NAME IN (SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_NAME = CU.TABLE_NAME AND 
COLUMN_NAME = CU.COLUMN_NAME AND 
CONSTRAINT_NAME IN (SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'FOREIGN KEY'))

来源:这里

于 2013-05-10T11:09:28.480 回答