CHAPTER NINE: MySQL for Server-Side Data Storage

Chapter opener image: © Godruma/ShutterStock

Overview and Objectives

From the dawn of the computer age, computers have been required to deal with large amounts of data. In the beginning, much of that data was in the form of results from numerical calculations that scientists at universities and other research institutions wanted to perform. But it was not long before companies large and small doing business of all kinds, governments, and other organizations wanted to get in on the action. And all of them had data, lots and lots of data.

In the early years, data was simply stored in files, but that was soon recognized as an inadequate solution to the problem of organizing data and keeping it organized. Indeed, this problem was recognized early in the game as one that would be of long-term and great significance, and much effort has been devoted to the topic over the last half-century.

A major problem that needed to be solved was the duplication of data, which made it a nightmare to keep all versions of the data up to date. Moreover, duplication required additional storage, at a time when storage was relatively expensive. But the real problem was that humans had to be intimately involved in the various processes needed to maintain the data. And humans make mistakes.

In this chapter we will once again give just a brief introduction to a topic of enduring interest to both researchers in computing science and end users of computers and the software that runs on them. That topic is the solution to many of those early problems that cropped up in the history of computing: the database. We begin by looking at the most widely used form of database, the relational database, and then move on to get some exposure to one of the major software packages for dealing with that kind of database, namely MySQL. This is a powerful, public-domain relational database management software system that can be used on a wide variety of computing platforms, and one that is freely available by download over the Internet. It is especially popular for website development using PHP. Databases like MySQL, as well as other (proprietary) systems from companies like Oracle and IBM, now play a major role in virtually all business applications that run on computers.

In particular, this chapter covers the following topics:

  • A brief discussion of the history and a high-level view of the relational database model, including tables and their rows (records or objects) and their columns (fields or attributes)

  • Some high-level goals to keep in mind when you are designing your own database

  • A brief discussion of some important aspects of the “preferred” structure of a relational database, including normalization, table keys, the possible relationships between tables, and the functional dependencies between record attributes

  • Making use of online resources to help you set up a suitable database for your needs

  • The phpMyAdmin interface to a MySQL database system, and setting up a MySQL database for our Nature’s Source website using this interface

  • Using the Structured Query Language (SQL) to manipulate a database in various ways

  • A brief look at the command-line interface of MySQL

  • Importing and exporting tables and databases to and from MySQL

9.1 Relational Databases

At the highest level, a database is simply a place where you store data. And again, at the highest level, the first rule of databases is to think about what should go into one, and what you expect to get out of it, before you start putting one together. Most people are familiar with “tables” of data, and the first impulse might be to put all of your data into one giant table. You should resist this urge if you have it, since you might wind up with a spreadsheet (which is not a database) containing all of your data, and if you do you will eventually have a great deal of trouble getting the kind of access you want to the information you have stored.

So, one of the primary design issues in creating a database is deciding what representation model to use when doing so. Such a model must provide a theory or specification describing how the database is to be structured and used.

Early databases used somewhat unstructured hierarchical and network models. In 1970, Edgar F. Codd from IBM introduced the relational database model, which revolutionized the database world. Some of the newer models, such as the object model, the object-relational model, and the more recent NoSQL models, may be advantageous in some cases, but the relational database remains the most widely used model. It is the one we will use in all of our discussions in this book.

If you permit us a few words of formal-speak, we can say that data in the relational model is represented as mathematical relations. The data is manipulated using relational calculus (or relational algebra). The mathematical basis for the relational database model makes it possible to minimize data redundancy and verify data integrity. Although there are many subtleties involved, and nonobvious aspects to all of this, it is not quite as scary as it sounds.

In fact, we can say, somewhat more informally but still with complete accuracy, that data in a relational database is stored in tables, and a table is just what you thought it was. Each row of the table, called a tuple in relational algebra, corresponds to a record or an object (such as an item to be sold, or a service to be rendered). Each field in the record is called an attribute in database terminology and represents a property of an entity stored in a record (such as the color of an item to be sold or the price of a service to be rendered).

It is in making the decisions as to what goes into the various tables, and what the relationships between those tables should be, that you must be careful if you are to have a good database design.

9.2 Database Design Goals

Virtually every business needs to store its data in some form, and in deciding what that form should be, you should keep certain goals in mind:

  • All data necessary for the smooth operation of the business needs to be recorded with whatever frequency the business deems appropriate.

  • Data integrity must be maintained as new information is added, information currently stored is updated, and (perhaps) some information is deleted.

  • The data must be stored in such a way that the business can easily retrieve whatever information it needs, in whatever form it is needed, and whenever it is required. This process is referred to as “querying the database”, or submitting “queries” to the database. You do this using a special language called SQL, which we discuss later in this chapter.

  • And finally, in the best of all worlds, the database would also occupy minimal storage, have lightning-fast response times to all queries, and be easily modifiable and extensible when the needs of the business change and grow.

An obvious question, of course, is this: What exactly are you trying to achieve by working toward these goals? One answer is that you are trying to have a database that avoids certain kinds of “anomalies” that tend to crop up with depressing regularity when you are working with a poorly designed database. These include:

  • The insertion anomaly, which occurs when you are entering a new record, but not all fields of that record can be filled in because some of the required data is missing.

  • The update anomaly, which occurs when information is updated in one place in the database, but the same information is not updated in some other database location. Elimination or minimization of redundant data in the database can help alleviate this problem, and also help to ensure more logical and efficient storage.

  • The deletion anomaly, which occurs when deletion of one piece of information from the database forces the deletion of something else that you perhaps would not want deleted.

Achieving all of the above goals is a tall order, seldom realized by any real-world business, but if you are careful with your design, even to the point of just applying some common sense (that rare commodity) during setup, you may be able to avoid some major headaches down the road.

9.3 Some Architectural Aspects of a “Good” Database

We are all familiar with tables of data, and if we have some data to organize, one “quick and dirty” first-impulse solution might be to put it all into one large table, as we have already mentioned. The trouble with this approach is that the same data winds up being stored in several different places (data redundancy), we soon lose track of what we have stored, and it becomes more and more difficult over time to add new data or update the data we already have.

9.3.1 Database Normalization

To deal with this problem, Codd did something more than simply invent the relational database. He also developed the concept of normalization for a relational database, a process that can help us to achieve at least some of the above goals.

A database is said to be in a particular normal form if the relationships between the attributes of the entities represented by the records in the tables of the database are rigorously defined in a certain way. Originally, in 1970, Codd specified three normal forms. These have the somewhat unimaginative, but easy-to-remember, names First Normal Form, Second Normal Form, and Third Normal Form, which are often abbreviated 1NF, 2NF, and 3NF.

Since Codd’s original proposal, several additional normal forms have been described, including the Boyce-Codd Normal Form (BCNF), as well as 4NF and 5NF, and a 6NF for “temporal” databases, for example. Clearly you can take the normalization process some distance, but going beyond the second or third normal form is often unnecessary, and may even, in some cases, be counterproductive. There are even “denormalization” procedures that can be applied if it is discovered that more normalization than is desirable has been performed, and some needs to be “undone”! In any case, we will have no need to discuss any of these “higher-level” normal forms. For further information see the end-of-chapter References.

One important aspect of these normal forms to remember is that they are cumulative in the sense that to be in 2NF, a database must first be in 1NF, and to be in 3NF it must first be in 2NF, and so on. Also, a database is said to be in 1NF if each table in the database is in 1NF, and similarly for the higher normal forms.

The first normal form essentially deals with the “shape” of a record type. For a database to be in 1NF, each row of a table must have the same number of columns of information (i.e., each record must have the same number of attributes) and each attribute value must contain a single piece of information. A short way of describing the latter requirement is to say that each attribute value must be atomic.

You need to keep in mind, however, that just what atomic means in any given database can depend significantly on the nature of the data and the viewpoint of the database designer. For example, in one database the name of a customer might have the customer’s first name, middle initial, and last name all together in one column and be regarded as being a single “atomic” value because there is no perceived need to have a name broken down into its constituent pieces. In another database, each of those three pieces of a name might be placed in its own column because there might well be a need for each part of the name to be accessible at one time or another, and having each name part in its own column makes that access much easier.

As a simple example, assume that we want to keep a list of products bought by a customer by using just two attributes (two fields per table row): Customer and Product. If a customer, say Pawan, buys two products, say Vitamin B and Vitamin C, the corresponding row of our table would look like the row shown in TABLE 9.1. The problem here is that the Product attribute value is not atomic (we cannot have a single record with Customer = Pawan and Product = Vitamin B, Vitamin C). Instead, we need two records with the same Customer value, but different Product values, as shown in TABLE 9.2. In other words, from the point of view of what’s in a table, each column must contain a single value (whatever that means within the given context) in each row.

TABLE 9.1 A simple illustration of 1NF violated.

Customer Product
Pawan Vitamin B, Vitamin C

TABLE 9.2 A simple illustration of 1NF satisfied.

Customer Product
Pawan Vitamin B
Pawan Vitamin C

Before discussing 2NF we need to say something about keys for our tables, and the functional dependency that may or may not exist between key and non-key record attributes in our tables.

9.3.2 Database Keys: Primary and Foreign, Natural and Surrogate, Simple and Composite

Placing appropriate keys in our database tables is an important step in setting up our database. A table column in which the attribute value is used to uniquely identify the record in its row, and serve, in effect, as a “lookup” value for that row, contains what we call the primary key for that table. Such a primary key must satisfy the following properties:

  • It must be unique for every record in the table, and here it is important to remember that the uniqueness must apply to every possible entry that might be put into the table, not just the entries in the table at some particular time.

  • It must always have a value and, moreover, a value that will never change.

A primary key column from Table A, say, may also appear in a second table, say Table B, and thus serve to “connect”, or establish a “relationship” between those two tables. In this case, in Table B the primary key from Table A is called a foreign key.

Sometimes we have a “natural” choice for a primary key, such as the social security number, which will be unique for each person in a table, for example. Other times we may simply want to generate an artificial (or surrogate) identifier to use for the primary key. For example, we could use something as simple as a sequence of positive integers. This has the dual advantage of being totally under the control of the database designer, and many database management systems can generate such values automatically as data is entered into the database.

A simple key value that appears in a single column is perhaps the most frequent and convenient kind of primary key, but sometimes the concatenation of the attribute values in two (or more) columns can be also used as the primary key, in which case we refer to it as a compound (or composite) primary key.

9.3.3 Functional Dependencies and 2NF

The second and third normal forms (2NF and 3NF) deal with the relationship (or the functional dependency) between key and non-key attributes in a table.

In particular, 2NF is violated when a non-key attribute is a fact about a subset of the primary key (i.e., there is a functional dependency from an attribute that is only part of the primary key to the non-key attribute). Thus this is only relevant when the primary key is composite (consisting of more than one attribute). Another way of saying this is that for 2NF we want each non-key attribute in a table to be information “about the primary key, about the whole primary key, and about nothing but the whole primary key”.

Let us illustrate what we mean by taking a simple example of a database that violates 2NF and converting it into one that satisfies 2NF.

Compare what you see in TABLE 9.3 with what you see in TABLE 9.4. Table 9.3 shows a single-table database with four attributes for purchases made in a store: Customer, email, Product, and Price. Suppose we are using the combination of the customer name attribute (the Customer field) and the product name attribute (the Product field) as the primary key. There are four records in the database. Clearly there is a functional relationship between the attribute Customer, which is part of the primary key, and the non-key attribute email, since a customer’s email address provides a fact about that customer. However, the Customer attribute is only part of the primary key and the customer’s email has nothing to do with any product the customer may be buying (the other part of the primary key).

TABLE 9.3 A simple illustration of 2NF violated.

Customer Email Product Price
Pawan Vitamin B $19.99
Pawan Vitamin C $24.99
Paul Vitamin C $24.99
Robert Vitamin B $19.99

TABLE 9.4 A simple illustration of 2NF satisfied.

Customer Email Customer Product Product Price
Pawan Pawan Vitamin B Vitamin B $19.99
Paul Pawan Vitamin C Vitamin C $24.99
Robert Paul Vitamin C
Robert Vitamin B

A similar functional dependency also applies from the attribute Product, which is part of the primary key, to the non-key attribute Price.

Now suppose we split the single table of the original database of Table 9.3 into three tables, as shown in Table 9.4. The first thing we achieve by doing this is that we save some storage. This may not be obvious in this example, because we have so few records, but note, for instance, that in the single-table database of Table 9.3 the product Vitamin C and its price appeared twice, but in the new arrangement this pairing occurs only once, so think about the implications of this for a much larger data set.

We are also moved toward our goal of maintaining database integrity. For example, if we had to change the price of Vitamin C to $29.99, then in the configuration of Table 9.4 we would need to make the change for only one record (the one in the table containing the record with the product Vitamin C and its price). That means we could not create an “update anomaly” by making the mistake of changing the price of Vitamin C in one place and not in the other (Vitamin C and its price appeared twice in the original table).

Note that these three tables can be “joined” together to recover the original table, and hence the decomposition shown in Table 9.4 is lossless. You will see how two or more tables can be joined in various ways using MySQL queries later in this chapter.

Though we will not pursue the discussion any further, let us say here simply that 3NF is violated when a non-key attribute provides a fact about another non-key attribute. The creation of further tables from the existing ones may be necessary to achieve 3NF.

9.3.4 Table Relationships in a Database

Another important aspect of a database design to think about during setup is the nature of the relationships that the tables in the database will have to one another, as the above discussion suggests. There are essentially three possibilities:

  • one-to-one: There is a one-to-one relationship between Table A and Table B if each record in Table A corresponds to one and only one record in Table B, and vice versa. For example, in your company there might be a one-to-one relationship between each employee and his or her desktop computer, if in fact each employee has a single desktop computer.

  • one-to-many: A one-to-many relationship from Table A to Table B means that each record in Table A will correspond (potentially at least) to several and perhaps many records in Table B, and several records in Table B can correspond to a single record in Table A. For example, a single customer may have placed several orders, so a perfectly “natural” relationship between a Customer table and an Order table might be one-to-many.

  • many-to-many: A many-to-many relationship from Table A to Table B means that at least one record in Table A corresponds to several (or many) records in Table B and at least one record in Table B corresponds to several (or many) records in Table A. Without going into detail, we should say that such relationships often create redundant data in your database and lead to problems in other ways, and so are best avoided.

In all cases, the relationships between tables are established with the help of table keys, the primary key of one table becoming a foreign key in a second table, for example. Sometimes it may prove to be convenient to set up a table containing nothing but keys, just to help “connect” other tables, particularly when you are trying to simplify a many-to-many table relationship.

9.3.5 Some General Advice

So, the first step in creating a database application for a business, or for any other endeavor, is to create a data model that contains all relevant attributes that need to be stored. The next step is to use the functional dependencies among the attributes to create an appropriate set of tables, and then establish the relationships between these tables using keys. The resulting set of tables should minimize data redundancy and preserve data integrity as various operations are performed on the database.

As a best practice, you should ensure that the resulting database model is at least in 2NF. But, as we have pointed out, 2NF is not a concern unless you are using a composite primary key. A normalized database tends to make general-purpose querying easier and faster, while a non-normalized database can favor some queries over others. However, keep in mind that normal forms represent only guidelines for the design of the records in the tables of your database. Though generally a good thing, they are just that—guidelines and not rules—and sometimes business requirements become the overriding factor in how a database is actually constructed. Nevertheless, some attempt at normalization is always a good starting point.

9.4 Make Use of Online Resources and Don’t Reinvent the Wheel

The previous discussion was intended to give you a brief glimpse into some of the considerations that go into the setting up of a suitable database for any enterprise, but it did not provide enough detail to use as a guide for serious, hands-on database development. In fact, as a practical matter, many entrepreneurs simply do not have the time, energy, or expertise to make use of the theoretical underpinnings of database design when it comes to the actual real world task of doing so. Fortunately, help can be found online, and unless you have very special needs for your particular situation it may be a good idea to go looking for a database design that will serve your purposes. For example, the website Database Answers at http://databaseanswers.org/ describes data models for a wide variety of applications, and you may want to browse this website for a data model that might be suitable for your situation.

FIGURE 9.1 graphics/ch09/dbSchema.jpg

Data Model for an e-commerce website based on models from DataBaseAnswers.org.

For our own e-commerce development, we have chosen a design that uses a combination of two of their data models, and we are actually using a subset of the combined data model. Furthermore, we have added and deleted attributes to suit our needs. FIGURE 9.1 shows the data model used in our database. The figure is drawn using the designer utility provided by phpMyAdmin, one of the GUI (Graphical User Interface) “front ends” for the MySQL database management software.

9.5 The Data Model for Our Nature’s Source Database

Let us take a few moments to examine our data model, which is illustrated in Figure 9.1. The figure shows that our database will consist of a total of 12 tables, with each box in the figure representing one of those tables. The rows in each box of the figure represent, in turn, the attributes of a record in the corresponding table. A common naming convention in the database world is to use multiple words to name an attribute, with the words joined by an underscore character (_), as in customer_id, for example.

On the left-hand side of each attribute you will see an icon that suggests the role and properties of that attribute. For example, an icon that looks like a key indicates that the corresponding attribute is a primary key for the table in which it appears. Recall that the primary key of a table uniquely identifies each record in that table, and it can be either of the following:

  • A single attribute that is guaranteed to be unique. For example, customer_id in the Customers table is unique in the sense that there is no more than one record in the table corresponding to any particular value of customer_id. It is possible to generate these unique values automatically when inserting records into the table.

  • A concatenation of multiple attributes. For example, in the Shipment_Items table the shipment_id and the order_item_id attributes are combined to form the primary key.

Other icons that we see on the left of an attribute include these:

  • A # sign, indicating a type whose values are numeric

  • A text page icon, indicating a type whose values are character strings

  • A date icon, indicating a type whose values represent dates

These icons are actually redundant, because on the right of each attribute name, after the colon, we have text that describes the type of the attribute.

MySQL supports a wide range of data types. Like other keywords in MySQL, their names are case-insensitive. In Figure 9.1 we show them in lowercase, but in the text discussion we show them in uppercase to help them stand out. So, here are the ones we use in our data model shown in Figure 9.1:

  • INTEGER (or its synonym INT, which can also be used), a numeric data type capable of storing 32-bit integer values

  • DOUBLE, a numeric data type capable of storing 64-bit floating point values (values containing a decimal point, i.e., “real numbers”)

  • VARCHAR(M), an efficient data type for storing variable-length strings (At the time of this writing, the maximum size M of such a string is 65,535 characters, but the effective maximum length is also subject to the maximum row size—also 65,535 characters, and shared among all columns—and the character set in use.)

  • TINYTEXT, another data type for storing strings of length not exceeding 255 characters

  • TEXT, for storing large amounts of text (up to 65,535 characters)

  • DATE, for storing date values

See the end-of-chapter References section for links to further information on these and other data types available in MySQL.

Let’s dig a little deeper into the structure of our database by discussing some of the table relationships. Each table is connected to another table through a common attribute. Usually the common attribute is the primary key in one of the two connected tables, and that same key is therefore a foreign key in the other table. It may be a good idea to make the foreign keys indexed in a table. Indexing an attribute allows for faster searching based on that attribute.

Figure 9.1 shows the connections from the primary key to the foreign keys. Lines with a small, filled-in semicircle at one end and a larger, filled-in semicircle at the other make these connections. The end with the smaller semicircle points to the primary key, while the end with the larger semicircle indicates the foreign key.

We have three main tables:

  1. Customers This table contains all the relevant information for each customer. Note that it is linked to the Orders table through its customer_id primary key attribute. For every order, there will be a customer_id foreign key in the Orders table identifying the customer in the Customers table who placed that order. On the other hand, a particular customer may typically have placed multiple orders, so there is a one-to-many relationship from the Customers table to the Orders table.

  2. Products This table contains all the relevant information for each product. Note that it is connected to the Ref_Product_Categories table, which provides information about the category of each product. Note that we are using the convention from the Database Answers website, according to which tables that start with the prefix Ref_ are essentially for reference. There is no physical object corresponding to one of the rows in such a table. Examples of product categories listed in Ref_Product_Categories include exercise equipment, stomach remedies, vitamins, and so on. Each category will contain many products that are listed in the Products table, so again we have a one-to-many relationship, this time from a product category to the products in that category.

  3. Orders This table contains some basic information about each order but is also connected to a number of other tables that provide additional information about each order. In particular, through its order_id primary key, which appears as a foreign key in those other tables, the Orders table is connected to each of the following tables:

    • Shipments This table keeps track of shipments. It is in turn connected to the Shipment_Items table to follow the details of the shipment of individual items.

    • Order_Items This table contains information relating to the individual items in a particular order. The attributes for an “order item” include the order_item_quantity and the order_item_price for that item.

    • Invoices This table contains invoice information and is connected to a number of other relevant tables, such as the Payments table.

Study Figure 9.1 until you have a reasonable sense of our data model. Once you have some understanding of the structure we are going to use for our database, we can proceed to implement that database using MySQL, and that is our next task.

9.6 MySQL, phpMyAdmin, and SQL

Our choice of MySQL is based on the fact that it is the most popular open source relational database management software for web programming. TABLE 9.5 gives a brief summary of its history. We will begin by using the phpMyAdmin GUI for MySQL, but we will always provide corresponding commands in SQL that we would enter directly if we were using the command-line interface to MySQL. In fact, we shall also discuss that command-line interface and how to enter these commands later in the chapter.

And by the way, these SQL commands tend to be the same for all the major database management systems. Using these commands is how you “talk to” a database system. There are various ways of doing this: using phpMyAdmin or the command-line interface, as we do in this chapter, or using an API (Application Programming Interface) provided by some programming languages such as Java or (in our case) PHP. We will make use of the PHP approach in the following chapter.

SQL commands are very often called queries, whether or not they are actually asking a database for information, and we will use the terms interchangeably. So, a “query” might also be creating a table, putting some information into a table, or updating that information as well.

At this point we assume that you have the necessary access to both MySQL itself and the phpMyAdmin interface to MySQL. You may wish to install both of these software packages on your own computer, and there are some links to information that will prove helpful in doing so in the end-of-chapter References section.

TABLE 9.5 A brief history of MySQL.

Date Version Notes
1994 Original development by Michael Widenius and David Axmark
May, 1995 First internal release
1996 3.19 First numbered release, according to Wikipedia
January 1998 Released for Windows
January 2001 3.23 Last 3.xx production release
March 2003 4.0 First 4.xx production release
October 2005 5.0 First 5.xx production release
2008 Sun Microsystems acquires MySQL
2010 Oracle acquires Sun Microsystems, causing Widenius to fork MySQL and launch MariaDB, perhaps fearing what Oracle would do with MySQL
October 2015 5.7 MySQL still alive and well . . .

More realistically, particularly if you are working in an academic environment, your instructor may have arranged for your system administrator to create an account and an empty database for you. Then, once you log in to the system and choose that database, you will be ready for the next step of creating and “populating” the database tables based on the design from Figure 9.1, or based on a design of your own.

If you are working with some other database system, much of what we say will still be meaningful and useful, but the screenshots shown in the text will not be as relevant. Although the vast majority of screenshots and figures in this second edition of the text have been updated, our Nature’s Source database has not changed, so the screenshots, figures, and tables in this chapter remain the same. This means that you will occasionally see references to webbook, the name we used for our account and database in the first edition. For this second edition this name became webbook2e, but the Nature’s Source database content for our Nature’s Source sample website and for the text has not changed.

9.7 Using phpMyAdmin and SQL to Set Up the MySQL Database for Our Nature’s Source Website

The phpMyAdmin software is a web-based tool, so to use it for accessing your MySQL installation you will need a URL for phpMyAdmin on the server on which your MySQL and phpMyAdmin have been installed, and on which you have a MySQL account. For example, in our case that URL is

http://cs.smu.ca/phpmyadmin

and we simply browse to that web address and respond to the login window we get by entering our MySQL username and password. You would do the same, and you would then get the local “home page” of your phpMyAdmin. At that point you would have whatever access your system administrator has established for you. We assume you have a local copy of our text website and have progressed to this point, if you wish to follow along with the discussion that comes next in the text.

We also assume that you are starting with an empty database (here called webbook in what follows). Once again, that is a likely scenario in an academic environment, though if you don’t have such a database but do have permission to create one, it is easy enough to do so, as we shall see shortly.

So, we want to start creating and populating tables within our webbook database, based on our design from Figure 9.1, and using phpMyAdmin. FIGURE 9.2 shows a typical dialog box that appears by default once you have logged in to phpMyAdmin, have chosen the database that you are building, and issued a request to create a table. We have filled in the necessary information to create a table called Customers, in which each row will have 13 fields. That is, each customer will have 13 attributes. The observant reader may notice that we actually have 14 attributes in the Customers table of our data model. An error like this during database construction is not uncommon, and we will see a bit later how we can fix such a mistake after the table has been created.

FIGURE 9.2 graphics/ch09/displayCreateTableCustomers.jpg

Specifying the name and size of the Customers table using phpMyAdmin.

Once you click on the Go button shown in Figure 9.2, you will get a screen like the one shown in FIGURE 9.3. Here we enter the names of the customer attributes and their types, according to our design shown in Figure 9.1. Then, clicking on the Save button causes the table to be created, as shown in FIGURE 9.4. This figure shows the complete structure of the table. Also, in the Indexes: part of the figure (at the bottom) we see a list of our primary and unique keys, as well as other indexed attributes. Finally, the figure also shows (partially) the actual SQL statement that was used to create the table.

If you are using another database management software system, the same query should still be able to create this table for you. We have reproduced the query separately in FIGURE 9.5.

SQL commands, or queries, can be grouped into various categories. Below we provide a list of the major groupings, together with an indication of the category to which each command of interest to us belongs. In the text we introduce and discuss each command as and when we need it. There are, of course, many additional MySQL commands that we do not discuss, and many options for using the commands that we do discuss for the simple reason that we do not have the time or space and have no immediate need for them. See the end-of-chapter References section for links to the MySQL documentation. Here are the categories:

  • Commands for data definition (CREATE, ALTER, DROP) that are most frequently used to create, modify, and delete database tables

  • A command for data retrieval (SELECT) used to query the database and retrieve useful information stored in it (probably the most-used command)

  • Commands for data manipulation (INSERT, LOAD, UPDATE, DELETE, TRUNCATE) that are used to enter data into tables, modify data that is already in those tables, and to remove data from tables

  • Commands for data transaction, and commands for data control, neither of which we will need to discuss

9.7.1 The CREATE Command

The CREATE command is most often used to create a table within a database, but it can also be used to create the database itself. For example, the command

CREATE DATABASE webbook;

FIGURE 9.3 graphics/ch09/displaySpecifyAttributeCustomers.jpg

Specifying the attributes for the Customers table using phpMyAdmin.

could be used to create our webbook database, provided we have the necessary MySQL permissions to do so. These permissions are under the control of the MySQL administrator. That administrator will be you if you have installed MySQL on your own machine. However, many users in an academic environment may not have the authority to create their own databases. If this is the case for you, your system administrator would probably have created a database for you, based on information provided by a course instructor.

FIGURE 9.4 graphics/ch09/displayResultCreateTableCustomers.jpg

Result of creating the (empty) Customers table using phpMyAdmin.

As for creating a table in an already existing database, the general syntax of the required command looks like this:

CREATE TABLE table_name (col_name col_type col_constraints[,...]);

Figure 9.5 shows the CREATE command used to create our Customers table. This command, as shown, could be typed in directly if we were using the command-line interface to MySQL, a topic for later discussion in this chapter. For the moment, we are happy to have phpMyAdmin “construct” this command for us “behind the scenes”, based on information we enter into the form it displays for us. This particular command illustrates almost all of the useful features of the CREATE command. In the first line, 'webbook'.'Customers' tells that we are creating a table called Customers in the database webbook. The use of single quotes is not always necessary, but phpMyAdmin has put them in for us. Within the parentheses we specify the list of attributes, along with their types, and their lengths and other constraints wherever appropriate (such as NOT NULL, which means that the attribute must have a value).

FIGURE 9.5 graphics/ch09/createCustomers.sql

The complete SQL command for creating our Customers table.

At the end of the list of attributes, we specify that customer_id is the primary key, that the table should be indexed based on phone_number, and that the attribute values of email_ address and login_name should be unique. Outside the parentheses, the “storage engine” is specified to be MYISAM, a specification that was not really necessary since MYISAM was the default storage engine at the time of this writing, though this may have changed by the time you read this and is likely to be irrelevant for our purposes in any case.

Now that we have created the Customers table using the CREATE command, let us move on to our next data definition command of interest, ALTER, and see how to make changes in the structure of our table.

9.7.2 The ALTER Command

The ALTER command allows us to modify an existing object in our database. For example, we can use it to add a column to, or delete a column from, an existing table. This is different from, and should not be confused with, the UPDATE command, which is used to change the values of attributes in the rows of our tables, and which we discuss later.

FIGURE 9.6 graphics/ch09/displayChangeAttributeProperty.jpg

Altering the Customers table using phpMyAdmin.

Recall that we had made a mistake in creating the Customers table by omitting the gender attribute. We will now add this attribute. Let us say that we realized our error as soon as we looked at the results returned by the CREATE command in Figure 9.4. Notice at the bottom of the figure that we have the option of adding an attribute. We can indicate that we want to add an attribute after customer_last_name and click on the Go button.

FIGURE 9.6 shows the page that will come up for specifying the properties of the new attribute. We indicate that the name of the attribute is gender, and it will be of type VARCHAR with a length of 1, and then click the Save button.

FIGURE 9.7 shows the structure of the altered Customers table, as well as the SQL ALTER command that was used to add the gender attribute. This command is also shown, more clearly, in FIGURE 9.8. ALTER is a very versatile command, which allows us to add, change, or delete attributes (as opposed to values of attributes, done with UPDATE, as we mentioned above). In our case, we have simply used the ADD “subcommand” to add gender after customer_last_name.

FIGURE 9.7 graphics/ch09/displayResultChangeAttributeProperty.jpg

Result of altering the Customers table using phpMyAdmin.

FIGURE 9.8 graphics/ch09/changeAttributeProperty.sql

The SQL command for altering our Customers table by adding a new attribute.

9.7.3 The DROP Command

The last SQL data definition command we want to look at is the DROP command. The DROP command can be used to remove an attribute from a table, a table from your database, or even to remove the complete database. Caution should be exercised when using this command, because the deletion is irreversible and the “dropped” data is therefore irretrievable. A GUI interface to MySQL, like phpMyAdmin, will generally warn you and ask you to confirm deletions, but remember later on when you are using the command-line interface to MySQL that you will not get any such warnings, the idea being that if you are at the command-line you know what you are doing, and you are doing it carefully.

We will study this command with the help of a table called temp. We have created such a table in our database with two attributes called dummy1 and dummy2, as shown in FIGURE 9.9. If you click on the red X icon in the row containing the dummy1 attribute, a dialog box will appear. This is also shown in Figure 9.9. The corresponding SQL query uses this ALTER command with a DROP subcommand:

ALTER TABLE 'temp' DROP 'dummy1';

As soon as you click on OK, the attribute dummy1 will disappear from the table. If we want to delete the entire table temp from the database, we can click on the database and then click on the red X icon in the row corresponding to that table, as shown in FIGURE 9.10. The required SQL query is:

DROP TABLE 'temp';

Clicking on the OK button will permanently delete the table temp from our database.

FIGURE 9.9 graphics/ch09/displayDropDummy1.jpg

Dropping an attribute from a table.

FIGURE 9.10 graphics/ch09/displayDropTemp.jpg

Dropping a table from a database.

9.7.4 The INSERT Command

Now that we have dealt with the SQL data definition commands that are of interest to us, we turn our attention to data manipulation commands, beginning with the INSERT command. Suppose we want to add two records to the table Ref_Invoice_Status. Clicking on that table in the database, and then clicking on the Insert link will bring up the web page shown in FIGURE 9.11. We then enter the values for the two records and click the Go button at the bottom right. The resulting web page, seen in FIGURE 9.12, shows the insertion as well as the corresponding SQL command. The SQL INSERT command is also reproduced in FIGURE 9.13.

Here is the general format of the INSERT command:

INSERT INTO table_name
(comma-separated list of column names)
VALUES
(comma-separated list of corresponding values for those columns)

In our command shown in Figure 9.13 we are indicating we want to enter the values of attributes invoice_status_code and invoice_status_description. The first record has the value 'IS' for invoice_status_code and 'Issued' for invoice_status_description. The second record has the value 'PD' for invoice_status_code and 'Paid' for invoice_ status_description.

