- MySQL
- What Is MySQL?
- Installation
- Configuration for Use with IMP
- Database System Concepts
- The IMP Database Structure
- SQL Overview
- MySQL's License
- Onward!
Database System Concepts
The next few sections cover database theory and implementation. These concepts will help you understand what was and was not done with the IMP database. This section can give you some ideas for future modifications. If you are interested only in installing IMP, skip to the next chapter.
Database Design Tips
After you have used IMP for awhile, you will probably want to change it. In this section, I will give you a brief overview of database design and use. The lessons learned here will give you concepts to use as you decide what information to capture.
You will also be shown how to query a database for specific information. The skills learned here will help you troubleshoot installation problems and future user problems.
System Design Overview
About 15 years ago, I learned a truth that has had a profound effect on my abilities as a designer. It is basically this: If you define every piece of data in a system and what you do to each piece of data, you have defined the system, and you know what your software must do.
This flew in the face of what I knew about flow charts and design documents. It turned out to be absolutely correct. After you have identified every piece of data, you can generate the design.
Even today, I often start the design first, and then realize I need to define my data. I then backtrack and nail down all the data. You need to do the same, especially in database design. Let's cover data definition and grouping issues.
General Database Design Issues
A good database design requires grouping the data element types together in certain ways. I will explore the methods involved using plain language and examples.
After you have IMP running, you might decide to provide a free service with IMP. There is no such thing as a free lunch, so the system has to be paid for. The most common way of financing free online services is to sell advertising space. For this to succeed, you usually need demographic data. The following examples don't have a direct bearing on the current IMP database. However, they do provide you with some concepts that will give you a handle on collecting useful data on users.
You will be taken through a database design scenario. This scenario has been chosen to illustrate database design concepts in the simplest fashion.
Let's assume that your customers live in an area famed for its best of breed dogs. You need to collect information regarding these customers and their pets for advertising demographics. We will start from the beginning concepts, and pretend we have the base information from the local veterinarian.
To begin with, let's take a brown dog. The dog is an animal of a certain type, or breed. The data are as follows:
- Animal Typedog
- BreedBlack Lab
The data element types are Animal and Breed. The information concerning these types is dog and Black Lab. To store this information in a database, we could create a column named Animal, and a column named Breed. The attributes dog and brown go into the rows. With this database design, we can also insert Persian cats.
Relational Databases
Now assume you have decided to narrow the database to dogs and have expanded the information you are collecting to include owner name and phone number plus the ID tag number attached to the dog's collar.
In our example, we are dealing with dogs and owners. Therefore, it makes sense to create a dog_info table and owner_info table. Generally, you need to create as many tables as you have classes of information. This is a very intuitive process. In this case, we can classify the information by dogs and owners.
This is the beginning of a relational database. The dogs are part of the pet owner's life. In real life, a veterinarian gives the dog the owner's last name. The data in the tables is related in real life, and the information in the tables reflects that relationship. Now let's work on the tables.
Data Identification Techniques
A good way to begin on a table design is to get a notepad (electronic or otherwise), and start listing all the data element types that will be in it. A data element type is a piece of information that you can describe. The data element type is usually put into a database as a fixed-name column. The description of the data element is usually put into the database as a variable piece of information stored in a row.
In database design, a data element type is referred to as an entity, a data field, or a data column.
A data element is referred to as an entity attribute (or simply attribute), a data record, or a data row.
A data table can be called a database. The description's data table and database are used interchangeably in many books. For our use, a data table (or simply table) is a collection of data columns, and a database is a collection of data tables.
The definitions used in database theory and design have evolved over the years, and will probably continue to evolve.
It is important to determine the level of detail and the type of information covered by your data element types. It makes sense to create a column named Breed because we are doing a table that consists solely of dog families. It would not make sense to do such a column if we were doing a table cataloging all earth animals. It is generally best to hold off determining the table names until you have most of the data described. After you have done this, you can begin naming tables and columns with confidence. After the tables are done, you can start defining keys.
As far as the IMP database is concerned, we find we do not need much relational information. The user column provides the glue between the preferences table and the address table. The preferences table contains the user-settable options for his IMP usage. The address table contains the list of all that user's contacts.
Database Design Helpers
Ironically, one of the best tools for developing a database is a database! The column names for such a database could possibly be item_name, item_description, item_use, item_group, when_modified, and rules_for_modification. These can also be headings on a paper worksheet or electronic spreadsheet.
Very carefully fill in all the fields for each data item you have identified. The item_use field would contain a description of when this item would be used, such as "When dog tag number is assigned." The item_group field can simply be a number or a group description. In our example, you could group information by "dog" or "dog owner." The when_modified field would contain information such as "when dog received." The rules_for_modification field could be something such as "when payment is made."
All this information helps you decide how to arrange the data. You make up the rules, and you determine when the information is correct. Try to use very similar or the same language in the same columns whenever possible.
After you have filled in the database information, try printing it out, sorted by various columns. Sort by group, sort by modification time, and sort by rules of modification. This will give you a head start on a good database design.
Another useful tool with a free evaluation download is called dezign. It is a visual tool that runs on the Windows platform. It is available from http://www.heraut.demon.nl/dezign/index.html. The registered version removes the limits on the number of entities and attributes you can deal with during the design process. It knows about MySQL, and it holds your hand as you work with tables. It can also generate SQL scripts.
Keys
A key is a unique item. In real life, you have a key to a door. In general practice, we assume a specific key only unlocks a specific door. A key in a database works much the same way. A unique key value in a database refers to only one row. Non-unique keys are analogous to master keys that unlock more than one door. Non-unique database keys will refer to more than one row.
It is a good idea to have at least one key in every database table. Keys are used to make data lookup in databases much faster. Keys are kept in special index tables that are managed by the database, in a specific sort order. This ordering of key information allows the database engine to do lookups for specific information quickly.
In our database example, a good key would be the dog tag ID number. This number is guaranteed to uniquely identify one dog. This number will never change for the life of the dog, or most likely even after death. It is a unique key. Only one instance of that value exists.
Another key could be an owner's name or a dog's name. However, you could have many dogs named Spot, or many owners named John Smith. These would be non-unique keys. Many rows could exist containing John Smith.
Finally, we can arbitrarily decide to create a unique key. We can give each owner an ID number that is guaranteed to be unique. It is unique because we say so. The term for that is by definition. It can be an automatically incrementing number, managed by the database. Only one instance of any particular number would ever exist in the database.
Note
A unique key managed by the auto-increment feature of the database can be considered unique. It might not be, in the strictest sense. Let's assume our database is extremely active, and our key is an auto-increment integer with an upper limit of about 4 billion. If we were putting 1 billion pets a year into the database, we would run out of unique keys in 4 years. However, if we put in one dog per hour, 24 hours a day, we would only put in about 9,000 pets per year. It would take somewhere around 500,000 years (a very rough approximation) to use all the keys in the database.
For all practical purposes, auto-increment keys can be considered totally unique and infinitely large. In this case, infinity is a relative concept.
In our example, the dog tag ID number is the key for the dog_info database. We will put a unique ID number in the owner_info database and make it a key.
Correct Utilization of Data
Now we need to determine how to relate the dogs to the owners. We can store the owner's name with each dog. We can also store the dog ID numbers with each owner. However, each of these solutions presents problems.
If we store the owner's name with each dog, we are taking up a lot of space in the database for each dog we enter. If we store the tag ID number for each dog with the owner, we have to worry about leaving enough room in the owner table to make sure we cover any number of dogs for that person. This is clearly very difficult.
Each of these solutions provides duplicate information. Also, if an owner should legally change her name, the amount of work necessary to correct the information in the database is quite large. Normalization is the name for the technique of correcting duplication of data.
Normalization is a term used for minimizing data duplication in a database. In other words, don't put the name "John Smith" in more than one database table. As you start considering the issues in handling this concept, you will begin to see it is simply the use of common sense. However, professors who teach database theory have come up with terms to describe this common-sense approach.
This leads to confusion for the people who have never heard this term. Also, unless you have thought through the ramifications of normalization, some surprises are in store. I use plain English to explain normalization. You might find that different database books explain normalization using different languages. Some books explain better than others.
To start normalizing, you must group the data properly. Generally, put the highest level concept into its own table. (Examples are dog_info and owner_info.) After that, create columns to hold data element types (or entities). The attributes of these entities are the entries (rows) in the database.
After you have done this, go over the database and look for duplicate information. In our example, let's assume the owner's name is initially stored in the dog_info table and in the owner_info table. Our table information looks something like this:
TABLE: dog_info TABLE: owner_info OwnerName OwnerName Breed Phone DogName OwnerIDNumber (KEY) OwnerIDNumber IDTagNumber (KEY)
Because we have the owner's name in two tables, we have duplicate information. If we consider the type of data carefully, we also realize we have a one-to-many relationship between the two tables. One owner can own many dogs. However, for the sake of illustration, one dog belongs to only one owner.
The problems we find in the sample tables illustrate the reasons for normalization. Several named normalization steps exist. The steps have technical terms not covered here. To do further study, a good book is Codd's Further Normalization of the Data Base Relational Model, 1972.
The first step in normalizing data is achieved when we have identified all data element types properly. In its simplest case, we have not achieved this step if we have made a table name a column. If we had made owner_info a column name, we would have a normalization problem. The problem is obvious when we realize the owner has several attributes. In our example, they are the owner's name and the owner's phone number. We can say, "The phone number is owner information." We can also say, "The owner's name is owner information." Both statements are true. However, this would lead to having multiple values for an owner info column.
After we realize this, we realize the owner_info belongs in a table. We then set separate columns for the attributes. This solves the problem of trying to stuff a table into a column. Removing all these types of problems is our goal.
The next step is to see that data is not duplicated across tables or within a table. In our example, because an owner can own multiple dogs, putting the owner's name in the dog_info table along with the reference ID pointing to the owner info table causes duplication of information in that table and in the owner_info table.
The name is repeated for each dog owned, and occurs once in the owner_info table. Errors can creep in, and misspellings of the owner's name can cause confusion. The data is also redundant because following the reference ID gives you the ability to recover the Owner's name.
To solve this, we place the name in one location where it appears once. The interesting thing about an owner is there is only one owner for a dog. Therefore, the owner name is relatively unique, and should show up in the database system only once. We handle this by removing the owner's name from the dog_info table.
This leaves us with another possible problem. For example, if we were to put State Name and a two-letter State Abbreviation in the address in owner_info, we would have a future problem. Whenever the State Name changed, the State Abbreviation would also have to change. At the same time, having both pieces of information in the owner_info table adds nothing to the owner_info table's data store.
The solution is to create a new table that lists State Name for State Abbreviation. You then would use either a State Abbreviation in the owner_info table, or an ID in that table to look up the State Name.
Finally, note that the rules in the second step are technically broken in our final design. There are two problems. The first problem is that the Breed column is the name of the Breed, rather than a Breed ID. Because Breed names repeat, and can be misspelled, this is not a good thing. To finish out the design, the Breed column needs to be an ID that references to a Breed table. If you decide to create this table, you should modify the database design to accommodate this change.
The second problem is that the owner's ID number is in the dog table, and it repeats for each dog the owner owns. This will always happen in a one-to-many relationship. Note, however, that the database design is clean, because changing information such as the phone number of the owner will affect all dogs owned. When you look up the dog's table entry using the IDTagNumber, you can then look up the owner's table entry by the using OwnerIDNumber. At this point you have the correct phone number. You can also find all dogs owned by the owner by finding all rows that have that specific OwnerIDNumber.
Our final database looks like this:
TABLE: dog_info TABLE: owner_info FKOwnerIDNumber (KEY) OwnerName Breed Phone DogName OwnerIDNumber (KEY) IDTagNumber (KEY)
You will see that the FKOwnerIDNumber in the dog_info table is defined as a key. This will be a non-unique key. Telling the database that it is a key will speed lookups of which dogs an owner has. This is a perfectly legitimate use of a key.
Modifications to the IMP Database
These concepts can be applied to your study of the IMP database design. As you examine the IMP tables, you find they are very short and very easy to understand. This is the KISS principal at work. There is one duplicate piece of data (the username is repeated between tables), but this is not a fatal mistake.
The IMP tables can be improved and enhanced. The most obvious enhancement is to change the user column to a key column in one of the tables. You would need to create a unique ID column and reference this in the other table. This would provide faster lookup. However, in personal experience, the IMP table supports about one thousand users very well. The amount of re-work of the PHP code to accomplish this could cause you much grief and cost time.
If you decide to store extra information in the IMP database, I recommend that you place your changes in separate tables using a key loosely based on the username. A username must be unique within an email system. Alternatively, because a Social Security Number is unique, it is a good key for identifying individuals. Another fairly unique ID is the username linked with the user phone number, though this can repeat in rare circumstances. Unless you really want to do a lot of rework of the PHP code, leave the current table structures alone and enhance the database with separate tables.
In other words, break the database design rules for simple changes! This will cause fewer problems in the long run, because the only code that will break is the new code you added. That is part of the KISS principal. When modifying an existing system, change as little of the current system as possible, unless the gain outweighs the pain. This is true even if the current system is poorly designed.
Graphical Database Representations
Many ways exist to design databases graphically. I like to use rectangular boxes with lines connecting them. The name of the table is at the top of the rectangular box, enclosed with lines to set it apart.
Each data element in the table is listed below the table name. A bullet or asterisk indicates which element is a key. A line goes from one table to another table where a relationship exists. I like to draw a line from the data element in one table to the corresponding data element in the next table.
Our sample database is shown in Figure 3.1. The good thing about this particular graphical approach is that it can be worked on the back of a napkin! When done, it will transfer easily into a drawing program. It is a matter of taste. You should feel free to look for a table representation method you prefer.
Figure 3.1 The sample database in graphic form as created in Dezign.