question

ramanathan avatar image
ramanathan asked

How to join two table? table image attached here

![alt text][1] [1]: /storage/temp/633-how+to+join+two+table.jpg
join
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

ThomasRushton avatar image ThomasRushton ♦♦ commented ·
what result are you looking for?
1 Like 1 ·
Kev Riley avatar image Kev Riley ♦♦ commented ·
Seriously? Was it easier to open word, type that, screenshot it, save the image and upload it here, rather than just type it here? The more you can provide as real code, the easier and quicker it is for people to answer.
1 Like 1 ·

1 Answer

·
Grant Fritchey avatar image
Grant Fritchey answered
To understand how to join two tables, you need to understand how they relate to each other. One table has to have at least one column, or a set of columns, that uniquely identifies each row. We refer to that as the primary key, or a candidate key. A table can actually have more than one unique constraint and it's possible to use them all for joining tables. But usually we focus on the primary key. Now your definition doesn't show a primary key on either table, so we're in trouble. The second table in a relationship will have a column, or columns, that matches the primary key in the first table. The column, or columns, is then referred to as a foreign key. These do not have to be unique values. But, through what's called referential constraints, the values in these columns have to match the values in the first table. Your tables also don't show any kind of foreign keys It's through this referential restraint that you join two tables together. You use a process in the T-SQL that looks something like this: SELECT * FROM MyFirstTable AS mft JOIN MySecondTable as mst ON mft.MftID = mst.MftID; So the ON part of the JOIN statement is defining the relationship between the two tables. The column, MftID, is the primary key in the first table and is in the second table in order to server as a foreign key. That's how you join tables together. Now try it with your code. It shouldn't be too difficult from here (but you do need to identify the primary and foreign key columns that relates the two tables).
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.