.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> SQL Query >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Query to Get the Forgien Key Name Their Primary Table and Referrence Table

Posted By:Lakhan Pal Garg       Posted Date: November 19, 2009    Points: 10    Category: SQL Query    URL: http://www.dotnetspark.com  
 

Following Query will give you the List of Forgien Key in a Database with Their Primary and Referrence Tables Name.

Sample Query:


SELECT F.name AS ForgienKey,O2.name AS ParentTable,O3.name AS RefTable FROM sys.foreign_keys F
INNER JOIN Sys.Objects O2 ON O2.object_id=F.parent_object_id
INNER JOIN Sys.Objects O3 ON O3.object_id=F.referenced_object_id


If you want to get forgien key for a particular table then you can use like this:

SELECT F.name AS ForgienKey,O2.name AS ParentTable,O3.name AS RefTable FROM sys.foreign_keys F
INNER JOIN Sys.Objects O2 ON O2.object_id=F.parent_object_id
INNER JOIN Sys.Objects O3 ON O3.object_id=F.referenced_object_id
Where O3.name='Tbl_User'


Thanks & Regards
Lakhan Pal Garg

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend