0%

Chapter 6

Relate Tables Instead of Repeating Data

Let a Loan Point to Two Records

Riley has borrowed the cordless drill. The catalog already knows both records, but neither record should be copied into a new row.

items
idasset_tagname
1IT-100Cordless drill
members
idmember_codename
17MB-017Riley

A loan can store the two IDs instead:

loans.iditem_idmember_idchecked_out_onreturned_on
5011172026-08-18NULL

Now follow the two numbers in the loan row. Each arrow ends at the matching id; loan 501 itself does not point anywhere:

Loan 501 uses item_id 1 to point to item 1, Cordless drill, and member_id 17 to point to member 17, Riley.

Read across that last row. Each table’s id is its . item_id is 1, which points back to the drill. member_id is 17, which points back to Riley. The loan’s own id, 501, identifies this borrowing event. Each ID has one job.

This keeps changing facts in one place. If Riley’s displayed name changes, the member row changes once. Past and current loans still point to member 17. If the drill’s name is clarified, its item row changes once for the same reason.

One member may have several loan rows over time. One item may also have several loan rows over time, because returned loans stay as history. This pattern is called a one-to-many relationship: one parent record can be connected to many related records.

The missing in returned_on has a useful meaning too. The item has not been returned yet. That fact will soon help us find active loans. For now, concentrate on the shape: a loan carries its own facts and the two IDs that connect it to records we already have.

Which values belong in the row that records Riley borrowing the drill?