Joins

Team-Fly    

Macromedia® DreamWeaver® MX Unleashed
By Matthew Pizzi, Zak Ruvalcaba
Table of Contents
Chapter 27.  SQL Primer


Up to this point you have focused primarily on extracting data from a single table. Depending on how advanced your database becomes, at times you might want to extract data from multiple tables at once. If that is the case, you will need to use joins. Although there are several types of joins, two types will be covered here:

  • Inner Joins

  • Outer Joins

Inner Joins

Of the different types of joins, inner joins are by far the most popular. Inner joins allow you to see all the records of two tables that have a relation established for one another. Remember, the customers table and the creditcards table had a relationship established. The two tables are similar to the following:

CustomerID FirstName LastName
2 Zak Ruvalcaba
4 Matthew Pizzi

CustomerID CreditCardType CreditCardExpiration
2 Visa 5555555555555555
4 MasterCard 3333444455553333

Assume that you wanted to extract the information from the customers table for CustomerID 2. Before inner joins, you would have to perform two Select statements:

 Select * From Customers Where CustomerID = 2  

And

 Select * From CreditCards Where CustomerID = 2  

You can begin to see how tedious this could get, not to mention that it is completely inefficient. To solve this problem, an inner joins could be performed:

 Select Customers.CustomerID, Customers.FirstName, Customers.LastName,  CreditCards.CustomerID, CreditCards.CreditCardType, CreditCards.CreditCardExpiration From Customers Inner Join CreditCards On Customers.CustomerID = CreditCards.CustomerID 

The joins would effectively produce one virtual table with the following results:

CustomerID FirstName LastName CreditCardType CreditCardExpiration
2 Zak Ruvalcaba Visa 555555555555555
4 Matthew Pizzi MasterCard 3333444455553333

You will notice that the preceding table now becomes more efficient and manageable. Notice that rather than referencing the names of the tables, you used TableName.Field notation. This is crucial when using joins; otherwise, you would end up with two CustomerIDs without a direct reference to its corresponding table.

Outer Joins

Outer joins enable rows to be returned from a join in which one of the tables does not contain matching rows for the other table. Suppose you have two tables that contain the following information:

CustomerID FirstName AddressID
2 Zak 45634
4 Matthew 34754
5 Jessica  
10 David 97895

AddressID Address
45634 555 Sample St., San Diego
34754 343 Chestnut Rd., San Diego
97895 523 Main St., San Diego

If you were to use an inner join on the preceding tables, such as:

 Select Customers.FirstName, Customers.AddressID, Address.AddressID,  Address.Address From Customers Inner Join Address On Customers.AddressID = Address.AddressID 

It would return the following results:

FirstName AddressID Address
Zak 45634 555 Sample St., San Diego
Matthew 34754 343 Chestnut Rd., San Diego
David 97895 523 Main St., San Diego

Notice that the record that did not contain an AddressID was excluded. If an outer join were performed, such as:

 Select Customers.FirstName, Customers.AddressID, Address.AddressID,  Address.Address From Customers Outer Join Address On Customers.AddressID = Address.AddressID 

The results would be slightly different:

FirstName AddressID Address
Zak 45634 555 Sample St., San Diego
Matthew 34754 343 Chestnut Rd., San Diego
David 97895 523 Main St., San Diego
Jessica    


    Team-Fly    
    Top


    Macromedia Dreamweaver MX Unleashed
    Macromedia Dreamweaver MX 2004 Unleashed
    ISBN: 0672326310
    EAN: 2147483647
    Year: 2002
    Pages: 321

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net