FIGURE 9.11 graphics/ch09/displayInsertRecords.jpg

Inserting records into the Ref_Address_Types table using phpMyAdmin.

The INSERT command is quite useful for loading a small number of records into our database. But what if we want to insert a large number of records from a file? In this case we can run the INSERT command for each record automatically using SQL programming. This can also be achieved using a GUI interface such as that provided by phpMyAdmin, a process we now describe.

In the ch09 subdirectory of the text website (or text files) you will find a data file called products870.csv, which contains 870 product records. Note that these are not real products; they are generated by changing the names of some of the products that one usually finds in a health products store. A typical store would carry many more products than this. The first 20 records from the file are shown (partially) in FIGURE 9.14, with the lines truncated so the display will fit on the page. The value for each attribute is enclosed in a pair of single quotes and values are separated by commas. That is why we use the (conventional) file extension.csv, which stands for “comma-separated values”.

We can import these records into our Products table using phpMyAdmin. If we click on the Products table, we will see a link called Import. Clicking on the Import link brings up a web page similar to the one shown in FIGURE 9.15, where we have entered the name of our data file and clicked on the CSV radio button. We have also indicated that the fields are enclosed in single quotes and separated by commas. Finally, we use the default value for the escape character. This means that any occurrences of tab, newline, or that are preceded by should be treated as literal characters. For example, \ will be treated as a single . Clicking on the Go button will add all 870 records to the table, using 870 INSERT queries, as confirmed by the message shown in FIGURE 9.16.

FIGURE 9.12 graphics/ch09/displayResultInsertRecords.jpg

Result of inserting records into the Ref_Address_Types table using phpMyAdmin.

FIGURE 9.13 graphics/ch09/insertRecords.sql

The SQL command for inserting two records into our Ref_Invoice_Status table.

FIGURE 9.14 graphics/ch09/products870Truncated.csv

Partial view of a file of comma-separated values containing product records.

FIGURE 9.15 graphics/ch09/displayInsertManyRecords.jpg

Inserting multiple records in the Products table using phpMyAdmin.

FIGURE 9.16 graphics/ch09/displayResultInsertManyRecords.jpg

Result of inserting multiple records in the Products table using phpMyAdmin.

9.7.5 The LOAD Command

There is a more efficient way to import a large number of records, by using the LOAD command. Let us explore this option with the help of the same Import interface, but this time we will use the Customers table. The data file we will import contains 10,000 customers, is called customers10000.csv, and a copy is also provided in the ch09 subdirectory of the text files. As with our products data file, a truncated version is shown in FIGURE 9.17.

FIGURE 9.17 graphics/ch09/customers10000Truncated.csv

Partial view of a file of comma-separated values containing customer records.

This time we choose the CSV using the LOAD DATA radio button, but the rest of the selections are the same as before. Clicking on the Go button in FIGURE 9.18 will add all 10,000 records using a single query based on the LOAD command in SQL, as shown in FIGURE 9.19. Caution should be exercised when loading large files through the phpMyAdmin interface. There may be a limit of 2MB. Even if your file is smaller than 2MB, the LOAD command may not be successful. We recommend that you upload the file through other means to the server that hosts your MySQL server. Then execute the SQL query preferably through the command-line interface that we will look at shortly.

FIGURE 9.18 graphics/ch09/displayLoadFile.jpg

Loading multiple customer records from a CSV file into the Customers table using phpMyAdmin.

In the meantime, look at the SQL query shown in FIGURE 9.20, which uses the SQL LOAD command. Here is the abbreviated (and generic) format of the LOAD command that we used for our example:

LOAD DATA LOCAL INFILE 'file_name'
    INTO TABLE tbl_name
    FIELDS TERMINATED BY 'string'
    ENCLOSED BY 'char'
    ESCAPED BY 'char'
    LINES TERMINATED BY 'string';

FIGURE 9.19 graphics/ch09/displayResultLoadFile.jpg

Result of loading multiple customer records from a CSV file into the Customers table using phpMyAdmin.

FIGURE 9.20 graphics/ch09/loadFile.sql

The SQL command for loading multiple customer records from a CSV file into the Customers table.

Here we see only the options that we actually used in loading our customer records. See the MySQL manual for details about many other options.

First, the keyword LOCAL means that the file needs to be copied to the server that hosts MySQL. That is why we have a funny filename in the command shown in Figure 9.20. Our file customers10000.csv was copied to a temporary file on the server, and the filename used in the command is that of the temporary file. We indicate that the fields are enclosed in single quotes and separated by commas. We use the default value for the escape character. As before, that means all occurrences of tab, newline, or that are preceded by should be treated as literal characters.

9.7.6 The UPDATE Command

We have now seen two commands (INSERT and LOAD) for getting data into our database. But what if we inadvertently entered some wrong data, or some piece of data already in the database has to be updated? Clearly we also need an UPDATE command and, of course, there is one. Here is its general syntax:

UPDATE 'table_name'
    SET column1_name=expression [column2_name=expression2,...]
    [WHERE where_expression]
    [LIMIT n]

The command works as follows. The columns in the table that are modified are those specified in the WHERE clause, and each of those rows has each column named in the SET clause set to the value of the corresponding expression. If there is no WHERE clause, be careful to note that all rows of the table are modified. If the LIMIT clause is given, the value n specifies the maximum number of rows to be modified.

See the end-of-chapter Short Exercises section for an exercise giving you a chance to get familiar with this command.

9.7.7 A First Look at the SELECT Command

We now know how to “populate” our database using two data manipulation commands, INSERT and LOAD. Two other commands, DELETE and TRUNCATE, will allow us to “de-populate” our database. Before looking at these two destructive commands, we will take a brief look at the most important data retrieval command, SELECT. It will be discussed in more detail later in the chapter when we discuss the command-line interface. However, in order to delete records, we will have to browse our tables and find what to delete. So a simple introduction to data retrieval is in order.

The records we have in our database allow us to experiment with the SELECT command. We can simply click on the Browse link when we are in the Customers table, and we will get (by default) the first 30 records in the table, as shown in FIGURE 9.21. The corresponding SQL query is shown in FIGURE 9.22 and you can see that these records have been retrieved from the database using a SELECT command.

In Figure 9.22 we show the use of the SELECT command in its simplest form to select the complete record by specifying '*' after the keyword SELECT. The word Customers after FROM indicates the table, and the range of 0 to 30 records appears after the keyword LIMIT. We will be experimenting with more sophisticated SELECT statements later on in this chapter, but for the moment we just look at how to delete a few, or all, of the records from a database.

FIGURE 9.21 graphics/ch09/displaySelectAll.jpg

Selecting records from the Customers table using phpMyAdmin.

FIGURE 9.22 graphics/ch09/selectAll.sql

Selecting records from the Customers table using SQL.

9.7.8 The DELETE Command

The Customers table shown in Figure 9.21 has one of the records highlighted with a different color, because we are hovering our mouse over that record. We are going to pick the record for deletion by clicking on the red X icon that appears just before the beginning of the record. FIGURE 9.23 shows the window that will pop up. It also shows the corresponding SQL query. The query uses the DELETE command. Once again, there are more options than we show, but the typical (generic) syntax for deleting from a single table looks like this:

DELETE FROM table_name
    [WHERE where_condition]
    [ORDER BY...]
    [LIMIT row_count]

FIGURE 9.23 graphics/ch09/displayDeleteRecord.jpg

Deleting a record from the Customers table using phpMyAdmin.

The WHERE clause allows you to specify a condition that a record must satisfy for it to be deleted. In our case, we specified that the customer_id must match the specified value. The ORDER BY option allows us to specify the attributes that should be used to order the records for deletion. The ordering is especially relevant if we were going to limit the number of records that should be deleted using the option LIMIT. In our example query, we are limiting the number of deletions to a single record. This is redundant, since customer_id is unique, and there will be only one record that will match the WHERE clause in any case.

9.7.9 The TRUNCATE Command

Since we have started deleting records, why stop at one record? Let us delete all the records and then rebuild our data—we now have the power. The command TRUNCATE TABLE allows us to delete all the records in a table. We can invoke it from phpMyAdmin by clicking on the database and then clicking on the red X next to the table name. A warning window including the SQL query will pop up as shown in FIGURE 9.24. Click the OK button and all the data in the table will be deleted. Most database engines implement the TRUNCATE TABLE command by dropping the table and recreating an empty table. It is much faster than deleting individual records when the tables are large. Should we spare you the obvious warning about exercising caution while using DELETE and TRUNCATE TABLE commands?

