Link to home
Start Free TrialLog in
Avatar of bjbrown
bjbrownFlag for United States of America

asked on

Join two table without like primary key

I have a clients table and a address table that do not share a common primary key. How do I join these? See attached photo.
SQL1.JPG
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

I dunno.  Close your eyes and use The Force?  Smoke, Magic, and Mirrors?  Mind reading?

Seriously, there are no columns these two tables have in common, at least in the image, so I don't see how you can pull this off.
Either you are missing a joining table, the Address table has a ClientID, or the Client table has an AddressID.  Otherwise you're SOL
Avatar of bjbrown

ASKER

Sounds like I  need a table that has a field from the other two.
It depends on the relationship you want between the two tables?

If you want a client to be able to have one or more addresses then place the ClientID in the Address table.

If you want multiple clients to be able to share the same address then place the AddressID in the Client table (unlikely).

If you want a many-to-many relationship where a single address could be shared by multiple clients and clients can have multiple address then Yes, you will need to create an intermediary join table.

CREATE TABLE ClientAddress
(
   ClientAddressID INT IDENTITY(1,1)  --optional
   ClientID INT NOT NULL,
   AddressID INT NOT NULL,
   Label VARCHAR(100)  --optional
)
Avatar of bjbrown

ASKER

Okay perhaps I'm not describing this clearly, this is only a select where we are trying to pull information out of two tables since the client table does not contain the client's address and the address table does not contain the same primary key.
If this is an existing system and you are sure that there is a relationship between the two tables then yes you are missing something.  Based on the information you provided there is no relationship between the tables.

It would help if you would provide the actual table definitions instead of incomplete screenshots.
ASKER CERTIFIED SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial