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

211<br />

Relationships<br />

Foreign keys represent a relationship between data in two tables. For example, the link<br />

from Orders to Customers represents a relationship between a row in the Orders table<br />

<strong>and</strong> a row in the Customers table.<br />

Three basic kinds of relationships exist in a relational database.They are classified<br />

according to the number of elements on each side of the relationship. Relationships can<br />

be either one-to-one, one-to-many, or many-to-many.<br />

A one-to-one relationship means that one of each thing is used in the relationship.<br />

For example, if you put addresses in a separate table from Customers, they would have a<br />

one-to-one relationship between them.You could have a foreign key from Addresses to<br />

Customers or the other way around (both are not required).<br />

In a one-to-many relationship, one row in one table is linked to many rows in another<br />

table. In this example, one Customer might place many Orders. In these relationships,<br />

the table that contains the many rows has a foreign key to the table with the one row.<br />

Here, we put the CustomerID into the Order table to show the relationship.<br />

In a many-to-many relationship, many rows in one table are linked to many rows in<br />

another table. For example, if you have two tables, Books <strong>and</strong> Authors, you might find<br />

that one book was written by two coauthors, each of whom had written other books,<br />

on their own or possibly with other authors.This type of relationship usually gets a table<br />

all to itself, so you might have Books, Authors, <strong>and</strong> Books_Authors.This third table<br />

would contain only the keys of the other tables as foreign keys in pairs, to show which<br />

authors are involved with which books.<br />

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

Knowing when you need a new table <strong>and</strong> what the key should be can be something of<br />

an art.You can read reams of information about entity relationship diagrams <strong>and</strong> database<br />

normalization, which are beyond the scope of this book. Most of the time, however, you<br />

can follow a few basic design principles. Let’s consider them in the context of Book-O-<br />

Rama.<br />

Think About the Real-World Objects You Are Modeling<br />

When you create a database, you are usually modeling real-world items <strong>and</strong> relationships<br />

<strong>and</strong> storing information about those objects <strong>and</strong> relationships.<br />

Generally, each class of real-world objects you model needs its own table.Think about<br />

it: You want to store the same information about all your customers. If a set of data has<br />

the same “shape,” you can easily create a table corresponding to that data.<br />

In the Book-O-Rama example, you want to store information about customers, the<br />

books that you sell, <strong>and</strong> details of the orders.The customers all have names <strong>and</strong> addresses.<br />

Each order has a date, a total amount, <strong>and</strong> a set of books that were ordered. Each book<br />

has an International St<strong>and</strong>ard Book Number (ISBN), an author, a title, <strong>and</strong> a price.

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

Saved successfully!

Ooh no, something went wrong!