FIGURE 9.24 graphics/ch09/displayTruncateTable.jpg

Truncating the Customers table using phpMyAdmin.

9.7.10 Inventory Management Systems

Most retail businesses will have some kind of inventory management system that will be part of the “back office” for the use of store management. It will handle the wholesale purchase of items from suppliers and maintain the data necessary to keep track of inventory.

Systems like this are not unique to e-commerce businesses. Our system can work with such an inventory management system simply by interacting with the product-related tables. We have chosen to limit the scope of our system, since the inclusion of many additional tables would not help us learn any new features of web programming. However, for the business you are working on in the “parallel project”, you may find it convenient to add a number of additional tables to deal with the specific activities in which your business is involved. Be thinking about the possibilities as you work on the next iterations of the parallel project at the end of this chapter and the next.

In the absence of a complete inventory management system, we have filled as many tables as necessary for experimenting with the database. Readers are encouraged to browse through the populated tables from the book’s website.

Later on in this chapter we will also describe how you can import our entire database into your own MySQL system so that you can experiment with it. In the next chapter, we will also see how to populate other tables using simulated e-commerce, when we process online “purchases” of our products by our customers.

However, our limited discussion of database management is not yet complete. We need to look at the command-line interface to MySQL. The command-line interface is generally less user-friendly than phpMyAdmin, but much faster to use for those who know what they are doing, especially when queries are more complicated. It is also less forgiving than the GUI counterpart, and is therefore meant for more knowledgeable users. To become comfortable with database management, it is necessary for all web programmers to learn how SQL queries are executed via the command-line interface, since when such queries are sent through a programming language API the commands have essentially the same form as when they are entered directly by typing them in at a command-line prompt.

9.8 MySQL’s Command-Line Interface

We will use the Linux command line to gain access to our MySQL command-line interface. You can also log in to MySQL from other operating systems such as Windows or the Mac OS. Once you have logged in to MySQL from any operating system, you use essentially the same interface.

9.8.1 A First Session with the Command-Line Interface

FIGURE 9.25 shows our first command-line interaction with MySQL. In line 1 we use the Linux command1

mysql –u webbook –p

to log in to our MySQL system. Note that the command to access the MySQL system under Linux is an all-lowercase mysql. The name following the –u option is the username by which MySQL identifies the user. The –p option indicates that the username requires a password, which will be entered in response to the prompt from MySQL on line 2.

As is always the case in these situations, when the password is typed, it does not show up on the screen. Once the password is verified and accepted, the prompt

mysql>

appears, and we are then ready to enter our commands at the command-line interface.

The first thing we have to do is tell MySQL which database we would like to use. That is the purpose of the USE command in line 9:

USE webbook;

We continue to capitalize MySQL commands, for emphasis, in our discussions of them, but we have saved time when entering them at the command line by using lowercase. Note that most commands in MySQL have to be terminated with a semicolon (;). Some commands do not actually require the semicolon, but having a semicolon at the end of those commands does not cause an error, so it is much safer, and a recommended “best practice”, simply to put a semicolon at the end of every command.

With the command

SHOW tables;

in line 13 we can find out what tables are currently in the database we have said we want to use. In this case, we see the 12 tables of our own Nature’s Source database that we have been discussing.

FIGURE 9.25 graphics/ch09/mysqlSession1.txt

A first look at the command-line interface for MySQL.

Next, we execute a simple SELECT command (line 31) to retrieve all the records from the table Ref_Invoice_Status:

SELECT * FROM Ref_Invoice_Status;

There are only two such records, as the display shows (lines 32–38). The use of * in this context means that we want to see all the fields from each record. This SELECT command is similar to the one we saw earlier. Note the format of the output for each of the queries we have seen here, which is typical.

In fact, all the SQL commands we saw in the previous section can be executed using the command-line interface, but in some cases—such as creating a table or adding one or two records—it may be easier to use the phpMyAdmin GUI.

9.8.2 A Closer Look at the SELECT Command

Unfortunately, the phpMyAdmin GUI may be of limited use when we are specifying more complex data retrieval queries using SELECT. Let’s now investigate some more sophisticated retrievals using the command-line interface. In a SELECT query, the user describes only the desired result set. The following are keyword modifiers commonly used with a SELECT command:

  • FROM, which is followed by a comma-separated list of the names of the tables from which the data is to be taken

  • WHERE, which is followed by a comma-separated list of the conditions that specify which rows are of interest for the retrieval

  • GROUP BY, which is followed by information indicating how the data in rows with related values is to be combined

  • ORDER BY, which is used to identify which columns are used to sort the retrieved data

  • LIMIT, which specifies a range of records for which the data is to be retrieved

We will explore these keywords with the help of some queries. Note that in the text discussion we continue to use all uppercase for the names of MySQL commands and their modifiers to distinguish them from the (user-chosen) names of other entities. When typing them into the command-line interface, however, it is easier to take advantage of the case-insensitivity of MySQL and use all lowercase. Keep in mind, however, that the user-chosen names (for tables and attributes, for example) will be case-sensitive, at least on Linux and other Unix-based systems.

Calling Built-In MySQL Functions and Performing Simple Arithmetic During Data Retrieval

MySQL has some built-in functions that can be very useful in data retrieval. Let us begin by illustrating COUNT( ), a function that retrieves just the total number of records, instead of the records themselves. So in FIGURE 9.26 our first query (line 1) is

SELECT COUNT(*) from Customers;

FIGURE 9.26 graphics/ch09/countSQL.txt

Use of the COUNT( ) function in SQL.

and the result shows the total number of records in the Customers table. You may recall that phpMyAdmin generated queries that used single quotes around table and attribute names. We are not using single quotes around the table names here, since they are unnecessary if the names do not contain any spaces. But if we had a table with a name like Our Customers, for example, we would have to enclose it in single quotes. However, best practice would dictate that we avoid such names.2

The second query in Figure 9.26 is

SELECT country, COUNT(*) FROM Customers GROUP BY country;

and uses the GROUP BY option (line 9), where the records are grouped by the values in the specified list of attributes. We are grouping by the country, so there will be a separate record for each country. That is why we get two records for two countries in our table, one for Canada and the other for the USA (we only have customers in those two countries).

Another useful MySQL function is SUM( ). FIGURE 9.27 shows its use with the following query from line 1:

SELECT SUM(product_inventory) FROM Products;

This query sums up the values of the attribute product_inventory for every record in the Products table. So we see that there are 43,097 items in our inventory (line 5).

FIGURE 9.27 graphics/ch09/sumSQL.txt

Use of the SUM( ) function in SQL.

The second query in Figure 9.27 also shows that we can do simple arithmetic during data retrieval using SQL (line 8):

SELECT SUM(product_inventory)/COUNT(*) FROM Products;

This query gives us the average number of items per product (line 12).

9.8.3 Restricting the Set of Records from Which We Retrieve Our Data

So far each of our queries has dealt with all of the records in a given table. We can restrict the scope of our retrieval by using a WHERE clause similar to the one we used for DELETE. Let us say we wanted to find out how many products in the Products table have fewer than 10 items in stock. As FIGURE 9.28 shows, we can use the query (line 1)

SELECT COUNT(*) FROM Products WHERE product_inventory < 10;

which gives a result of 96 (line 5).

If we want to sort the records, we can use the ORDER BY option, as shown in FIGURE 9.29 (line 4). In this case we want to retrieve a list of the products with inventory size greater than 90 and have it sorted based on the value of the attribute product_inventory. The DESC option indicates that we want the sorting performed in descending order. We also used the LIMIT option here so that we only see the top 10 records in the list. The LIMIT option has two (comma-separated) values in this case, 0 and 10. The first indicates how many records to skip before starting the retrieval, and the second tells how many records to retrieve. Since the first value is 0, it is actually redundant in this case, and we could have used the single value 10.

FIGURE 9.28 graphics/ch09/conditionSQL.txt

An SQL query illustrating “conditional retrieval”.

FIGURE 9.29 graphics/ch09/orderSQL.txt

Ordering retrieved records using SQL.

