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.

214 Chapter 8 Designing Your <strong>Web</strong> Database<br />

Use Atomic Column Values<br />

Using atomic column values means that in each attribute in each row, you store only<br />

one thing. For example, you need to know what books make up each order.You could<br />

do this in several ways.<br />

One solution would be to add a column to the Orders table listing all the books that<br />

have been ordered, as shown in Figure 8.5.<br />

ORDERS<br />

OrderID<br />

CustomerID<br />

Amount<br />

Date<br />

Books Ordered<br />

1<br />

3<br />

27.50<br />

02-Apr-2007<br />

0-672-31697-8<br />

2<br />

1<br />

12.99<br />

15-Apr-2007<br />

0-672-31745-1. 0-672-31509-2<br />

3<br />

2<br />

74.00<br />

19-Apr-2007<br />

0-672-31697-8<br />

4<br />

3<br />

6.99<br />

01-May-2007<br />

0-672-31745-1. 0-672-31509-2. 0-672-31697-8<br />

Figure 8.5<br />

With this design, the Books Ordered attribute in each row has<br />

multiple values.<br />

This solution isn’t a good idea for a few reasons.What you’re really doing is nesting a<br />

whole table inside one column—a table that relates orders to books.When you set up<br />

your columns this way, it becomes more difficult to answer such questions as “How<br />

many copies of Java 2 for Professional Developers have been ordered?”The system can no<br />

longer just count the matching fields. Instead, it has to parse each attribute value to see<br />

whether it contains a match anywhere inside it.<br />

Because you’re really creating a table-inside-a-table, you should really just create that<br />

new table.This new table, called Order_Items, is shown in Figure 8.6.<br />

ORDER_ITEMS<br />

OrderID<br />

ISBN<br />

Quantity<br />

1<br />

2<br />

2<br />

3<br />

4<br />

4<br />

4<br />

0-672-31697-8<br />

0-672-31745-1<br />

0-672-31509-2<br />

0-672-31697-8<br />

0-672-31745-1<br />

0-672-31509-2<br />

0-672-31697-8<br />

1<br />

2<br />

1<br />

1<br />

1<br />

2<br />

1<br />

Figure 8.6<br />

This design makes it easier to search for particular books that<br />

have been ordered.

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

Saved successfully!

Ooh no, something went wrong!