17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Now let’s see what happens when we join that to the very same (only one record) Films table:<br />

FilmID FilmName YearMade FirstName LastName<br />

1 My Fair Lady 1964 Rex Harrison<br />

1 My Fair Lady 1964 Audrey Hepburn<br />

As you can see, the result has changed a bit — we are no longer seeing things as being one-to-one, but<br />

rather one-to-two, or more appropriately, what we would call one-to-many. We can use that single<br />

record in the Films table as many times as necessary to have complete (joined) information about the<br />

matching records in the Actors table.<br />

Have you noticed how they are matching up? It is, of course, by matching up the FilmID field from the<br />

two tables to create one record out of two.<br />

The examples we have used here with such a limited data set would actually yield the same results no<br />

matter what kind of JOIN was used. Let’s move on now and look at the specifics of the different JOIN types.<br />

INNER JOINs<br />

INNER JOINs are far and away the most common kind of JOIN. They match records together based on<br />

one or more common fields, as do most JOINs, but an INNER JOIN returns only the records where there<br />

are matches for whatever field(s) you have said are to be used for the JOIN. In our previous examples, every<br />

record was included in the result set at least once, but this situation is rarely the case in the real world.<br />

Let’s modify our tables to use an INNER JOIN; here’s our Films table:<br />

FilmID FilmName YearMade<br />

1 My Fair Lady 1964<br />

2 Unforgiven 1992<br />

And our Actors table:<br />

FilmID FirstName LastName<br />

1 Rex Harrison<br />

1 Audrey Hepburn<br />

2 Clint Eastwood<br />

5 Humphrey Bogart<br />

Chapter 4: JOINs<br />

83

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!