Note that this (somewhat longer than usual) query is entered over several lines, and the prompt from MySQL changes to -> from the second line on, until we enter the terminating semicolon.

9.8.4 Retrieving Data from More Than One Table with a Join

So far, in making our queries, we have only dealt with a single table. Our retrievals can also collect information from multiple tables at the same time, which is referred to as a join. We illustrate this in FIGURE 9.30. The query in this figure is our most complex query to date.

FIGURE 9.30 graphics/ch09/multiTableSQL.txt

Retrieving data from multiple tables using join in SQL.

In this example, we want to list the number of products in each product category as well as the total number of items in each category. We want to use the category description as the first column in our retrieved data. The category description is in the Ref_Product_Categories table, while the rest of the information is in the Products table.

This query also employs another useful feature of SQL that allows us to assign temporary aliases to certain entities, which provide more meaningful or concise names to be used within the query itself. Here, for example, we are indicating that the product_category_description will also be known as category, by using the keyword AS (line 1). Similarly, the count of products will also be known as products, and the sum of the product inventory will be known as product_inventory. We further abbreviate the Products table simply as P and the table Ref_ Product_Categories as R (line 4).

These two tables are “joined” by requiring the following condition, which connects them via their common keys, to be satisfied (line 5):

P.product_category_code = R.product_category_code

The records are then grouped by category (line 6), and finally they are sorted in descending order based on the value of products (line 7), and we are also limiting ourselves to viewing the first 10 records (line 8). The join we have seen in this case is the simplest form of join. SQL allows for more sophisticated joining of tables. See the MySQL manual or the References section at the end of this chapter for more information.

The data from any of the SQL queries we might make can be stored in new tables. We will look into that facility in the next section, along with general importing and exporting of both individual tables and entire databases.

9.9 Importing and Exporting Tables and Databases

In this section, we look at some of the SQL commands, as well as the GUI facilities, that we can use to copy, import, and export data in MySQL. You should know how to perform these tasks, since from time to time you may need to make a copy of one or more tables, with or without modifications, or even make a copy of an entire database.

9.9.1 Copying a Table or Part of a Table

Let’s illustrate this process by copying just part of a table. We can achieve this by combining the CREATE and SELECT commands, as shown in FIGURE 9.31. Suppose we want to make a copy of just a portion of our Customers table. We will only copy the customer_id, customer_first_name, customer_last_name, and login_name to another table called Customers2. First, we have to retrieve the required information, and here is the SELECT command we need:

SELECT customer_id,customer_first_name,
       customer_last_name,login_name
       FROM Customers;

Now all we have to do is precede this command with

CREATE Customers2 AS

and the output from the SELECT statement will be stored in the table Customers2. Figure 9.31 shows the complete MySQL session that performs this action. The SHOW tables; command (line 6) shows that the table Customers2 does not exist. We then create it using the above described combination of CREATE and SELECT. We then verify the creation of the table by looking at the first five records in the newly created Customers2 table.

Later, we delete the table with a

DROP TABLE Customers2;

command (not shown in the session), since we do not want it to be part of our database.

FIGURE 9.31 graphics/ch09/copyTableSQL.txt

Creating a copy of a portion of the Customers table using SQL.

9.9.2 Copying an Entire Database

Now we will show you how to create a copy of our entire database. First, we will use the phpMyAdmin interface, and then the command-line option.

In phpMyAdmin, you click on the database you want to “export” (webbook in our case) and then click on the Export link at the top. FIGURE 9.32 shows the web page that will pop up. You can select the tables you want to export, as well as the format of the exported file. We will use the default options of exporting all the files and exporting them as an SQL file. Clicking on the Go button will bring up the dialog box that tells you that the file will be saved as webbook.sql, as shown in FIGURE 9.33. You can save the file in an appropriate location.

FIGURE 9.32 graphics/ch09/displayExport.jpg

Exporting a database using phpMyAdmin.

The exporting and importing of large databases using phpMyAdmin can be a problem, as it may result in an unusually high volume of traffic over the Internet. For this reason, the command-line option is the preferred method for performing these tasks. You need a related command-line utility called mysqldump on your system, which should be available with any installation of MySQL.

FIGURE 9.34 illustrates how to export our entire database using a mysqldump command on a Linux system. The command works much like the mysql command. After the –u option, we specify that the user is webbook, and the –p option indicates that we will be entering a password.

FIGURE 9.33 graphics/ch09/displayResultExport.jpg

Result of exporting a database using phpMyAdmin.

FIGURE 9.34 graphics/ch09/mysqldump.txt

Exporting a database using mysqldump.

The name webbook after the –p option says that we want to dump the webbook database. The > sign redirects the output to a file called backupbookSep09.sql.

We confirm the existence of this file simply by doing a directory listing (line 3). The file is in place and has approximately 1.8MB worth of data. The format of the file created by exporting from phpMyAdmin and mysqldump is identical. We can take a quick peek at the file created by mysqldump, as shown in FIGURE 9.35.

Note that the most recent version of this file (which you should now use) is called

webbook2e_backup20150504.sql

and is available from the ch09 subdirectory of the text website.

As we can see, it is really just a file containing SQL commands that can be used to create a database, and you can run these SQL commands to recreate the database. In fact, you can import the entire Nature’s Source database (that is, the webbook database) to your own system using the commands in this file. First, you need to have an empty database with complete manipulation privileges. It can have any name. To illustrate, we will leave the webbook database unchanged and import its contents from the most recent version of the backup file into another database called webbook1 using this backup file. Our system administrator has created this database for us and granted us all the necessary privileges. All we need to do is run the following command:

mysql –u webbook –p webbook1 < webbook2e_backup20150504.sql

After the –u option, we are specifying that the user is webbook. The –p option again indicates that we will be entering a password. The name webbook1 after the –p option tells us that we want to work with the webbook1 database. The < sign redirects the input from our previously created file webbook2e_backup20150504.sql, which contains all the commands necessary to replicate the original database.

All of this activity, including the results of the command-line import, is shown in FIGURE 9.36 and its continuation, FIGURE 9.37 (except in the figures we are using the original backup file backupbookSep09.sql rather than the latest webbook2e_backup20150504.sql). We refer to the line numbers in those two figures to help you follow the action. First, we verify that we do in fact have a database called webbook1 (lines 9–10 of Figure 9.36). There are no tables in the database, since the command SHOW tables; comes up empty (lines 11–12 of Figure 9.36). Now we exit from MySQL and run the MySQL command that imports the database from the Linux command-line interface (lines 14–17 of Figure 9.36). As Figure 9.37 now shows, by going back into the database we can see that all the tables are in place. We also perform two SELECT queries (lines 50 and 62 of Figure 9.37) to do a “spot check” to convince ourselves that the records have been imported and we have the right number of customers. All of this takes place in lines 18–72 of Figure 9.37.

FIGURE 9.35 ch09/webbook2e_backup20150504.sql (excerpt)

Exported database in an SQL file.

9.9.3 Potential Problem with Importing via phpMyAdmin

As we cautioned before, using phpMyAdmin over the Internet can be problematic for importing large databases. Our database is of moderate size—less than 2MB. Let us see what happens if we try to import it using phpMyAdmin. FIGURE 9.38 shows what will happen if we click on a database such as webbook3, which is currently empty. We indicate that we want to import from the file webbook.sql that we had saved earlier by exporting through phpMyAdmin. Clicking on the Go button starts the importing process.

FIGURE 9.36 graphics/ch09/importSQL.txt (Part 1)

Importing a database using the command-line interface to MySQL (up to the actual import).

FIGURE 9.37 graphics/ch09/importSQL.txt (Part 2)

Importing a database using the command-line interface to MySQL (confirming the import).

FIGURE 9.38 graphics/ch09/displayImport.jpg

Importing a database using phpMyAdmin.

FIGURE 9.39 graphics/ch09/displayResultImport.jpg

Result of importing a database using phpMyAdmin.

Unfortunately, as shown in FIGURE 9.39, phpMyAdmin returns an error saying that it does not have the resources to carry out the importing task. Given what we have said, this is not entirely surprising. It follows that your best option may well be to work at the command line when you are importing a database, as shown in Figures 9.36 and 9.37.

Summary

All businesses need to keep track of their data and the most convenient and efficient way to do this is to keep that data in a well-designed database. Generally, a well-designed database will often have been normalized up to third normal form and will allow for scaling upward as the business grows.

