In This Chapter
MySQL Database Server
MySQL basics
Getting data into
and out of tables
Getting relational
MySQL utilities
e all need a place for our stuff. This is true for homeown-
ers, kids with too many toys, and web application devel-
opers. For the latter crowd, we have a database server.
This is a piece of software that allows you to store data in and,
perhaps more importantly, pull data from a database.
You can think of a database as a spreadsheet; the vertical columns
represent the kinds of data that you’ll store — such as Name,
Address, and Phone Number.
There are also horizontal rows, which make up an individual data-
base record. Each row contains fields, which store the information
that belongs to each column — such as P. Sherman, 42 Wallaby
Way, Sydney, and 800-333-4332. Put it all together, and you have a
complete database of contact information.
Perhaps this doesn’t sound that incredible. After all, spreadsheets
have been around for ages, and people have long been using them
to store things such as contact information. But MySQL and other
similar systems also use relationships, which make a database very
A relational database allows you to create multiple tables of infor-
mation and then link them together in many different ways. For
example, you might have different addresses for each person. So,
one table would contain the names, while a second table might
contain the addresses. This relationship is shown in Figure 14.1.
This model shows two tables. You might imagine one record for
each person, with multiple possible records for addresses, such as
one for home and another for work. With a relational database, you
can connect these tables together very easily.
21_533994-ch14.indd 31721_533994-ch14.indd 317 2/12/10 7:20 PM2/12/10 7:20 PM
Server-Side Development
Figure 14.1
A database table relationship: people and addresses
MySQL Basics
As it happens, the relational database we’ll use is MySQL. It’s a free, open-source server that’s
quite pervasive on web servers, so a competence in this database will mean you’ll always have a
place to put your data.
In this chapter, you’re going to use the MySQL command-line client to connect with the server
and create databases and the tables that compose them. Then, I’m going to show you different
ways of putting information into a table and getting it out. You finish your tour of MySQL with a
description of some of the utilities that make working with MySQL even easier.
Creating a database
Assuming that you followed the setup steps in Chapter 13, you should now have a working
MySQL installation on your Mac; if not, it’s imperative that you have MySQL up and running to
fully appreciate the discussion in this chapter.
When dealing with the command-line client, you always activate MySQL the same way: by typ-
ing your username, password, and, if you have one, a database name. Your command might
look like this:
achilles:~ aaronvegh$ mysql -u root -pmyfunkypassword mydatabase
Substitute your own password and database name here, and note the lack of a space between
the -p switch and the actual password.
If you’re uncomfortable with your password being on full display on the command line for any-
one nearby to see, you can omit it:
21_533994-ch14.indd 31821_533994-ch14.indd 318 2/12/10 7:20 PM2/12/10 7:20 PM

Get Web Development with the Mac® now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.