Solution →

CiTi · Computer Coding · Year One · Unit 4

Relational Databases: Why Lists Aren’t Enough

Student Handout

The Big Idea

A list keeps every fact on every row. A relational database keeps each fact in one place and uses a number to point at it. Two days, one plan: today we learn the words and build a data map together. Tomorrow you build your own.

One big list

Type “Bethesda” on every row that needs it. Change one and you have to find them all.

Relational tables

Type “Bethesda” once. Every other table says r_developer = 4 and the number does the pointing.

Opening question: think of a video game you play. What are 3 things it has to remember, and where do you think it keeps them?

Part A: Words to Know

WordMeaningAlso called
TableA collection of rows and columns about one kind of thing (store, city, game)entity, relation
RowOne item in the table (one store)record, tuple
ColumnOne characteristic of every row (title, phone)field, attribute
RelationshipA link between two tables, so you can find data in one that belongs to a row in the other
Primary keyThe column that identifies a row. It is unique for every row. We always call it sku.PK
Foreign keyA column that holds the sku of a row in another table. This is the link.FK
Junction tableA table that holds only foreign keys (and sometimes data of its own) to connect two tables in a many-to-many relationshipjoin table, link table
Our naming rules
  • The first column of every table is sku: a number, unique, never reused.
  • A foreign key is r_ plus the table it points at: r_state, r_store. The r_ prefix is Hungarian notation: a short prefix on a name that tells you what kind of thing it holds. Here r_ stands for relatedto: r_city reads “related to city.”
  • A foreign key holds the number, never the name.
  • Junction tables start with jt1_, jt2_ and name the tables they connect.
  • All lowercase. Underscores, no spaces.
A peek ahead: in WebDNA, a table is a plain text file with the values separated by tabs, plus a header file that lists the column names.
city.hdr sku title r_state city.db 1 Mexico 1 2 Syracuse 1 3 Cincinnati 2
In your own words: what is the difference between a primary key and a foreign key?

Part B: Break the List

Here is a store’s game prices kept in one big list. Look for what is repeated.

one_big_list
store city state phone game developer platform price
Walmart Mexico NY 555-1111 Starfield Bethesda XBOX 40
Walmart Mexico NY 555-1111 Skyrim Bethesda PC 65
Walmart Syracuse NY 555-2222 Starfield Bethesda XBOX 55
Walmart Syracuse NY 555-2222 Skyrim Bethesda PC 60
Target Mexico NY 666-1111 Starfield Bethesda PC 49
Game Stop Mexico NY 888-1111 Skyrim Bethesda XBOX 25
1. How many times is “Bethesda” typed? 2. Game Stop changes its name to GameStop. How many rows must you edit, and what happens if you miss one? 3. Target removes its only Starfield listing, so you delete that row. What information have you lost that has nothing to do with Starfield? 4. Someone types “Syracues” on one row. What goes wrong when you search for Syracuse?

Part C: One-to-Many

One teacher teaches many classes. Each class has one teacher. So the foreign key goes on the “many” side: class gets r_teacher. No extra table needed.

sku primary key (unique id for the row)   r_name foreign key (the sku of a row in another table)

teacher
sku first_name last_name subject
1 Sarah Johnson Math
2 Mark Davis English
3 Priya Patel Science
class
sku title r_teacher
101 Algebra I 1
102 Geometry 1
103 English II 2
104 Biology 3
How many classes does Sarah Johnson (teacher sku 1) teach? Which ones? Who teaches Biology? Write the steps you followed.

Part D: Many-to-Many and the Junction Table

A student takes many classes. A class has many students. Neither table can hold the link, so a junction table does. Each row says “this student is in this class.”

student
sku first_name last_name
1 Ann Smith
2 John Doe
3 Emily Brown
class
sku title r_teacher
101 Algebra I 1
102 Geometry 1
103 English II 2
104 Biology 3
jt1_student_class
sku r_student r_class
1 1 101
2 1 103
3 2 101
4 2 104
5 3 101
Which classes is John Doe (student sku 2) in? Who is in Algebra I (class sku 101)? Why can’t we just put r_class in the student table?

