SunQuest
 
       Administration
  Home arrow Administration arrow Page 6 - Database Essentials
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ADMINISTRATION

Database Essentials
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2001-02-26

    Table of Contents:
  • Database Essentials
  • The Customer Is King
  • Relationships
  • Invasion Of The Foreign Keys
  • Looking Up The Index
  • Joined At The Hip
  • Room With A View
  • Pulling The Trigger
  • So That's Where All My Money Went...

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!

    Database Essentials - Joined At The Hip


    (Page 6 of 9 )

    Most databases also allow you to query two or more tables at a time, and display a combined set of results. This is technically referred to as a "join", since it involves "joining" different tables at specific points to create new views of the data.

    Here's an example of a simple join:

    sql> SELECT names.CustomerID, FirstName, LastName, StreetAddress, City, State, Zip, Country FROM names, addresses WHERE names.CustomerID = addresses.CustomerID;
    +------------+-----------+----------+------------------------------+---------------+-------+--------+---------+
    | CustomerID | FirstName | LastName | StreetAddress                | City          | State | Zip    | Country |
    +------------+-----------+----------+------------------------------+---------------+-------+--------+---------+
    |     234673 | John      | Doe      | 12, Some Street              | Nowheresville | ZZ    | 748202 |      56 |
    |     734736 | Julius    | Caesar   | The Palace Behind The Lake   |Ancient Rome   | NA    |      0 |       0 |
    |       1243 | Daffy     | Duck     | Next Door To The Rabbit Hole |    Toontown   | TT    |  98765 |       0 |
    +------------+-----------+----------+------------------------------+---------------+-------+--------+---------+
    


    In this case, the "names" and "addresses" tables have been joined together through the common column "CustomerID".

    And you can also nest one query within another, such that the result of the inner query provides data for the outer query. Such a query is referred to as a "subquery", and it allows a great deal of flexibility when formulating long and complex queries.

    Let's suppose you wanted to find out the names and addresses of all customers with an account balance greater than $1000.00

    sql> SELECT FirstName, LastName, StreetAddress, City, State, Zip, Country FROM names, addresses WHERE names.CustomerID = addresses.CustomerID AND names.CustomerID IN (SELECT CustomerID FROM balance WHERE AccountBalance >= 1000);

    In this case, SQL will first execute the inner query

    sql> SELECT CustomerID FROM balance WHERE AccountBalance >= 1000;+------------+| CustomerID |+------------+| 734736 || 234673 |+------------+


    and then assign the return value(s) to the outer query, which will display the result.

    sql>SELECT FirstName, LastName, StreetAddress, City, State, Zip, Country FROM names, addresses WHERE names.CustomerID = addresses.CustomerID AND names.CustomerID IN ('734736', '234673'); +-----------+----------+----------------------------+---------------+-------+--------+---------+ | FirstName | LastName | StreetAddress | City | State | Zip | Country | +-----------+----------+----------------------------+---------------+-------+--------+---------+ | John | Doe | 12, Some Street | Nowheresville | ZZ | 748202 | 56 | | Julius | Caesar | The Palace Behind The Lake | Ancient Rome | NA | 0 | 0 | +-----------+----------+----------------------------+---------------+-------+--------+---------+

    There is a limit on the number of sub-queries you can use in a single SQL statement, but it's usually quite a comfortable number.

    More Administration Articles
    More By icarus, (c) Melonfire


     

       

    ADMINISTRATION ARTICLES

    - Scalix: Linux Administrator`s Guide
    - Network Administration with FreeBSD 7
    - Components of an Information Architecture
    - The Anatomy of an Information Architecture
    - Configuring Load-Balanced Clusters
    - Load-Balanced Clusters
    - UNIX Time Format Demystified
    - Making Changes in the CVS
    - Building Your First CVS Repository
    - CVS Quickstart Guide
    - Authorizing Users in Samba
    - Handling User Accounts in Samba
    - Authentication in Samba
    - Accounts, Authentication, and Authorization
    - Advanced Concepts on Dealing with Files and ...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway