Solved

Create relationship between 2 existing tables - SQL Server

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 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

22 Experts available now in Live!

Get 1:1 Help Now