Solved

Create relationship between 2 existing tables - SQL Server

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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