SQLServer - 如何查找我的表上的依赖表?
使用 SQLServer
:
我有一个表 user
:
id
- < code>name
email
还有一些其他表(大约 200更多表),其中一些使用 user.id
作为外键在级联删除
上。
所以,我想找出 - 哪些表使用这个外键(user.id
)?
我正在使用 SQL Server Management Studio
访问我的 sql-server。
Using SQLServer
:
I have a table user
:
id
name
email
There are some other tables (about 200 more tables), some of which use user.id
as foreign key on cascade delete
.
So, I want to find out - Which tables use this foreign key (user.id
) ?
I am accessing my sql-server with SQL Server Management Studio
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
获取ONLY TABLE引用(即使用给定表作为外键的表和给定表使用相同方式的表)的方法可以使用以下代码片段:
The way to get ONLY TABLE references (i.e. tables that uses given table as a foreign key and tables that given table uses the same way) you can use this code snippet:
在 SQL Server Management Studio 中,您可以右键单击对象资源管理器中的表,然后选择“查看依赖关系”。这将打开一个新窗口,您可以在其中看到依赖于您的表以及您的表所依赖的所有其他对象(不仅仅是表)。
In SQL server management studio, you can right click your table in the object explorer, and then select 'View Dependencies'. This will open a new window in which you can see all other objects (not just tables) that depend on your table, and on which your table depends.
这是我部分基于上述答案整理的存储过程。
Here is a stored procedure I put together based in part on the above answer.
使用 SSMS GUI:
在 SQL Server Management Studio (SSMS) 中,您可以右键单击表并选择“查看依赖关系”。这将打开一个新窗口,您可以在其中看到依赖于您的表以及您的表也依赖于的所有对象。
另外,如果您想使用 TSQL 执行此操作,其中所有对象都依赖于您的表,
则方法 1:使用 sp_depends 存储过程,尽管 sql server 团队将在未来版本中删除此功能,但它仍然有用到获取指定对象的所有依赖关系,包括表、视图、存储过程、约束等,sql server 团队建议使用 sys.dm_sql_referencing_entities 和 sys.dm_sql_referenced_entities 代替。
方法 2:
方法 3:在函数、过程和视图中查找表依赖
项 方法 4:
如果您想获取表所依赖的所有对象。
Using SSMS GUI:
In SQL server management studio (SSMS), you can right click your table and select 'View Dependencies'. This will open a new window in which you can see all the objects that depend on your table, and on which your table depends also.
Additionally If you want to do it with TSQL in where all objects that depends on your table
Approach-1: Using sp_depends store procedure , though sql server team is going to remove this feauture in future version but it still useful to get all the dependencies on the specified Object, includes Tables, Views, Stored Procedures, Constraints, etc., sql server team recommend to use sys.dm_sql_referencing_entities and sys.dm_sql_referenced_entities instead.
Approach-2:
Approach-3: Find Table dependencies in Function, Procedure and View
Approach-4:
If you want to get all objects on which your table depends on.
如果您已将这些定义为外键,则只需检查表设计并查看“关系”对话框,该对话框将显示为表定义的所有内容。
或者,您可以使用“查看依赖关系”。
If you've got these defined as foreign keys then just examine the table design and look at the Relationships dialog which will show you everything that's defined for the table.
Alternatively you can use "View Dependencies".
试试这个
Try this
获取外键的另一种选择。
这将产生所有外键及其主要/依赖信息。如果从属列是从属表中主键的一部分,它还包括一个额外的列 - 有时需要注意这一点。
要仅获取ORDER BY 之前添加一个
Users
表,只需在最终的WHERE
子句即可Another option to get foreign keys.
This will yield all foreign keys and their primary/dependent information. It also includes an extra column if the dependent column is part of the primary key in the dependent table - sometimes important to note that.
To get only the
Users
table, just add aWHERE
clause before the finalORDER BY
获取具有架构名称的所有表的选项
Option to get all tables with Schema Names