Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I add new rows to a new table where there isn't an existing row that matches the foreign key?

Posted on 2008-10-03
4
Medium Priority
?
193 Views
Last Modified: 2010-04-21
I want to create records in a table with a value for every matching record in another table  but only if there isn't already a record.
So for the following :
Customers                  Locations      
CustomerID                  CustomerID      Location
1                  2      Unknown
2                  3      England
3                  4      Spain
4                        
5                        

I would like for the sql command to end up with the tables looking like this:
Customers                  Locations      
CustomerID                  CustomerID      Location
1                  1      Unknown
2                  2      Unknown
3                  3      England
4                  4      Spain
5                  5      Unknown
0
Comment
Question by:dgloveruk
[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
4 Comments
 
LVL 18

Accepted Solution

by:
UnifiedIS earned 1000 total points
ID: 22634044
I'm not following your table layouts but a general way to do this would be:
insert into tableA
(SELECT columns from tableB
WHERE key not in (SElect key from tableA))
0
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 1000 total points
ID: 22634050
Something like this.  You can replace with actual columns/values you want.
INSERT INTO Locations (CustomerID, Location)
SELECT CustomerID, 'Unknown'
FROM Customers
WHERE CustomerID NOT IN (SELECT CustomerID FROM Locations)
 
-- OR
INSERT INTO Locations (CustomerID, Location)
SELECT CustomerID, 'Unknown'
FROM Customers c
WHERE NOT EXISTS (SELECT 1 FROM Locations l WHERE l.CustomerID = c.CustomerID)

Open in new window

0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22634065
Guess, we had the same thought, UnifiedIS. :)
0
 

Author Closing Comment

by:dgloveruk
ID: 31502741
Thanks guys, I split the points on this occasion because you actually seemed to answer simultaneously!
Thanks again,
0

Featured Post

Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

Question has a verified solution.

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

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 …
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
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 …

670 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