MySQL
  Home arrow MySQL arrow Page 2 - Taking a Look at MySQL 4.1
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 
Moblin 
JMSL Numerical Library 
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? 
MYSQL

Taking a Look at MySQL 4.1
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 10
    2006-04-20

    Table of Contents:
  • Taking a Look at MySQL 4.1
  • MySQL 4.1
  • Subqueries As Scalar Values
  • Benefits of Subqueries
  • Other New Features in MySQL 4.1

  • 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


    Taking a Look at MySQL 4.1 - MySQL 4.1


    (Page 2 of 5 )

    We’ve already talked about some of the features that are new in MySQL 4.1, such as prepared statements and multiple statements and the SHOW WARNINGS command, in previous chapters. In this section we’ll discuss what are probably the two most important new additions in MySQL 4.1 because of the increased speed, flexibility, and power they lend to SELECTqueries. These are subqueries (also known as nested queries or subselects) and derived tables. In addition, as we’ll see later in this chapter, they mark an important stage in MySQL’s evolution, as they pave the way for enterprise-level features such as stored procedures, triggers, and views.

    Subqueries and Derived Tables

    Try as we might to combine queries using the various types of joins, SQL functions, and operators that we’ve looked at previously in this book, sometimes it’s necessary to use more than one query to derive the exact data that we require. Even in cases where we can use a single query to obtain a desired result, we find that what seems conceptually very simple often requires very complex joins or unions. In MySQL 4.1, these problems can often be overcome through the use of subqueries, that is, queries within other queries. Being able to use subqueries can ease matters greatly, and they can allow us to combine queries or simplify them considerably. They also provide for structured queries, in which the different parts of a query can be considered apart from the others. This tends to make them much more easily read and understood than the more complex statements that they can be used to replace.

    Subqueries can be used in several places within queries and in several ways. In the next few examples, we’ll use two tables, representing products and categories of products. (You may recognize some of the products table data from the QA Testing example in Chapter 2.) These tables are structured as shown in the following twoCREATE TABLEstatements:

    CREATE TABLE products (
      id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
      category_id INT(11) NOT NULL,
      name VARCHAR(30) NOT NULL,
      price DECIMAL(6,2) NOT NULL
    );
    CREATE TABLE categories (
      id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
      name VARCHAR(30) NOT NULL
    );

    Each product has a name and a price. Each product belongs to a category (and only one category); this relationship is indicated by the category_id column in the products table, which serves as a foreign key linking to the categories table.

    The table creation statements and some statements to insert sample data are included in the ch8 directory of the code download for this book (available from the Downloads section ofhttp://www.apress.com).

    Subqueries can be used in any of theSELECT,INSERT,UPDATE,DELETE, orSETstatements. (They can also be used with theDOstatement, which we’ll see when we discuss stored procedures later in this chapter.) Subqueries can include any of the constructs found in any otherSELECTquery, includingFROMandWHEREclauses, joins,LIMITclauses, unions, function calls, and so forth.

    More MySQL Articles
    More By Apress Publishing


       · This article is an excerpt from the book "Beginning MySQL Database Design and...
     

    Buy this book now. This article is excerpted from chapter eight of Beginning MySQL Database Design and Optimization: From Novice to Professional, written by Jon Stephens and Chad Russell (Apress, ISBN: 1590593324). Check it out today at your favorite bookstore. Buy this book now.

       

    MYSQL ARTICLES

    - Take Some Load off MySQL with MemCached
    - MySQL Table Prefix Changer Tool in PHP
    - Using the SIGNAL Statement for Error Handling
    - Error Handling Examples
    - Error Handling
    - Completing a Search Engine with MySQL and PH...
    - Paginating Result Sets for a Search Engine B...
    - Building a Search Engine with MySQL and PHP 5
    - Using Boolean Operators for Full Text and Bo...
    - PHP, MySQL and the PEAR Database
    - Working with PHP and MySQL
    - Getting PHP to Talk to MySQL
    - Creating an RSS Reader: the Reader
    - MySQL Security Overview
    - Creating the Admin Script for a PHP/MySQL Bl...





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