SQLServer - 如何在我的表上查找依赖表?

时间:2011-10-04 12:14:41

标签: sql database sql-server-2008 relational-database

使用SQLServer

我有一张表user

  • id

  • name

  • email

还有一些其他表(大约200多个表),其中一些表使用user.id作为cascade delete上的外键。

所以,我想找出 - 哪些表使用这个外键(user.id)?

我正在使用SQL Server Management Studio访问我的sql-server。

8 个答案:

答案 0 :(得分:14)

获取 ONLY TABLE 引用的方法(即使用给定表作为外键的表和给定表使用相同方式的表)可以使用此代码段:

declare @tableName varchar(64);
set @tableName = 'TABLE';

select
SO_P.name as [parent table]
,SC_P.name as [parent column]
,'is a foreign key of' as [direction]
,SO_R.name as [referenced table]
,SC_R.name as [referenced column]
,*
from sys.foreign_key_columns FKC
inner join sys.objects SO_P on SO_P.object_id = FKC.parent_object_id
inner join sys.columns SC_P on (SC_P.object_id = FKC.parent_object_id) AND (SC_P.column_id = FKC.parent_column_id)
inner join sys.objects SO_R on SO_R.object_id = FKC.referenced_object_id
inner join sys.columns SC_R on (SC_R.object_id = FKC.referenced_object_id) AND (SC_R.column_id = FKC.referenced_column_id)
where
    ((SO_P.name = @tableName) AND (SO_P.type = 'U'))
    OR
    ((SO_R.name = @tableName) AND (SO_R.type = 'U'))

答案 1 :(得分:13)

在SQL Server管理工作室中,您可以右键单击对象资源管理器中的表,然后选择“查看依赖关系”。这将打开一个新窗口,您可以在其中查看依赖于您的表以及您的表所依赖的所有其他对象(不仅仅是表)。

答案 2 :(得分:2)

以下是我根据上述答案部署的存储过程。

-- =============================================
-- Author:      R. Mycroft
-- Create date: 2012-08-08
-- Description: Lists foreign keys to & from a named table.  
-- (Have yet to find this one via Google!)
-- =============================================
alter procedure usp_ListTableForeignKeys 
    @tableName varchar(300) = ''
as
begin
set nocount on;

select 
    object_name(parent_object_id) as childObjectName
    , object_name(referenced_object_id) as parentObjectName
    , name, type_desc, create_date
from sys.foreign_keys
where object_name(parent_object_id) = @tableName
or object_name(referenced_object_id) = @tableName
end

答案 3 :(得分:1)

如果您已将这些定义为外键,那么只需检查表格设计并查看“关系”对话框,该对话框将显示为表格定义的所有内容。

或者,您可以使用“查看依赖关系”。

答案 4 :(得分:1)

试试这个

select 
    OBJECT_NAME(parent_object_id) as parent_object_name,
    *
from sys.foreign_keys
where name = 'YourFKName'

答案 5 :(得分:1)

使用 SSMS 图形用户界面:

View Table Dependencies

在 SQL 服务器管理工​​作室 (SSMS) 中,您可以右键单击您的表并选择“查看依赖项”。这将打开一个新窗口,您可以在其中看到所有依赖于您的表以及您的表所依赖的对象。

List of Dependencies

此外,如果您想在所有依赖于您的表的对象中使用 TSQL 来实现

方法一:使用 sp_depends 存储过程,虽然 sql server 团队将在未来版本中删除此功能,但获取指定对象的所有依赖项仍然很有用,包括表、视图、存储过程、约束等., sql server 团队推荐使用 sys.dm_sql_referencing_entities 和 sys.dm_sql_referenced_entities 。

-- Query to find Table Dependencies in SQL Server: 
EXEC sp_depends @objname = N'dbo.aspnet_users' ;

方法 2:

-- Query to find Table Dependencies in SQL Server: 
    SELECT referencing_id, 
       referencing_schema_name, 
       referencing_entity_name 
FROM sys.dm_sql_referencing_entities('dbo.aspnet_users', 'OBJECT');

方法三:在函数、过程和视图中查找表依赖

