However, most real-world problems involve data that is too complex to fit in one table.. Database designers have developed some standard tech-niques for handling complex data that reduce
Trang 1This page intentionally left blank
Trang 2In chapters 9 and 10 you learn how to create a basic database and connect it to a PHP program PHP and MySQL are wonderful for working with basic
databases However, most real-world problems involve data that is too complex to fit in one table Database designers have developed some standard tech-niques for handling complex data that reduce redundancy, improve efficiency, and provide flexibility In this chapter you learn how to use the relational model to build complex databases involving multiple entities Specifically, you learn:
• How the relational model works.
• How to build use-case models for predicting data usage.
• How to construct entity-relationship diagrams to model your data.
• How to build multi-table databases.
• How joins are used to connect tables.
• How to build a link table to model many-to-many relationships.
• How to optimize your table design for later programming.
Data Normalization
11
Trang 3g r
s o
l u
g in
e r
Introducing the spy Database
In this chapter you build a database to manage your international spy ring (You
task, so you’ll need a database to keep track of all your agents Secret agents are assigned to various operations around the globe, and certain agents have certain skills The examples in this chapter will take you through the construction of such a database You’ll see how to construct the database in MySQL In chapter
12, “Building a Three-Tiered Data Application” you use this database to make a really powerful spymaster application in PHP
The spy database reflects a few facts about my spy organization (called the Pan-theon of Humanitarian Performance, or PHP)
• Each agent has a code name
• Each agent can have any number of skills
• More than one agent can have the same skill
• Each agent is assigned to one operation at a time
• More than one agent can be assigned to one operation
• A spy’s location is determined by the operation
• Each operation has only one location
This list of rules helps explain some characteristics of the data In database
are enforced
I N THE R EAL W ORLD
I set up this particular set of rules in a somewhat arbitrary way because they help make my database as simple as possible while still illustrating most of the main problems encountered in data design Usually you don’t get to make up business rules Instead, you learn them by talking to those who use the data every day.
Trang 4At first glance, the badSpydatabase design seems like it ought to work, but
prob-lems crop up as soon as you begin adding data to the table Figure 11.2 shows the
field agents
Inconsistent Data Problems
Gold Elbow’s record indicates that Operation Dancing Elephant is about
infil-trating a suspicious zoo Falcon’s record indicates that the same operation is
about infiltrating a suspicious circus For the purpose of this example, I’m
expecting that an assignment has only one description, so one of these
descrip-tions is wrong There’s no way to know whether it’s a zoo or a circus by looking
at the data in the table, so both records are suspect Likewise, it’s hard to tell if
Operation Enduring Angst takes place in Lower Volta or Lower Votla, because the
two records that describe this mission have different spellings
The circus/zoo inconsistency and the Volta/Votla problem share a common cause
In both cases the data-entry person (probably a low-ranking civil servant, because
i o
FIGURE 11.1
The badSpy
database schema
looks reasonable
enough.
Trang 5international spy masters are fartoo busy to do their own data entry) had to type the same data into the database multiple times This kind of inconsistency causes all kinds of problems Different people choose different abbreviations You may see multiple spellings of the same term Some people simply do not enter data if it’s too difficult When this happens, you cannot rely on the data (Is it a zoo or a circus?) You also can’t search the data with confidence (I’ll miss Black-ford if I look for all operatives in Lower Volta, because he’s listed as being in
be very difficult to find everywhere this word is misspelled and fix them all
Problem with the Operation Information
There’s another problem with this database If for some reason Agent Rahab were dropped from the database (maybe she was a double agent all along), the infor-mation regarding Operation Raging Dandelion would be deleted along with her record, because the only place it is stored is as a part of her record The opera-tion’s data somehow needs to be stored separately from the agent data
Problems with Listed Fields
The specialty field brings its own troubles to the database This field can contain more than one entity, because spies should be able to do more than one thing
362
g r
s o
l u
g in
e r
FIGURE 11.2
The badSpy
database after
I added a few
agents.