1

I want a simple way to find all the Foreign Keys of my table.

For example having a table Customers, I want to find the tables with a relationship to this table, I use to find relationships using the Diagram but it is too slow.

Desired result = Customers_Accounts, Customers_Cities, Customers_Properites, etc
  • Possible duplicate of [How can I list all foreign keys referencing a given table in SQL Server?](https://stackoverflow.com/questions/483193/how-can-i-list-all-foreign-keys-referencing-a-given-table-in-sql-server) – The_Black_Smurf Aug 31 '17 at 00:48

2 Answers2

0

Yes just select your table and press ALT + F1, at the bottom of the result set you will see all the references

Victor Hugo Terceros
  • 2,969
  • 3
  • 18
  • 31
  • 3
    Probably worth mentioning that ALT F1 executes sp_help ‘object_name’, where object_name is the highlighted table. Keyboard shortcuts can easily be changed by users or plugins. – twoleggedhorse Aug 31 '17 at 00:25
0

The reason that the dependencies is slow is due to the number and complexity of queries that sql sever runs in order to show you a nice hierarchical structure.

There are some ways to get the data you need which vary depending on the version of sql and how much info you are looking for.

Taken from Microsoft Docs - View the Dependencies of a Table

USE AdventureWorks2012;  
GO  
SELECT * FROM sys.sql_expression_dependencies  
WHERE referencing_id = OBJECT_ID(N'Production.vProductAndDescription');   
GO

Obviously you'll need to change the AdventureWorks2012 to your database and Production.vProductAndDescription to your Customer schema and table name.

The above works for the latest sql server editions, for older editions you may need to refer to the following links:

MSSQL Tips - Different Ways to Find SQL Server Object Dependencies

MSSQL Tips - Listing SQL Server Object Dependencies

twoleggedhorse
  • 4,938
  • 4
  • 23
  • 38
  • 1
    running this I always get 0 rows and I am sure my table has FKs **SELECT * FROM sys.sql_expression_dependencies WHERE referencing_id = OBJECT_ID(N'dbo.Participation');** –  Aug 31 '17 at 00:25
  • @Kevin what version of sql server are you using? – twoleggedhorse Aug 31 '17 at 00:27