Foreign Keys
We now have a starting point for a physical schema. We have not yet translated the relationships into the physical data model. As we discussed earlier, once you have refined your data model, you should have all 1-to-1 and 1-to-M relationships—the M-to-M relationships were resolved via junction tables. We model relationships by adding a foreign key to one of the tables involved in the relationship. A foreign key is the unique identifier, or primary key, of the table on the other side of the relationship.
The most common relationship is the 1-to-M relationship. This relationship is mapped by placing the primary key from the “one” side of the relationship into the table on the “many” side. In our example, this rule means we need to do the following:
Place a
RECORD_LABEL_ID
column in theCD
table.Place a
CD_ID
column in theSONG
table.Place an
ARTIST_ID
column in theSONG
table.
Table 7-3 shows the new schema.
Table |
Column |
Data type |
Notes |
|
|
|
Primary key |
|
| ||
|
|
Foreign key | |
|
|
|
Primary key |
|
| ||
|
|
|
Primary key |
|
| ||
|
| ||
|
|
Foreign key | |
|
|
Foreign key | |
|
|
|
Primary key |
|
|
We do not have any 1-to-1 relationships in this data ...
Get Managing & Using MySQL, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.