03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

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

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

436 x CHAPTER 12 INTRODUCTION TO DATABASES<br />

In addition, you can use other jo<strong>in</strong>s <strong>in</strong>clud<strong>in</strong>g cross jo<strong>in</strong>s <strong>and</strong> self jo<strong>in</strong>s. For a detailed description<br />

of these types of jo<strong>in</strong>s, pick up a copy of the book <strong>Beg<strong>in</strong>n<strong>in</strong>g</strong> Microsoft SQL Server 2012<br />

Programm<strong>in</strong>g by Paul Atk<strong>in</strong>son <strong>and</strong> Robert Vieira, Wrox, 2012 (ISBN: 978-1-1181-0228-2).<br />

You see how to use a very common type of jo<strong>in</strong>, the INNER JOIN, <strong>in</strong> the next Try It Out.<br />

TRY IT OUT<br />

Jo<strong>in</strong><strong>in</strong>g Data<br />

To jo<strong>in</strong> data from two tables, you need to write a JOIN statement <strong>in</strong> your code. To help you write the<br />

code, SSMS adds a JOIN for you whenever you add related tables to the Diagram pane. However, sometimes<br />

this JOIN is not correct, so you’ll need to check the code to see if it’s okay.<br />

1 . Still <strong>in</strong> your test database <strong>in</strong> SSMS, right-click the Review table <strong>and</strong> choose Edit Top 200 Rows.<br />

You’ll see all the reviews <strong>in</strong> the table appear. Next, enable the Diagram, Criteria, <strong>and</strong> SQL panes<br />

by click<strong>in</strong>g their respective buttons on the Query Designer toolbar.<br />

2. Right-click an open spot of the Diagram pane next to the Review table <strong>and</strong> choose Add Table.<br />

Alternatively, choose Query Designer Í Add Table from the ma<strong>in</strong> menu.<br />

3. In the dialog box that opens, click the Genre table <strong>and</strong> then click the Add button. F<strong>in</strong>ally, click<br />

Close.<br />

4. The SQL statement that SSMS generated looks like this:<br />

SELECT TOP (200) Review.Id, Review.Title, Review.Summary, Review.Body,<br />

Review.GenreId, Review.Authorized, Review.CreateDateTime, Review.UpdateDateTime<br />

FROM Review<br />

INNER JOIN Genre ON Review.GenreId = Genre.Id<br />

SSMS correctly detected the relationship def<strong>in</strong>ed <strong>in</strong> the database between the GenreId column of<br />

the Review table <strong>and</strong> the Id column of the Genre table, <strong>and</strong> applied the correct JOIN for you.<br />

5. To see how you can create JOINs yourself without writ<strong>in</strong>g code directly, you’ll manually re-create<br />

the JOIN. First, right-click the l<strong>in</strong>e that is drawn between the two tables <strong>in</strong> the Diagram pane <strong>and</strong><br />

choose Remove. The SQL statement now conta<strong>in</strong>s a CROSS JOIN.<br />

6. Next, click the GenreId column of the Review table <strong>in</strong> the Diagram pane once <strong>and</strong> drag it onto the<br />

Id column of the Genre table. As soon as you release the mouse, SSMS creates a new INNER JOIN<br />

<strong>in</strong> the SQL pane for you with the exact same code as you saw earlier. SQL Server underst<strong>and</strong>s the<br />

primary <strong>and</strong> foreign keys that have been set up <strong>in</strong> the database tables <strong>and</strong> correctly jo<strong>in</strong>s the primary<br />

key of the Genre table (Id) to the foreign key of the Reviews table (GenreId).<br />

7. Modify the SQL statement so it selects only the Id <strong>and</strong> the Title columns from the Review<br />

table <strong>and</strong> the Name column from the Genre table. You can do this by alter<strong>in</strong>g the SQL statement<br />

manually or by uncheck<strong>in</strong>g the columns <strong>in</strong> the Diagram pane. Your SQL statement should now<br />

look like this:<br />

SELECT TOP (200) Review.Id, Review.Title, Genre.Name<br />

FROM Review INNER JOIN Genre ON Review.GenreId = Genre.Id

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

Saved successfully!

Ooh no, something went wrong!