
June 23rd, 2008, 09:42 AM
|
|
Contributing User
|
|
Join Date: Jan 2008
Location: Connecticut, USA
Posts: 34
Time spent in forums: 9 h 37 m 44 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by Zuhaib.bukhari [SIZE=4]Just tell me How I create relations in Tables in Sql Server 2000. pls I need ur Help. |
Code:
SELECT field1, field2, fieldN
FROM table1 INNER JOIN table2 ON table1.field1=table2.field1
INNER JOIN => the relationship (INNER JOIN means "give me all of the records where these two fields match in these two tables)
ON => what field do you want to match between tables. They do not need the same name but the same type.
Code:
The tables...
Table1
UserId FName
-------------------
1 John
2 Dave
3 Bob
5 Drew
Table2
UserId LName
-------------------
1 Smith
2 Jones
4 Gates
The Code ...
SELECT Table1.FName, Table2.LName
FROM Table1 INNER JOIN Table2 ON Table1.UserId=Table2.UserId
The Results ...
FName LName
-------------------
John Smith
Dave Jones
NOTE:
Since Table1 does not have a UserId of "4" and Table2 does the record in Table2 is not grabbed. Only where the record exists in BOTH tables will it show up.
Likewise, Table1 does has a UserId value of "5" and Table2 does not so that record is not grabbed.
If you are going to do much with SQL then I highly recommend getting a book that goes over it in detail.
For example, there are different types of JOIN between tables; INNER, OUTER and each has their own benefits and purposes.
I started with a Wrox book that has helped me. If you are going to be doing a lot of writing queries and such, perhaps http://www.wrox.com/WileyCDA/WroxTitle/productCd-076457955X.html would help.
|