CiTi · Computer Coding · Year One · Unit 4
A lesson within “Building Data-Driven Websites & Client Management”
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.
Demonstrates understanding in:
Database Fundamentals Normalization Rules & Techniques Flat Files vs. Related Data Structures Basic RDMS UnderstandingHands-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
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.
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.
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.
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.
sku unique and every r_ field holding a number.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.
.db and .hdr format). Use it for the vocabulary minutes and as a leave-behind.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.datamap.html); its full text is also in Part G of the handout.one_big_list works). Decide which city each store location goes in before class so the board and the handout match.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.
r_developer = 4. Change the name once and every page that follows the number is right.| Word | Also called | Meaning | In our maps |
|---|---|---|---|
| Table | entity, relation | A collection of rows and columns about one kind of thing | store, city, game |
| Row | record, tuple | One item in the table | One store, one game |
| Column | field, attribute | One characteristic of every row | title, phone |
| Primary key | PK | The column that identifies a row, unique for every row | sku |
| Foreign key | FK | A column holding the primary key of a row in another table; this is the link | r_state |
| Junction table | join table, link table | A table of foreign keys that connects two tables in a many-to-many relationship; it can carry its own data | jt1_store_city |
sku: a number, unique, never reused.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.)jt1_, jt2_ and name the tables they join.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.
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.
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)?
| 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.”
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.
| sku | title | abbv |
|---|---|---|
| 1 | New York | NY |
| 2 | Ohio | OH |
| 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.
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.
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?”
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.
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.
Six tables hold things (store, city, state, platform, developer, game). Three junction tables hold connections. Say the target for tomorrow: 6 core, 3 junction.
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.
Every row needs its own sku. If all three students are sku 1, no foreign key can point at the right one.
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.
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.
r_jt2 = 25 when jt2 only has 20 rows. Teach the check: every r_ number must match a sku in the table it names.
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.
| Time | Phase | What happens |
|---|---|---|
| Day 1 Build together | ||
| 6 min | Hook | The flat list (stage 1). Students answer the three break-it questions in Part B. “What is the same thing typed over and over?” |
| 9 min | Direct Instruction | Part 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 min | Two Relationships | Parts 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 min | Build Together | Stages 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 min | Follow the Keys | Stage 6. One question from you, one from a student, answered by tracing numbers (Part F). |
| 4 min | Exit Ticket & Preview | Handout exit ticket. Read the Day 2 brief aloud and name the target: 6 core tables, 3 junction tables. |
| Day 2 Build alone | ||
| 5 min | Closed-Book Skeleton | Handout 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 min | Plan | Read 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 min | Build | Build 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 min | Key Audit | Swap 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 min | Client Swap | The 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 min | Closure | Publish 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). |
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.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.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.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):
| Points | Check |
|---|---|
| 3 | Six core tables (store, city, state, platform, developer, game), each with a unique sku |
| 2 | State and city handled as one-to-many with r_state (names typed once) |
| 4 | Three junction tables, with r_ fields that hold numbers only |
| 2 | Address and phone live in the store-city junction; no two phone numbers in the same city |
| 3 | Price table joins a store location row to a game-on-platform row; prices differ by platform and location |
| 3 | Each 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 |
| 2 | Every foreign key number matches a row that exists |
| 1 | Each 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.