代码之家  ›  专栏  ›  技术社区  ›  David

有没有一种快速的方法来确定哪些表将特定的主键作为外键引用?

  •  0
  • David  · 技术社区  · 14 年前

    我正在做一点重构,但是,我想检查一下,我想更改的当前主键当前是否在任何其他表中被引用为外键。我正在使用的模式非常大,因此扫描模式中的每个表是不可行的。

    3 回复  |  直到 14 年前
        1
  •  4
  •   Will A    14 年前

    EXEC sp_help 'yourtable' -结果集中的一个表包含引用该表的FK。

        2
  •  3
  •   marc_s HarisH Sharma    14 年前

    像这样的东西??

    SELECT
        fk.name,
        t1.name 'Child table',
        t2.name 'Parent table'
    FROM 
        sys.foreign_keys fk
    INNER JOIN 
        sys.tables t1 ON fk.parent_object_id = t1.object_id
    INNER JOIN 
        sys.tables t2 ON fk.referenced_object_id = t2.object_id 
    WHERE 
        t2.name = '(your table name here)'
    
        3
  •  0
  •   jl.    14 年前