Year 1 · Unit 4 · Lesson 3

Relational Databases

Why lists aren’t enough

statecitystorejt1_store_city

Day 1: build a data map together     Day 2: build your own

Where are the databases in a video game?

Think of a game you play. What does it need to remember?

Turn and talk

  • Name a game you know
  • List 3 things it has to store
  • Where might a game keep them?

One big list: what breaks?

one_big_list
store city phone game developer platform price
Walmart Mexico 555-1111 Starfield Bethesda XBOX 40
Walmart Mexico 555-1111 Skyrim Bethesda PC 65
Walmart Syracuse 555-2222 Starfield Bethesda XBOX 55
Walmart Syracuse 555-2222 Skyrim Bethesda PC 60
Target Mexico 666-1111 Starfield Bethesda PC 49
Game Stop Mexico 888-1111 Skyrim Bethesda XBOX 25
  1. 1How many times is Bethesda typed?
  2. 2Game Stop becomes GameStop. How many rows do you edit?
  3. 3Target drops its only Starfield row. What else do you lose?

A list vs. a relational database

Flat list

  • Every fact on every row
  • Rename something: find every copy
  • One typo makes a second “Bethesda”
  • Delete a row, lose facts with it

Words to know

Table

One kind of thing: store, city, game

Row

One item in the table (one store)

Column

One characteristic of every row (title, phone)

Primary key

Unique id for the row. We call it sku.

Foreign key

Holds the sku of a row in another table. This is the link.

Junction table

Connects two tables in a many-to-many relationship

Our naming rules

skuFirst column of every table. A number, unique, never reused.
r_tablenamer_ stands for relatedto. r_state is related to state.
jt1_  jt2_Junction tables. Name the tables they connect.
lowercase_onlyUnderscores, no spaces.
city
sku title r_state
1 Mexico 1
2 Syracuse 1
3 Cincinnati 2
state
sku title abbv
1 New York NY
2 Ohio OH

r_state = 1 means related to state 1

One-to-many: the key goes on the “many” side

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

Each class has one teacher: r_teacher

Sarah Johnson (teacher 1): which classes?

Many-to-many needs a junction table

student
sku first_name last_name
1 Ann Smith
2 John Doe
3 Emily Brown
jt1_student_class
sku r_student r_class
1 1 101
2 1 103
3 2 101
4 2 104
5 3 101
class
sku title r_teacher
101 Algebra I 1
102 Geometry 1
103 English II 2
104 Biology 3

A student takes many classes. A class has many students.

A junction table can carry data of its own: the phone number for a store in a city, the price for a game at a store.

Build the store map together

  1. 1state, city, storethe core tables: one-to-many, r_state lives in city
  2. 2jt1_store_citythe junction: many-to-many, address and phone live here

Follow the keys

What is the phone number of the Target in Syracuse?

store + city
Target = store 2
Syracuse = city 2
store 2, city 2
→
jt1_store_city
r_store = 2
r_city = 2
row 4
666-2222

A good map answers questions using nothing but numbers.

Day 2: build your own map

  • database.html in your lessons folder: one HTML table per data table
  • 6 core tables + 3 junction tables
  • Each store location carries 3 or more games, priced on every platform
  • Every game at 2 or more different stores
  • No two phone numbers in the same city

Exit ticket: name two problems with one big list, and say why students and classes need a junction table but teachers and classes do not.

Relational Databases · Why lists aren’t enough

Questions & Discussion

What would you add to the map next?

skur_jt1_