Inserting two tables together with no Id or UNIQUE anything
Question
Let's assume we have two tables:
table1
, retaining the following data:Name Height Age ---- ------ --- Mary 1.60 18 Mary 1.62 19 Mary 1.68 20 John 1.80 17 John 1.85 18 John 1.86 19
table2
, with the rows shown below:Name Height Age ---- ------ --- Mary 1.60 18 Mary 1.62 19 Mary 1.68 20 John 1.80 17 John 1.85 18 John 1.86 19 Vince 1.78 22 Vince 1.80 23
How would I JOIN these two tables using Sybase so that afterward table1
has Vinces data in it?
Think of table2
as a temporary table that contains new information. I want to update table1
with the new information.
Solution
Should be:
insert into table1
select * from table2
except
select * from table1;
You could also use:
select *
from table2
where Name not in (select Name from table1)
This works too (assuming Name
cannot be NULL
).
The difference is that the except
version delivers all rows that are not the same whereas the not in
query delivers all rows where the name does not exist. Up to you to decide if this is what you need.
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange