Solved

Create relationship between 2 existing tables - SQL Server

Posted on 2008-10-02
5
6,578 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
  • 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 500 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now