13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Designing Your <strong>Web</strong> Database<br />

213<br />

OrderID<br />

Amount<br />

Date<br />

CustomerID<br />

Name<br />

Address<br />

City<br />

12<br />

199.50<br />

25-Apr-2007<br />

1<br />

Julie Smith<br />

25 Oak Street<br />

Airport West<br />

13<br />

43.00<br />

29-Apr-2007<br />

1<br />

Julie Smith<br />

25 Oak Street<br />

Airport West<br />

14<br />

15.99<br />

30-Apr-2007<br />

1<br />

Julie Smith<br />

25 Oak Street<br />

Airport West<br />

15<br />

23.75<br />

01-May-2007<br />

1<br />

Julie Smith<br />

25 Oak Street<br />

Airport West<br />

Figure 8.4<br />

A database design that stores redundant data takes up extra space<br />

<strong>and</strong> can cause anomalies in the data.<br />

Such a design creates two basic problems:<br />

n It’s a waste of space.Why store Julie’s details three times if you need to store them<br />

only once?<br />

n It can lead to update anomalies—that is, situations in which you change the database<br />

<strong>and</strong> end up with inconsistent data.The integrity of the data is violated, <strong>and</strong> you no<br />

longer know which data is correct <strong>and</strong> which is incorrect.This scenario generally<br />

leads to losing information.<br />

Three kinds of update anomalies need to be avoided: modification, insertion, <strong>and</strong> deletion<br />

anomalies.<br />

If Julie moves to a new house while she has pending orders, you will need to update<br />

her address in three places instead of one, doing three times as much work.You might<br />

easily overlook this fact <strong>and</strong> change her address in only one place, leading to inconsistent<br />

data in the database (a very bad thing).These problems are called modification anomalies<br />

because they occur when you are trying to modify the database.<br />

With this design, you need to insert Julie’s details every time you take an order, so<br />

each time you must make sure that her details are consistent with the existing rows in<br />

the table. If you don’t check, you might end up with two rows of conflicting information<br />

about Julie. For example, one row might indicate that Julie lives in Airport West, <strong>and</strong><br />

another might indicate she lives in Airport.This scenario is called an insertion anomaly<br />

because it occurs when data is being inserted.<br />

The third kind of anomaly is called a deletion anomaly because it occurs (surprise, surprise)<br />

when you are deleting rows from the database. For example, imagine that after an<br />

order has been shipped, you delete it from the database. After all Julie’s current orders<br />

have been filled, they are all deleted from the Orders table.This means that you no<br />

longer have a record of Julie’s address.You can’t send her any special offers, <strong>and</strong> the next<br />

time she wants to order something from Book-O-Rama, you have to get her details all<br />

over again.<br />

Generally, you should design your database so that none of these anomalies occur.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!