Getting Started with MySQL and SQL
This chapter provides an introduction to the MySQL relational database management system (RDBMS), and to the Structured Query Language (SQL) that MySQL understands. It lays out basic terms and concepts you should understand, describes the sampdb sample database that we'll use for examples, and serves as a tutorial that shows you how to use MySQL to create a database and interact with it.
Begin here if you are new to databases and perhaps uncertain whether you need one or can use one. You should also read the chapter if you don't know anything about MySQL or SQL and need an introductory guide to get started. Readers who have experience with MySQL or with database systems might want to skim through the material. However, everybody should read the section "A Sample Database," to become familiar with the purpose and contents of the sampdb database that is used throughout the book.
How MySQL Can Help You
This section describes situations in which the MySQL database system is useful. This will give you an idea of the kinds of things MySQL can do and the ways in which it can help you. If you don't need to be convinced about the usefulness of a database systemperhaps because you've already got a problem in mind and just want to find out how to put MySQL to work helping you solve ityou can proceed to "A Sample Database."
A database system is essentially a high-powered way to manage lists of information. The information can come from a variety of sources. It might be research data, business records, customer requests, sports statistics, sales reports, personal hobby information, personnel records, bug reports, or student grades. However, although database systems can deal with a wide range of information, you don't use such a system for its own sake. If a job is easy to do already, there's no reason to drag a database into it just to use one. A grocery list is a good example: You write down the items to get, cross them off as you do your shopping, and then throw the list away. It's highly unlikely that you'd use a database for this. Even if you have a palmtop computer, you'd probably keep track of a grocery list using its notepad function rather than its database capabilities.
The power of a database system comes into play when the information you want to organize and manage becomes so voluminous or complex that your records become more burdensome than you care to deal with by hand. Clearly this is the case for large corporations processing millions of transactions a day; a database is a necessity under such circumstances. But even small-scale operations involving a single person maintaining information of personal interest might require a database. It's not difficult to think of scenarios in which a database can be beneficial, because you needn't have huge amounts of information before that information becomes difficult to manage. Consider the following situations:
Your carpentry business has several employees. You need to maintain employee and payroll records so that you know who you've paid and when, and you must summarize those records so that you can report earnings statements to the government for tax purposes. You also need to keep track of the jobs your company has been hired to do and which employees you've scheduled to work on each job.
You run a network of automobile parts warehouses and need to be able to tell which ones have any given part in their inventory so that you can fill customer orders.
That pile of research data you've been collecting over the course of many years needs to be analyzed for publication. You want to boil down large amounts of raw data to generate summary information, and to pull out selected subsets of observations for more detailed statistical analysis.
You're a teacher who needs to keep track of grades and attendance. Each time you give a quiz or a test, you record every student's grade. It's easy enough to write down scores in a gradebook, but using the scores later is a tedious chore. You'd rather avoid sorting the scores for each test to determine the grading curve, and you'd really rather not add up each student's scores when you determine final grades at the end of the grading period. Counting each student's absences is no fun, either.
The organization for which you serve as the secretary maintains a directory of members. (The organization could be anythinga professional society, a club, a repertory company, a symphony orchestra, or an athletic booster club.) You generate the directory in printed form each year for the members, based on a word processor document that you edit as membership information changes. You're tired of maintaining the directory that way because it limits what you can do with it. It's difficult to sort the entries in different ways, and you can't easily select just certain parts of each entry (such as a list consisting only of names and phone numbers). Nor can you easily find a subset of members, such as those who need to renew their memberships soonif you could, it would eliminate the job of looking through the entries each month to find those members who need to be sent renewal notices. Also, you'd really like to avoid doing all the directory editing yourself, but the society doesn't have much of a budget, and hiring someone is out of the question. You've heard about the "paperless office" that's supposed to result from electronic record-keeping, but you haven't seen any benefit from it. The membership records are electronic, but, ironically, aren't in a form that can be used easily for anything except generating paper by printing the directory!
These scenarios range from situations involving small amounts to large amounts of information. Their common characteristic is that they involve tasks that can be performed manually but that could be performed more efficiently by a database system.
What specific benefits should you expect to see from using a database system such as MySQL? It depends on your particular needs and requirements, and as illustrated by the preceding examples, those can vary quite a bit. Let's look at a type of situation that occurs frequently and is fairly representative of database use. Database management systems are often employed to handle tasks such as those for which people use filing cabinets. Indeed, a database is like a big filing cabinet in some ways, but one with a sophisticated built-in filing system. There are some important advantages of electronically maintained records over records maintained by hand. For example, if you work in a dentist's office setting in which client records are maintained, here are some of the ways MySQL can help you in its filing system capacity:
Reduced record filing time. You don't have to look through drawers in cabinets to figure out where to add a new record. You just hand it to the filing system and let it put the record in the right place for you.
Reduced record retrieval time. When you're looking for records, you don't search through each one yourself to find the ones containing the information you want. If you want to send out reminders to all patients who haven't been in for their checkup in a while, you ask the filing system to find the appropriate records for you. Of course, you do this differently than if you were talking to another person, with whom you'd say, "Please determine which patients haven't visited within the last six months." With a database, you invoke a strange incantation:
SELECT last_name, first_name, last_visit FROM patient WHERE last_visit < DATE_SUB(CURDATE(), INTERVAL 6 MONTH);
That can be pretty intimidating if you've never seen anything like it before, but the prospect of getting results in a second or two rather than spending an hour shuffling through your records should be attractive. (In any case, you needn't worry. That odd-looking bit of gobbledygook won't look strange for long. In fact, you'll understand exactly what it means by the time you've finished this chapter.)
Flexible retrieval order. You needn't retrieve records according to the fixed order in which you store them (by patient's last name, for example). You can tell the filing system to pull out records sorted in any order you like: by last name, insurance company name, date of last visit, and so forth.
Flexible output format. After you've found the records in which you're interested, there's no need to copy the information manually. You can let the filing system generate a list for you. Sometimes you might just print the information. Other times you might want to use it in another program. For example, after you generate the list of patients who are overdue on their dental visits, you might feed this information into a word processor that prints out notices that you can send to those patients. Or you might be interested only in summary information, such as a count of the selected records. You don't have to count them yourself; the filing system can generate the summary for you.
Simultaneous multiple-user access to records. With paper records, if two people want to look up a record at the same time, the second person must wait for the first one to put the record back. MySQL gives you multiple-user capability so that both can access the record simultaneously.
Remote access to and electronic transmission of records. Paper records require you to be where the records are located, or for someone to make copies and send them to you. Electronic records open up the potential for remote access to the records or electronic transmission of them. If your dental group has associates in branch offices, those associates can access your records from their own locations. You don't need to send copies by courier. If someone who needs records doesn't have the same kind of database software you do but does have electronic mail, you can select the desired records and send their contents electronically.
If you've used database management systems before, you already know about the benefits just described, and you may be thinking about how to go beyond the usual "replace the filing cabinet" applications. The manner in which many organizations use a database in conjunction with a Web site is a good example. Suppose that your company has an inventory database that is used by the service desk staff when customers call to find out whether you have an item in stock and how much it costs. That's a relatively traditional use for a database. However, if your company puts up a Web site for customers to visit, you can provide an additional service: a search page that allows customers to determine item pricing and availability. This gives customers the information they want, and the way you provide it is by searching the inventory information stored in your database for the items in questionautomatically. The customer gets the information immediately, without being put on hold listening to annoying canned music or being limited by the hours your service desk is open. And for every customer who uses your Web site, that's one less phone call that needs to be handled by a person on the service desk payroll. (Perhaps the Web site can pay for itself this way?)
But you can put the database to even better use than that. Web-based inventory search requests can provide information not only to your customers, but to your company as well. The queries tell you what customers are looking for, and the query results tell you whether you're able to satisfy their requests. To the extent you don't have what they want, you're probably losing business. So it makes sense to record information about inventory searches: what customers were looking for, and whether you had it in stock. Then you can use this information to adjust your inventory and provide better service to your customers.
Another Web-based application for databases is to serve up banner advertisements in Web pages. I don't like them any better than you do, but the fact remains that they are a popular application for MySQL, which can be used to store advertisements and retrieve them for display by a Web server. In addition, MySQL can perform the kind of record-keeping often associated with this activity by tracking which ads have been served, how many times they've been displayed, which sites accessed them, and so forth.
So how does MySQL work? The best way to find out is to try it for yourself, and for that we'll need a database to work with.