MySQL is (currently, at least) an open-source database management system that is both simple enough for the home business owner to use and powerful enough for major corporations like Google and YouTube to employ as well.

A database comprises a number of tables in which each row (or record) contains related information about some aspect of a business, such as a customer or a product. Each column of the table contains information (an attribute) relating to the item in its corresponding row. It is important to have “good” tables, which means that each table will contain information concerning a single aspect of the business, such as customers, invoices, or products for sale, and each table has a key column that can be used if you wish to refer to rows in that table from another table.

SQL is the language we use to communicate with our database, and we can perform such communication using a command-line interface, or a sophisticated GUI like the one provided by phpMyAdmin, or one or more simple PHP scripts that we can write ourselves. There are SQL commands for creating databases, creating tables within those databases, adding information to the tables, modifying information that has already been stored, deleting some or all of that information, retrieving information and displaying it in various ways, and so on. We can also use SQL to perform numerical calculations on our data during the retrieval process.

Data can be imported to a database from external files, and exported to external files from a database. Even an entire database may be exported and used to replicate itself elsewhere.

Quick Questions to Test Your Basic Knowledge

  1. Can you think of at least three reasons why storing large amounts of data in “ordinary” files turned out not to be a good idea? Here are some keywords to guide your thinking: duplication, maintenance, excessive human involvement.

  2. What would you say to a business owner who had decided to keep all of the data for his business in one large table?

  3. How would you describe what it means for a database to be in first normal form?

  4. How would you describe what it means for a database to be in second normal form?

  5. How would you describe what it means for a database to be in third normal form?

  6. What has happened to MySQL that may (or may not) affect its future as a freely available, open-source database management system?

  7. What is the MySQL command for creating a database? Can you always expect this command to work on any system?

  8. What is the MySQL command that says you are going to use a database called my_business?

  9. What MySQL command would you use to create a table called Customers with the following five fields: a unique identification number, first name, last name, telephone number, and email address? Choose good names for the columns in your table.

  10. Give a typical command for inserting a record in the table created in the previous exercise.

  11. If you wanted to import a lot of data into this table from an external file, what would a typical line in that file look like?

  12. What is the MySQL command for displaying all the information in this table?

  13. What is the command for displaying just the names of the customers in the table?

Short Exercises to Improve Your Basic Understanding

The following exercises assume you have access to an installation of MySQL and that installation either has at least one database to which you also have access, or you have permission to create databases on the system. These are just a few suggestions to get you started on the road to becoming comfortable working with a database. Some of the exercises will take more time and effort than others, but all are well worth the effort. In fact, we do not specify whether to use the command-line interface or the GUI interface to perform any particular activity, and you should use both, preferably, to do the same exercise.

  1. Log in to your MySQL and give the command that shows you all of the databases you are allowed to “see”.

  2. Decide whether you will work with an already existing database, or create a new one. If you are going to create a new one, first do that. Then, in either case, give the command that sets you up for using whatever database you plan to use.

  3. Create a table called Customers that will hold, for each customer, that customer’s first and last name, telephone number, email address, and a unique identification number.

  4. Modify your Customers table using the ALTER command to add a city attribute for each customer.

  5. Enter some imaginary customers into your Customers table. First, use the INSERT command to enter just a couple of customers. Then create a file of customers and use the LOAD command to enter them.

  6. Create a second table called either Products or Services that contains products or services that might be purchased by one or more of these customers. Enter some imaginary products or services into the table, based on the kind of business you are working on in your “parallel project”.

  7. Create a third table called Orders that “connects” the customers and the products that they have purchased. Enter some imaginary orders into the table.

  8. Practice some SQL retrieval commands to display information from one or more of these tables in various forms.

  9. Experiment with the UPDATE command by changing some attribute values. Then confirm the changes using a SELECT query. Finally, use UPDATE again to change the values back to what they were.

  10. When you have finished the above experimentation, continue by experimenting with the DELETE, DROP, and TRUNCATE commands until all of your sample tables have been removed from your database.

Exercises on the Parallel Project

  1. Rethink the objective(s) of your business, from the point of view of the kind of data that you will need to handle, and the kind of information you will need to store and later retrieve. Virtually every business has products and/or services to sell and needs to have some kind of inventory management system to deal with all aspects of information relating to those products and services. Time constraints may limit the scope of your effort, but give some thought to what is both reasonable and feasible to implement, given your particular situation.

  2. Decide what tables you will need for your data, and the relationships between them.

  3. Decide what attributes should go into each table, what data type each attribute should have, and what keys should be used to identify the rows in individual tables and connect the tables.

  4. Think about any “business rules” that may be peculiar to your business and make sure they do not “break” your design. If you discover a business rule that is inconsistent with your database design, revise your design accordingly.

  5. Implement your design in MySQL, using any combination of phpMyAdmin and the command-line interface, but try to use both enough to enhance your familiarity with each.

  6. Add enough data to each table so that you can perform some meaningful queries, and test your database until you are satisfied it is going to help you perform the necessary tasks your business requires.

What Else You May Want or Need to Know

  1. MySQL started life as an open-source, freely available, database system. As of this writing, that is still true. However, MySQL was taken over by Sun Microsystems which, in turn, has been acquired by Oracle, who owns one of the largest proprietary database systems. Some believe that this does not bode well for the future of MySQL as a freely available system, and some of the MySQL websites are taking on a decidedly commercial look and feel. Nevertheless, until you hear differently, you should assume you will be able to download and install a free version of MySQL.

  2. When you first begin to work with an RDBMS (Relational Database Management System) it can be an intimidating experience. Even the installation of such a system on your home computer can seem like a daunting undertaking, especially since to get the full advantage of the installation you will want to have a web server, the PHP programming language, and phpMyAdmin all installed as well. Fortunately, some very clever and helpful people have worked long and hard to make all this “easy” for you, and you should know that it’s not as bad as it may seem. This is not to say you will not encounter the odd bump in the road during the setup, but it should be nothing you cannot overcome with a quick search on the web or a question to a more knowledgeable friend. See the following References section for some relevant links.

  3. In this chapter we have dealt only with the relational database and the MySQL version of that kind of database, as well as the SQL used to communicate with such databases. These databases and even MySQL are very widely used and are not going away anytime soon. However, you should be aware that there are many other kinds of databases and that recently the so-called NoSQL databases such as MongoDB and Couchbase have been gaining much influence on the web. You should keep an eye out for further developments in this area.

References

  • 1. Whatever information and software you need to get started with MySQL should be available at one or both of the following links:

    http://www.mysql.com/
    
    http://dev.mysql.com
    
  • 2. The home page for phpMyAdmin can be found here:

    http://www.phpmyadmin.net/home_page/index.php
    
  • 3. You can download MySQL, PHP, phpMyAdmin, and the Apache web server software in a single package as well. Such a package may also include other useful pieces of software. If you wish to pursue this option, check out one or both of the following sites:

    http://www.apachefriends.org/en/xampp.html
    
    http://www.wampserver.com/en/
    
  • 4. The W3Schools tutorial on SQL starts here:

    http://www.w3schools.com/sql/default.asp
    
  • 5. Wikipedia has articles on database normalization, functional dependency, and SQL:

    http://en.wikipedia.org/wiki/Database_normalization
    
    http://en.wikipedia.org/wiki/Functional_dependency
    
    http://en.wikipedia.org/wiki/SQL
    
  • 6. All of the following links will take you to articles that discuss the database normalization process:

    http://agiledata.org/essays/dataNormalization.html
    
    http://databases.about.com/od/administration/u/database_ basics.htm
    
    http://www.bkent.net/Doc/simple5.htm
    
    http://www.phlonx.com/resources/nf3/nf3_tutorial.pdf
    
  • 7. Here is a link to a tutorial article on the Database Answers site entitled How to Understand a Database Schema:

    http://www.databaseanswers.org/tutorial4_db_schema/index.htm
    
  • 8. For a tutorial on joins in MySQL see:

    http://www.tizag.com/mysqlTutorial/mysqljoins.php
    
  • 9. For information on NoSQL databases, and MongoDB and Couchbase in particular see:

    https://en.wikipedia.org/wiki/NoSQL
    
    https://www.mongodb.org/
    
    http://www.couchbase.com/
    
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.174.95