SECTION 02 / 15

One bill, many items: link two tables

2 min read
On this page 2 headings

Shopping bills have a different shape. A bill has one shop, customer and total, but it can contain any number of items. Srivatsan’s first bill has seven items. Giving each item its own set of columns would make wide, uneven rows.

A bill table stores fixed details once. An item table stores multiple rows, linked by BillID.
A bill table stores fixed details once. An item table stores multiple rows, linked by BillID.

One large table is possible, but repeats details

One row per item gives a fixed set of columns, but repeats the shop, customer and bill total on every row. That wastes space and creates a trap: adding those repeated bill totals counts the same purchase several times.

Split fixed details from item details

BillIDShopCustomerTotal
1SV StoresSrivatsan567
BillIDItemQuantityPriceCost
1Carrots1.55075
1Soap432128
1Tomatoes24080
1Bananas8864
1Socks356168
1Curd0.53216
1Milk1.52436
REAL COURSE DATA · FOLLOW THE PROCEDURE

Link a bill to its item rows

Read the current record, check the explanation, and watch the variables. Use Back to revisit any step.

Ready

CURRENT RECORD

Ready to begin

No records have been processed. Start with Next step.

What we remember

Processed
0

Start at the first step. Watch the current row and the values we remember.

BillID 1 appears once in the bill table and on all seven matching item rows. It connects the two tables. Reconstruct the bill by finding its fixed row and all item rows with that same ID. Use an identifier unique within the dataset; a customer can have several bills.

Quantity × Price = Cost for an item. Adding the seven Costs gives ₹567. Do not add the bill Total seven times. Every row in a particular table has the same column structure.

Try it yourself

Why not use Customer as the link?

Show explained answer

The same customer can have several bills. A unique BillID identifies the purchase.

Try it yourself

How many fixed-detail rows does Bill 1 need?

Show explained answer

One, even though it has seven item rows.

Try it yourself

What does adding ₹567 on every item row produce?

Show explained answer

₹3,969, which wrongly counts the same bill seven times.

Try it yourself

Can two different tables have different columns?

Show explained answer

Yes. The fixed-detail table and item table serve different purposes; each table has its own consistent columns.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