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_IDcolumn in theCDtable.Place a
CD_IDcolumn in theSONGtable.Place an
ARTIST_IDcolumn in theSONGtable.
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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access