Chapter 4: Fetching Data from the Database (Basics)


In Chapter 3, you learn to connect to the database and to create MySQL database tables. In this chapter, you work with data in the tables.

You'll be learning about the SQL SELECT keyword. SELECT is the SQL keyword you will use most often by far (along with FROM). All queries you perform on a database will begin with the SELECT keyword. Another keyword that will be in all of the queries you'll perform is FROM. SELECT tells the DBMS what you are looking for, and FROM tells the DBMS where to look for the data. SELECT and FROM are like Abbot and Costello-when you have one, you can always count on the other one being nearby.

When you create a query using the SQL keywords and table fields, the finished product is called a SELECT statement. Once you learn some of the basics of SELECT statements, we'll move on to some of the different ways to gain access to the data using the various methods the DBI gives us.

Following is the database structure used for this chapter, as well as the SQL statements needed to create the table if you have not yet done so. If you are using MySQL, you may want to download the code from the Web site for this book ( The code contains the tables and some sample data you can import into the tables-which should save you a lot of time.

+-----------+--------------+------+-----+---------+-------+ | Field     | Type         | Null | Key | Default | Extra | +-----------+--------------+------+-----+---------+-------+ | isbn      | varchar(20)  | YES  |     | NULL    |       | | title     | varchar(255) | YES  |     | NULL    |       | | author    | varchar(255) | YES  |     | NULL    |       | | price     | varchar(20)  | YES  |     | NULL    |       | | format    | varchar(50)  | YES  |     | NULL    |       | | publisher | varchar(255) | YES  |     | NULL    |       | | pubdate   | varchar(50)  | YES  |     | NULL    |       | | notes     | text         | YES  |     | NULL    |       | +-----------+--------------+------+-----+---------+-------+

Below is the SQL to create a database in MySQL:

CREATE TABLE library (   isbn varchar(20) default NULL,   title varchar(255) default NULL,   author varchar(255) default NULL,   price varchar(20) default NULL,   format varchar(50) default NULL,   publisher varchar(255) default NULL,   pubdate varchar(50) default NULL,   notes text ) TYPE=MyISAM;

Perl Database Programming
Perl Database Programming
ISBN: 0764549561
EAN: 2147483647
Year: 2001
Pages: 175

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: