在FK中查找目标列

时间:2013-05-10 10:52:57

标签: sql-server

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

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

4 个答案:

答案 0 :(得分: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-in-database/

答案 1 :(得分: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

答案 2 :(得分: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';

e.g。

<强>设置

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 Fiddle Demo

答案 3 :(得分: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'))

来源:here