Part E: Build the Store Map Together

We are mapping stores, cities and states for a client. Fill each table in as the class decides it. Foreign key columns hold numbers.

Step 1: state and city (one-to-many)
state
sku title abbv
city
sku title r_state
Step 2: stores
store
sku title
Step 3: store in a city (many-to-many). The address and phone number belong to a store in a city, so they live here.
jt1_store_city
sku r_store r_city address phone
Step 4: Count. How many core tables? How many junction tables?

Part F: Follow the Keys

The test of a good data map is that you can answer questions with it, using nothing but numbers. Use your tables from Part E.

1. Pick one row in your jt1_store_city. Write down its store, city and state by following each foreign key to its table. 2. The class question: what is the phone number of the Target in Syracuse? List each table you visit, in order.

Part G: Your Assignment (Day 2)

Your First Data Map. Using the information below, build a normalized database as HTML tables in a file named database.html in your lessons folder. Hint: you will end up with 6 core tables and 3 junction tables.

Stores (a store can have several locations)
Walmart: 2 cities, phones 555-1111 and 555-2222
Target: 2 cities, phones 666-1111 and 666-2222
Best Buy: 1 city, phone 777-1111
Game Stop: 1 city, phone 888-1111
Cities
Mexico, NY
Syracuse, NY
Cincinnati, OH
Platforms
PS5
PS4
Nintendo
XBOX
PC
Games (developer) and where they run
No Man’s Sky (Hello Games): PS5, PS4, XBOX, PC
Last of Us (Naughty Dog): PS5, PS4
Apex (EA): PS5, PS4, XBOX, PC
Starfield (Bethesda): XBOX, PC
Madden (EA): PS5, PS4, XBOX, PC
Skyrim (Bethesda): PS5, PS4, XBOX, PC

You must show the price of every game, on every platform, at every store location.

Rules from the client
  • Make the platforms for each game as accurate as the list above. Make up the prices, addresses and phone numbers, but every price must be different by platform and by location.
  • No two phone numbers in the same city.
  • Each store location must carry at least 3 games. For every game a location carries, price every platform that game comes on.
  • Each game must be available at at least 2 different stores (two Walmart locations count as one store).
  • Every table: a title, sku first, r_ columns that hold numbers only.
Example: writing one table in HTML

Each data table in database.html is one HTML table with a title row, a header row, and one row per record. This made-up pet table only shows the layout. It is not part of your map: your tables, columns and data come from the client information above.

<table border=1 cellpadding=5> <tr><th colspan="3">pet</th></tr> <tr><th>sku</th><th>title</th><th>r_owner</th></tr> <tr><td>1</td><td>Rex</td><td>1</td></tr> <tr><td>2</td><td>Mittens</td><td>1</td></tr> <tr><td>3</td><td>Goldie</td><td>2</td></tr> </table>

What the browser shows:

pet
skutitler_owner
1Rex1
2Mittens1
3Goldie2

colspan must equal the number of columns, or the title bar will not line up with the table below it.

Plan first (on paper or here). List your six core tables: List your three junction tables and the tables each one connects: Which 3 or more games does each of the six store locations carry? Check that every game lands in at least 2 different stores.

Self-check: add up the platforms for every game. Your game-on-platform junction table should have exactly that many rows.

Before you hand it in, check each one

Rules to Remember

  • Store each fact once. If you type it twice, it needs its own table.
  • Every table starts with sku, and every sku is different.
  • Foreign keys hold numbers. r_city = 2, never r_city = Syracuse.
  • One-to-many: put the foreign key on the “many” side. Many-to-many: make a junction table.
  • A junction table can carry data that belongs to the connection itself (the phone for a store in a city, the price for a game at a store).
  • Check your keys: every r_ number must match a sku that exists.

Success Criteria Check-In

Check each one when you feel confident you can do it.

Exit Ticket

Day 1: Name two problems with one big list. Then explain why students and classes need a junction table, but teachers and classes do not. Day 2: Which table was the hardest to place, and what helped you decide where it belonged?