Understanding the Role of Keys in Database Design
So far, this chapter has presented quite a few ERDs. Many of them depict relationships, but so far there's been no discussion of exactly how a relationship between two entities is created and maintained. The answer is simple: We create fields in each entity called keys, which allow instances of one entity to be associated with instances of another. You might relate orders to customers, for example, by using a customer's Social Security number as a key. Each order would then contain the Social Security number of the related customer as one of its attributes. The following sections explore the concept of keys in more detail.
Keys That Determine Uniqueness
One of the crucial tenets of relational database theory is that it has to be possible to identify any database row, anywhere, without ambiguity. Put differently, every row in every table should have a unique identifier. If I have a record in a table of orders, I want to be able to ask it "What customer do you tie to?" and get an unambiguous answer. I need a simple answer: "Customer 400." End of story. The number 400, as it appears in the customer table, is a unique identifier.
A piece of data that is capable of uniquely identifying a database row is known as a primary key. A primary key is an attribute the values of which are (and always will be) unique for every single row in the database. It's a unique identifier, like a Social Security number, an ISBN number for a book, or a library card catalog number.
We recommend that every database table you design have a primary key, without exception. Some database systems force you to create a primary key for each new table. FileMaker Pro doesn't, but we strongly recommend that you do so anyway. There's very little to lose and a great deal to gain by following this practice.
The discussions in this chapter assume that every table you design, without exception, has a primary key.
Keys That Refer to Other Tables
Keys are essential to specifying relationships between tables. Going back to the example of customers and orders, the relationship between these entities is one-to-many: One customer may have many orders.
If you've followed the rule about always having a primary key, your Customer entity has a primary key, which you might call Customer ID. Now, each unique customer may have many related orders. To forge that relationship, each record in the Order table needs to store the Customer ID (the primary key) of the related customer. This value, when it's stored in the Order table, is known as a foreign key. The reason for the term is simple: The value in the Order table refers to a primary key value from a different ("foreign") table.
Figure 5.13 demonstrates how primary and foreign keys work together to create relationships between database tables. In a one-to-many relationship, the "many" side of the relationship always needs to contain a foreign key that points back to the "one" side. The child record thus "knows" who its parent is.
Figure 5.13. A one-to-many relationship between customers and orders, showing primary and foreign keys.
FileMaker Pro has several built-in capabilities that help you add strong key structures to your FileMaker databases. For some ideas on how best to define key fields in FileMaker Pro, see "Working with Keys and Match Fields," p. 162. |
Many to Many Relationships Solving the Puzzle
|