Database Articles
  Home arrow Database Articles arrow Page 4 - Query Primer
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Forums Sitemap 
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
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? 
DATABASE ARTICLES

Query Primer
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2007-10-25

    Table of Contents:
  • Query Primer
  • Query Clauses
  • The select Clause
  • Column Aliases

  • 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


    Query Primer - Column Aliases


    (Page 4 of 4 )

    Although the mysql tool will generate labels for the columns returned by your queries, you may want to assign your own labels. While you might want to assign a new label to a column from a table (if it is poorly or ambiguously named), you will almost certainly want to assign your own labels to those columns in your result set that are generated by expressions or built-in function calls. You can do so by adding a column alias after each element of your selectclause. Here’s the previous query against theemployeetable with column aliases applied to three of the columns:

      mysql> SELECT emp_id,
          ->   'ACTIVE' status,
          ->   emp_id * 3.14159 empid_x_pi,
          ->   UPPER(lname) last_name_upper
          -> FROM employee;

     

    emp_id  status  empid_x_pi  last_name_upper 

     

     

    1  ACTIVE 

    3.14159  SMITH

      

     

    2  ACTIVE 

    6.28318  BARKER

     

     

    3  ACTIVE 

    9.42477  TYLER

     

     

    4 ACTIVE 

    12.56636  HAWTHORNE

     

     

    5  ACTIVE 

    15.70795  GOODING

     

     

    6  ACTIVE 

    18.84954  FLEMING

     

     

    7 ACTIVE 

    21.99113  TUCKER

     

     

    8 ACTIVE 

    25.13272 PARKER

     

     

    9 ACTIVE

    28.27431  GROSSMAN

     

     

    10  ACTIVE 

    31.41590 ROBERTS

     

     

    11  ACTIVE 

    34.55749  ZIEGLER

     

     

    12  ACTIVE 

    37.69908 JAMESON

     

     

    13 ACTIVE 

    40.84067  BLAKE

     

     

    14  ACTIVE

    43.98226 MASON

     

     

    15 ACTIVE 

    47.12385 PORTMAN

     

     

    16  ACTIVE

    50.26544 MARKHAM

     

     

    17  ACTIVE 

    53.40703  FOWLER

     

     

    18 ACTIVE 

    56.54862 TULMAN

     

     

    18 rows in set (0.00 sec)

    If you look at the column headers, you can see that the second, third, and fourth columns now have reasonable names instead of simply being labeled with the function or expression that generated the column. If you look at theselectclause, you can see how the column aliasesstatus,empid_x_pi, andlast_name_upperare added after the second, third, and fourth columns. I think you will agree that the output is easier to understand with column aliases in place, and it would be easier to work with programmatically if you were issuing the query from within Java or C# rather than interactively via the mysql tool.

    Please check back next week for the continuation of this article.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · This article is an excerpt from the book "Learning SQL," published by O'Reilly. We...
     

    Buy this book now. This article is excerpted from chapter three of the book Learning SQL, written by Alan Beaulieu (O'Reilly; ISBN: 0596007272). Check it out today at your favorite bookstore. Buy this book now.

    DATABASE ARTICLES ARTICLES

    - More on Query Optimization for Oracle Databa...
    - Query Optimization in Oracle
    - Clusters and Other Data Structures for Oracle
    - Using Indexes with an Oracle Database
    - The Basics of Data Structures in Oracle
    - Oracle Data Structures
    - Best Practices for PL/SQL Variables
    - What`s Code Without Variables?
    - Clauses, Sorting, and SQL Queries
    - The From Clause and SQL Queries
    - Query Primer
    - Full Text Searches and Strings
    - Searching with Strings
    - Pattern Matching with Strings
    - Working with Cases of Strings





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT