eXtropia: the open web technology company
Technology | Support | Tutorials | Development | About Us | Users | Contact Us
Resources
 ::   Tutorials
 ::   Presentations
Perl & CGI tutorials
 ::   Intro to Perl/CGI and HTML Forms
 ::   Intro to Windows Perl
 ::   Intro to Perl 5
 ::   Intro to Perl
 ::   Intro to Perl Taint mode
 ::   Sherlock Holmes and the Case of the Broken CGI Script
 ::   Writing COM Components in Perl

Java tutorials
 ::   Intro to Java
 ::   Cross Browser Java

Misc technical tutorials
 ::   Intro to The Web Application Development Environment
 ::   Introduction to XML
 ::   Intro to Web Design
 ::   Intro to Web Security
 ::   Databases for Web Developers
 ::   UNIX for Web Developers
 ::   Intro to Adobe Photoshop
 ::   Web Programming 101
 ::   Introduction to Microsoft DNA

Misc non-technical tutorials
 ::   Misc Technopreneurship Docs
 ::   What is a Webmaster?
 ::   What is the open source business model?
 ::   Technical writing
 ::   Small and mid-sized businesses on the Web

Offsite tutorials
 ::   ISAPI Perl Primer
 ::   Serving up web server basics
 ::   Introduction to Java (Parts 1 and 2) in Slovak

 

Introduction to Databases for Web Developers
Retrieving Data  
Okay, enough of all that DB Admin stuff. Let's get down to the nitty gritty of retrieving data from your database.

In SQL, the "SELECT - FROM" statement is used to grab data from a database. The statement follows the generic syntax of:

     SELECT column_name
     FROM table_name;

There are a few things to notice about the format of this SQL statement that will help us understand all SQL statement formats.

First, notice that the statement ends in semicolon. Like English, SQL requires termination punctuation so that the SQL database will know when you are done speaking to it. In the case of SQL, the semicolon (;) character works like the period in English.

Second, notice that the statement spans multiple lines. This is more of a convention than a necessity. Like HTML, SQL ignores whitespace in a statement, so you could just as easily write SELECT column_name FROM table_name. The reason we break the statement up into multiple lines is to increase readability. As your SQL statements get more and more complex, you will find that if you break them up into logical blocks, they will be easier to read.

Finally, notice that we have used all uppercase letters for our keywords (like SELECT and FROM). This is a good idea as even though most implementations of SQL are case insensitive, some are. Okay, let's get back to the example...

In other words, if you wanted to get all the names of the employees in the EMPLOYEE table, you would type:

     SELECT EMP_NAME
     FROM EMPLOYEES;

In the case of our sample database, you would get the following results:

    EMP_NAME
    -------------
    Lim Li Chuen
    Lim Sing Yuen
    Loo Soon Keat
    -------------

We are going to be using the sample database throughout the day in order to show examples, so get familiar with the tables.

You needn't limit yourself to single columns of course, The column_name parameter may take a comma delimited list so that if you also wanted a report for the employee number and salary, for example, you would simply use:

     SELECT EMP_NAME, EMP_NUMBER, EMP_SALARY
     FROM EMPLOYEES;

In this case, you would get the following results:

    EMP_NAME		EMP_NUMBER 	EMP_SALARY
    ------------------------------------------
    Lim Li Chuen	001		90,000
    Lim Sing Yuen	002		40,000
    Loo Soon Keat	003		50,000
    ------------------------------------------

Note that if you specify a column name that does not exist, you will get an error.

Previous | Next | Table of Contents