?
Solved

Create relationship between 2 existing tables - SQL Server

Posted on 2008-10-02
5
Medium Priority
?
6,588 Views
Last Modified: 2010-04-21
OK,  I've googled the heck out of this one, but i don't know how to explain my problem to a search engine.  I have 2 existing tables on SQL Server that I need to create a relationship between.  I inherited this database and the table structure doesn't exactly follow the "rules".  

Primary table has Table1ID (int)  and JobNo (int)
Foreign table has Table2ID (int) and JobNo (int)

Each has 1,000s of records with Table2 - JobNo having multiple duplicates, which is ok.

In Table2 I need to create a field Table1ID to act as a foreign key to Table1.  

What kind of query do I need to run to go through each record of Table2 and cross-reference the JobNo of both tables then insert the corresponding Table1ID into Table2's Table1ID.

In other words I need a foreign key value in Table2 where the two JobNo fields match.

Hope that is not too confusing.
0
Comment
Question by:partypants
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 22

Expert Comment

by:dportas
ID: 22628963
Is JobNo unique in Table1? If not, then how are we to determine which ID from Table1 should be used for the corresponding JobNo in Table2?

If JobNo is unique in Table1 then it seems logical to make that the foreign key in Table2.
0
 
LVL 4

Accepted Solution

by:
ThorSG1 earned 2000 total points
ID: 22629476
As stated above this will not work if JobNo is not unique in Table1.  You would have to create an xref table or come up with something else.

UPDATE Table2
SET Table2.Table1ID = Table1.Table1ID
FROM Table2
INNER JOIN Table1 ON Table2.JobNo = Table1.JobNo

If you need an Xref table:
Create Table3 (
Table1ID int null
,JobNo int null

-- You might have to populate manually but this might work depending on your data.
INSERT INTO Table3 (Table1ID, JobNo)
SELECT DISTINCT Table1ID, JobNo
FROM Table1

-- Then you would run this to update from the Xref table.
UPDATE Table2
SET Table2.Table1ID = Table3.Table1ID
FROM Table2
INNER JOIN Table3 ON Table2.JobNo = Table3.JobNo
0
 
LVL 4

Expert Comment

by:ThorSG1
ID: 22658646
Sorry a forgot a )

Create Table3 (
Table1ID int null
,JobNo int null)
0
 

Author Closing Comment

by:partypants
ID: 31502573
The poster replied to correct a missing )
0
 

Author Comment

by:partypants
ID: 22663517
Thank You Thank You!!!! Exactly what I wanted to do!!!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

752 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