← All Lessons Student Handout → Solution → Slides →

CiTi · Computer Coding · Year One · Unit 4

Relational Databases: Why Lists Aren’t Enough

A lesson within “Building Data-Driven Websites & Client Management”

Program Computer Coding: Year 1
Unit 4: Building Data-Driven Websites & Client Management
Duration Two class periods: build together, then build alone
Grouping Whole class, then individual
Project Game Store Data Map (9 tables)

Essential Question

From the Year 1 curriculum map
What are databases? What should be stored in DBs vs. files?
What is a relational database and what advantages do they offer?
What is normalization and why do we need it?

Every site so far stores its information in pages: type it, save it, repeat it. This lesson shows why that breaks as soon as the same fact appears twice, and gives students the fix: break the information into small tables that point at each other by number. Students leave able to read a data map and draw one.

Curriculum Map Alignment

Verbatim from Yr 1 curriculum

Concept / Knowledge / Content: Relational Databases

  • Explain how a relational database differs from other databases
  • Describe the purpose of primary keys and foreign keys

Normalization

  • Explain normalized data
  • Define un-normalized data
  • Demonstrate proficiency in normalization of data by labeling and organizing within a database to avoid mistakes and coding confusion

Skills / Tasks

Demonstrates understanding in:

Database Fundamentals Normalization Rules & Techniques Flat Files vs. Related Data Structures Basic RDMS Understanding
Standards touched: ITPD01.10.01 Explain database development processes · ITPD01.10.02 Create, populate, and maintain a database. Builds on: HTML tables from Table Usage (students draw their data map as tables). Leads to: real database files and searches, and Year 2 entity mapping.

Program Connections

Skills & Portrait alignment

Program Skills: What I’m Building Toward

Hands-On:

#4 Design a relational database
#6 Work a problem through Define→Prepare→Try→Reflect

Professional:

#5 Work through ambiguity
#9 Document as you go

NYS Portrait of a Graduate

Critical Thinker Creative Innovator Academically Prepared

Guaranteed & Viable Curriculum Framework

3 Dimensions for this lesson

Integrated Academic Knowledge

Sets and relationships: one-to-many (one teacher, many classes) and many-to-many (many students, many classes). Using an id number as a stand-in for a name, the way a function uses a variable. Counting rows to check that a design is complete.

Employability Skills

Reading a client brief and turning it into a plan before building. Checking your own work against stated requirements (two phone numbers never share a city; every game is at two stores). Explaining a design choice to someone who did not make it.

Technical Skills

Naming the parts of a table (table, row, column, primary key, foreign key, junction table); spotting repeated data in a flat list; splitting a flat list into lookup tables and junction tables; following foreign keys across tables to answer a question; and laying the finished map out as HTML tables in database.html.

Learning Intention

“Unwrapped” from the standard

Students will understand that a relational database stores each fact once and connects tables with keys, and will use that idea to turn a client’s flat list of stores, cities, platforms and games into a normalized set of linked tables.

Success Criteria

  • I can name two problems with keeping everything in one big list.
  • I can say what a table, row, column, primary key and foreign key are.
  • I can tell a one-to-many relationship from a many-to-many one, and I know when I need a junction table.
  • I can follow foreign keys from table to table to answer a question.
  • I can turn a written brief into a set of tables with every sku unique and every r_ field holding a number.
  • I can check my data map against the brief and fix what is missing.

Authenticity Continuum

Where this lesson sits today
Instruction & Skill Practice
Teacher-Designed Simulation
Realistic Industry Task
Authentic Audience / Client
Product, Service, Impact, Solution

The brief is written like a client request (“show me the price of every game on every platform at every store location”), but the stores and prices are made up. Push further: students swap data maps and a classmate, acting as the client, asks three questions the map must be able to answer. Or students map a real list they care about (a team roster, a music library, the school store) and present the design.

Materials & Prep

  • Classroom slide show (12 slides, in your browser): arrow keys or click to step through, N shows speaker notes, F goes full screen. Slides 2 to 9 are Day 1; slide 10 previews Day 2, and slide 11 is the questions and discussion close.
  • Student Handout (y1-u4-l3-so): vocabulary, the build-together tables, a keys-tracing exercise, the assignment brief, and the exit ticket. Answers save in the browser.
  • Concept sheet: the one-page Databases PDF (tables, rows, columns, relationships, primary and foreign keys, the WebDNA tab-delimited .db and .hdr format). Use it for the vocabulary minutes and as a leave-behind.
  • Build-together page: your finished data map (databases.html). Reveal it one table at a time, or draw the same tables on the board as the class decides each one. Do not hand students the full page before the assignment.
  • Assignment brief: the Day 2 task (datamap.html); its full text is also in Part G of the handout.
  • Projector, whiteboard, Sublime Text, CyberDuck, Chrome
  • Teacher prep: have a flat list of about 8 rows ready to show (the handout’s one_big_list works). Decide which city each store location goes in before class so the board and the handout match.

