Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Server: need a list of referencing tables

Posted on 2013-11-05
4
Medium Priority
?
296 Views
Last Modified: 2013-11-19
Hi, I need a list of tables which foreign keys reference primary key of the current table.
Thanks.
0
Comment
Question by:quasar_ee
  • 2
4 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39625210
Copy-paste the below T-SQL into your SSMS, execute it to verify it works, then delete the WHERE clause
CREATE TABLE boo (foo varchar(100) DEFAULT 'banana')

SELECT t.name, c.name, dc.definition
FROM sys.tables t
	JOIN sys.columns c ON t.object_id = c.object_id
	JOIN sys.default_constraints dc ON c.default_object_id = dc.object_id
WHERE c.name = 'foo'

Open in new window

0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39625255
Disregard the above, posted in the wrong question.
0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 1000 total points
ID: 39625815
You can get that by querying sys.objects and sys.foreign_key_columns.
0
 
LVL 27

Assisted Solution

by:Zberteoc
Zberteoc earned 1000 total points
ID: 39633574
One quick way is to execute the following in the database where your table is:

USE YourDatabase
GO
EXEC sp_help table_name

Open in new window


And you will get info in multiple result sets about the table. The last 2 results sets will be about FKs from your table to others and FKs from other tables referencing your table.

If you only want the constraints you can run:

USE YourDatabase
GO
EXEC sys.sp_helpconstraint table_name

Open in new window

0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question