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 />

215<br />

This table provides a link between the Orders <strong>and</strong> Books tables.This type of table is<br />

common when a many-to-many relationship exists between two objects; in this case, one<br />

order might consist of many books, <strong>and</strong> each book can be ordered by many people.<br />

Choose Sensible Keys<br />

Make sure that the keys you choose are unique. In this case, we created a special key for<br />

customers (CustomerID) <strong>and</strong> for orders (OrderID) because these real-world objects might<br />

not naturally have an identifier that can be guaranteed to be unique.You don’t need to<br />

create a unique identifier for books; this has already been done, in the form of an ISBN.<br />

For Order_Item, you can add an extra key if you want, but the combination of the two<br />

attributes OrderID <strong>and</strong> ISBN are unique as long as more than one copy of the same book<br />

in an order is treated as one row. For this reason, the table Order_Items has a Quantity<br />

column.<br />

Think About What You Want to Ask the Database<br />

Continuing from the previous section, think about what questions you want the database<br />

to answer. (For example, what are Book-O-Rama’s best-selling books?) Make sure that<br />

the database contains all the data required <strong>and</strong> that the appropriate links exist between<br />

tables to answer the questions you have.<br />

Avoid Designs with Many Empty Attributes<br />

If you wanted to add book reviews to the database, you could do so in at least two ways.<br />

These two approaches are shown in Figure 8.7.<br />

BOOKS<br />

ISBN<br />

Author Title Price<br />

Review<br />

0-672-31697-8<br />

0-672-31745-1<br />

0-672-31509-2<br />

Michael Morgan<br />

Thomas Down<br />

Pruitt.et al.<br />

Java 2 for Professional Developers<br />

Installing GNU/Linux<br />

Teach Yourself GIMP in 24 Hours<br />

34.99<br />

24.99<br />

24.99<br />

BOOKS_REVIEWS<br />

ISBN<br />

Review<br />

Figure 8.7<br />

To add reviews, you can either add a Review column to the<br />

Books table or add a table specifically for reviews.

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

Saved successfully!

Ooh no, something went wrong!