How It Works

The ideas and the naming rules

A flat list (a spreadsheet, a text file, one big HTML table) keeps every fact on every row. A relational database keeps each fact in one place and uses a number to point at it.

Flat listStarfield’s developer is typed on every row that mentions Starfield. Rename one and you must find them all. One typo makes a second “Bethesda.”
RelationalBethesda is typed once, in the developer table. Everything else says r_developer = 4. Change the name once and every page that follows the number is right.
WordAlso calledMeaningIn our maps
Tableentity, relationA collection of rows and columns about one kind of thingstore, city, game
Rowrecord, tupleOne item in the tableOne store, one game
Columnfield, attributeOne characteristic of every rowtitle, phone
Primary keyPKThe column that identifies a row, unique for every rowsku
Foreign keyFKA column holding the primary key of a row in another table; this is the linkr_state
Junction tablejoin table, link tableA table of foreign keys that connects two tables in a many-to-many relationship; it can carry its own datajt1_store_city

Naming rules (our class convention)

  • Every table’s first column is sku: a number, unique, never reused.
  • A foreign key is r_ plus the table it points at: r_state. (r_ stands for “relatedto”. The prefix is Hungarian notation: a short prefix that tells you what kind of thing the name holds.)
  • A foreign key holds the number, never the name.
  • Junction tables start jt1_, jt2_ and name the tables they join.
  • All lowercase, underscores only.

Which relationship?

One-to-many: a teacher has many classes, a class has one teacher. Put r_teacher in class. No new table.

Many-to-many: a student has many classes and a class has many students. Neither table can hold the link, so a junction table does.

Connection to WebDNA: in WebDNA a table is a tab-separated .db file with a .hdr file listing the column names. The maps students draw this week are the plans for real .db files later.

Day 1: The Build

Seven stages, one table at a time

Work backward from a mess. Students propose the next table before you reveal it. The handout’s Part E has blank tables that match stages 2 to 5.

1

Show the flat list and break it

Put the six-row list on the board. Ask three things: How many times is Bethesda typed? What do you edit if Game Stop becomes GameStop? What do you lose if Target drops its only Starfield row (its phone number)?

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

Say: “Repeated data, update mistakes, and facts that die with a row. A relational database fixes all three.”

2

State and city: the first one-to-many

Pull state out (New York and Ohio are typed once), then city with r_state. One state, many cities; the foreign key lives on the “many” side.

state
sku title abbv
1 New York NY
2 Ohio OH
city
sku title r_state
1 Mexico 1
2 Syracuse 1
3 Cincinnati 2

Ask: “What state is Syracuse in?” Follow r_state = 1 over to state. Say it out loud: foreign key, find the matching sku.

3

Store and city: the first many-to-many

Walmart is in two cities and Syracuse has more than one store, so neither table can hold the link. Create store, then jt1_store_city with r_store and r_city. Ask where each store’s address and phone number belong. They belong to a store in a city, so they live in the junction table.

Key idea: a junction table can carry its own data. Rule from the brief: no two phone numbers in the same city.

4

Platform, game, developer: another junction

A game runs on several platforms and a platform runs many games: many-to-many again, so jt2_platform_game_developer. Each row means “this game on this platform, made by this developer.” Starfield has two rows (XBOX, PC); Madden has four.

Why the developer is in the junction table: in our data every game has one developer, but real games often have several (co-developers, or a port by another studio). Putting r_developer in the junction table plans for that: a second developer is just another row, and no table changes. Ask: “What would we have to rebuild if Starfield’s PC version had a second studio?”

5

The price: a junction of two junctions

A price belongs to one game on one platform at one store location. That is a row from jt1 paired with a row from jt2. Build jt1_jt2 with r_jt1, r_jt2 and price. This is the hardest idea of the day; draw arrows from the foreign keys to the rows they name.

6

Follow the keys

Ask the class a real question and answer it with arrows only: “What does Last of Us cost on PS4 at the Target in Syracuse?” Start in jt1_jt2, follow r_jt1 to the store location, r_jt2 to the game and platform, then out to the title tables. Run a second question yourself, then a student runs a third.

7

Count and name

Six tables hold things (store, city, state, platform, developer, game). Three junction tables hold connections. Say the target for tomorrow: 6 core, 3 junction.

The Tricky Parts

What students get wrong

1Names in a foreign key column

A student writes Syracuse in r_city instead of 2. Then the name is repeated again and nothing has been gained. Foreign keys hold the sku number only.

2Repeated sku values

Every row needs its own sku. If all three students are sku 1, no foreign key can point at the right one.

3Junction table where a lookup table belongs (or the reverse)

Teacher → class is one-to-many: r_teacher on class, no junction. Student ↔ class is many-to-many: junction. If a student can name how many on each side, they can pick.

4Price attached to the wrong thing

A price on game gives one price everywhere. A price on jt1 ignores the platform. It needs the store location and the game-on-platform together.

5Foreign keys that point at nothing

