Solved

Create relationship between 2 existing tables - SQL Server

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Merge join vs exist 3 37
SQL Query - Multiple match on field with no extras 7 36
SQL Server Serialization error 8 44
When are cursors useful? 8 58
In this article I will describe the Backup & Restore 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.
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

737 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