SELECT *
    FROM sys.sql_expression_dependencies A, sys.objects B
    WHERE referenced_id = OBJECT_ID(N'dbo.aspnet_users') AND 
        A.referencing_id = B.object_id

方法 4:

-- Value 131527 shows objects that are dependent on the specified object
EXEC sp_MSdependencies N'dbo.aspnet_users', null, 1315327

如果您想获取您的表所依赖的所有对象。

-- Value 1053183 shows objects that the specified object is dependent on
EXEC sp_MSdependencies N'dbo.aspnet_users', null, 1053183 

答案 6 :(得分:0)

获取外键的另一种选择。

-- CTE to fetch all primary key information.
WITH PrimaryKeys AS (
    SELECT 
        s.name as [Schema], 
        t.name as [Table], 
        c.name as [Column], 
        ic.index_column_id AS [ColumnNumber]
    FROM sys.index_columns ic
    JOIN sys.columns c ON ic.object_id = c.object_id and ic.column_id = c.column_id
    JOIN sys.indexes i ON ic.object_id = i.object_id and ic.index_id = i.index_id
    JOIN sys.tables t ON i.object_id = t.object_id
    JOIN sys.schemas s ON t.schema_id = s.schema_id
    WHERE i.is_primary_key = 1
),
-- CTE to fetch table information.
TableInfo AS (
    SELECT
        tab.name AS [Table],
        col.name AS [Column],
        sch.name AS [Schema],
        tab.object_id AS TableId,
        col.column_id AS ColumnId
    FROM sys.tables tab
    JOIN sys.schemas sch ON tab.schema_id = sch.schema_id
    JOIN sys.columns col ON col.object_id = tab.object_id
)

-- Primary query selecting foreign keys and primary/dependent information.
SELECT
    obj.name AS FK_NAME,
    p.[Schema] AS [PrimarySchema],
    p.[Table] AS [PrimaryTable],
    p.[Column] AS [PrimaryColumn],
    d.[Schema] AS [DependentSchema],
    d.[Table] AS [DependentTable],
    d.[Column] AS [DependentColumn],
    prim.ColumnNumber AS IsDependentPrimaryColumn -- has value if is part of dependent table's primary key
FROM  sys.foreign_key_columns fkc
JOIN sys.objects obj ON obj.object_id = fkc.constraint_object_id
JOIN TableInfo d ON d.TableId = fkc.parent_object_id AND d.ColumnId = fkc.parent_column_id
JOIN TableInfo p ON p.TableId = fkc.referenced_object_id AND p.ColumnId = fkc.referenced_column_id

-- Join in primary key information to determine if the dependent key is also
-- part of the dependent table's primary key.
LEFT JOIN PrimaryKeys prim ON prim.[Column] = d.[Column] AND prim.[Table] = d.[Table]

ORDER BY [PrimarySchema], [PrimaryTable], [DependentSchema], [DependentTable]

这将产生所有外键及其主要/相关信息。如果依赖列是从属表中主键的一部分,它还包括一个额外的列 - 有时需要注意这一点。

要仅获取Users表格,只需在最终WHERE之前添加ORDER BY条款

WHERE PrimaryTable = 'Users'

答案 7 :(得分:0)

获取具有架构名称的所有表的选项

    select
SO_P.name as [parent table]
,SS_P.name as [parent table schema]
,SC_P.name as [parent column]
,'is a foreign key of' as [direction]
,SO_R.name as [referenced table]
,SS_R.name as [referenced table schema]
,SC_R.name as [referenced column]
,*
from sys.foreign_key_columns FKC
inner join sys.objects SO_P on SO_P.object_id = FKC.parent_object_id
inner join sys.schemas SS_P on SS_P.schema_id = SO_P.schema_id
inner join sys.columns SC_P on (SC_P.object_id = FKC.parent_object_id) AND (SC_P.column_id = FKC.parent_column_id)
inner join sys.objects SO_R on SO_R.object_id = FKC.referenced_object_id
inner join sys.schemas SS_R on SS_R.schema_id = SO_P.schema_id
inner join sys.columns SC_R on (SC_R.object_id = FKC.referenced_object_id) AND (SC_R.column_id = FKC.referenced_column_id)

where SO_P.type = 'U' OR SO_R.type = 'U'