SQL Server Central is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 

Effectively mapping primary key – foreign key relations

By John Liu, 2008/01/10

Total article views: 833 | Views in the last 30 days: 61

To properly locating the primary key - foreign key relations in a database sounds like a tough task. Mohit Nayyar posted a technique to find out primary key and foreign key that have the same column names. The limitation of that is that, in reality, column names are not always the same in a database. For example, in the AdvantureWorks sample database in SQL2005, the SalesPersonID in Sales.SalesPerson table has a foreign key reference to the EmployeeID column in the HumanResources.Employee table.

This script will map the primary key and foreign key, regardless if the column names are same or not. This script has been tested on both SQL2000 and 2005. This is another evidence of the usefulness of the information_schema view.

If you are interest in a particular column, simple add " and ccu.column_name='<column name>'" in the where cluse.

Mohit's Column: http://www.sqlservercentral.com/scripts/Miscellaneous/31983/

By John Liu, 2007/11/07

By John Liu, 2008/01/10

Total article views: 833 | Views in the last 30 days: 61
Your response
 
 
Related tags

Miscellaneous    
T-SQL Aids    
 
Like this? Try these...

Don't forget the Title

By Absinthe | Category: T-SQL
| 625 reads
Already registered?  

Free registration required

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Register

E-mail address:
Password:
Password (confirm):

  

Subscriptions

We ask you to register on the site and subscribe to our newsletters. Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

We ask that you give the newsletter a try for a week. Over 200,000 SQL Server Professionals a day find it entertaining and useful. If not, you are welcome to unsubscribe at anytime.

Steve Jones
Editor, SQLServerCentral.com