r_jt2 = 25 when jt2 only has 20 rows. Teach the check: every r_ number must match a sku in the table it names.

6The two-phone rule and the two-store rule

Students build the structure and forget the brief’s conditions: no two phone numbers in the same city; each store location carries at least 3 games, priced on every platform the game comes on; and every game at two or more different stores (two Walmart locations count as one store). Make these a final pass on the checklist. Expect about 40 to 70 price rows.

Lesson Flow

Two periods, ~50 min each
TimePhaseWhat happens
Day 1 Build together
6 minHookThe flat list (stage 1). Students answer the three break-it questions in Part B. “What is the same thing typed over and over?”
9 minDirect InstructionPart A vocabulary from the concept sheet: table, row, column, relationship, primary and foreign key. Naming rules. Peek at the WebDNA .db and .hdr files.
8 minTwo RelationshipsParts C and D: teacher/class (one-to-many) and student/class (many-to-many with a junction table). Students answer the questions under each example.
17 minBuild TogetherStages 2 to 5, with students filling in Part E as the class decides each table. Stop after every table: “What would break if we left this out?”
6 minFollow the KeysStage 6. One question from you, one from a student, answered by tracing numbers (Part F).
4 minExit Ticket & PreviewHandout exit ticket. Read the Day 2 brief aloud and name the target: 6 core tables, 3 junction tables.
Day 2 Build alone
5 minClosed-Book SkeletonHandout closed. On blank paper, students list the nine table names and each table’s columns from memory, then check against Part E and fix gaps. No Day 1 map on screen.
5 minPlanRead the brief. Students decide which 3 or more games each of the six store locations carries (Define, Prepare). Each game needs at least 2 different stores, so check that before building.
22 minBuildBuild database.html in the lessons folder, one HTML table per data table, titled with its name. Core tables first (fast), then the junctions, then the prices. Their own prices, addresses and phone numbers. Self-check: jt2 has exactly 20 rows.
6 minKey AuditSwap with a partner. Check that every r_ number matches a row that exists in the table it names, and that no sku repeats. Fix what the partner finds.
8 minClient SwapThe partner acts as the client and asks three questions the map must answer (for example: cheapest Skyrim on PC in Syracuse? which stores carry Starfield?). The builder answers by tracing keys; anything the map cannot answer is a design flaw to fix (Try, Reflect).
4 minClosurePublish to the server. Reflection on the handout: which table was hardest to place and why. Early finishers: add a sale price column or a ninth junction (stores to the platforms they carry).

Teacher Watch List

  • Day 1 and Day 2 use the same data on purpose. Day 2 rebuilds the map from the brief with the student’s own prices, addresses and phone numbers, and is more complete than the Day 1 build (every game a location carries is priced on every platform). The closed-book skeleton opens Day 2 so students retrieve the design before they see it. Keep the full map off student screens until after Day 2.
  • Why at least 3 games per location. Pricing every game on every platform at every location would be 120 price rows. Three or more games per location keeps it to roughly 40 to 70 rows and still forces every game to be priced across its platforms.
  • Developer placement is deliberate. r_developer lives in jt2_platform_game_developer because a game can have more than one developer, and this future-proofs the schema. Students who put it on game still get 6 core and 3 junction tables; accept it, then ask what happens when a game has two developers.
  • Lowercase names. The handout and plan use lowercase field and table names (sku, title, abbv) to match the class file-naming rule. Your key page uses SKU, Title, r_State; students may copy either style, so grade on structure.
  • Pace. Stage 5 is where students stall. If it takes too long, give the structure (r_jt1, r_jt2, price) and let them fill in the rows tomorrow. Day 2 is tight on price rows; anyone unfinished completes them at home.

Assessment

Formative (Day 1): Circulate during Part E and check that foreign key columns hold numbers and every sku is unique. Exit ticket: “Name two problems with one big list. Then say why a student/class relationship needs a junction table but teacher/class does not.”

Summative (Day 2): database.html. Suggested 20-point checklist (adjust to your gradebook):

PointsCheck
3Six core tables (store, city, state, platform, developer, game), each with a unique sku
2State and city handled as one-to-many with r_state (names typed once)
4Three junction tables, with r_ fields that hold numbers only
2Address and phone live in the store-city junction; no two phone numbers in the same city
3Price table joins a store location row to a game-on-platform row; prices differ by platform and location
3Each store location carries at least 3 games, each priced on every platform it comes on; every game at two or more different stores; platforms match the brief
2Every foreign key number matches a row that exists
1Each table titled with its name, file named database.html in the lessons folder

Solution document: hand out the Solution (y1-u4-l3-sol) after Day 2 work is in. It shows the full nine-table map, with a complete price table under the 3-games-per-location rule, three worked key traces, and a rules check. Students compare their own map to it and fix mismatches.

Extension: Add a second price column for a sale price, or a ninth junction connecting stores to the platforms they carry. Students explain which table changes and why nothing else has to.