CiTi · Computer Coding · Year One · Unit 4
Student Handout
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.
Type “Bethesda” on every row that needs it. Change one and you have to find them all.
Type “Bethesda” once. Every other table says r_developer = 4 and the number does the pointing.
| Word | Meaning | Also called |
|---|---|---|
| Table | A collection of rows and columns about one kind of thing (store, city, game) | entity, relation |
| Row | One item in the table (one store) | record, tuple |
| Column | One characteristic of every row (title, phone) | field, attribute |
| Relationship | A link between two tables, so you can find data in one that belongs to a row in the other | |
| Primary key | The column that identifies a row. It is unique for every row. We always call it sku. | PK |
| Foreign key | A column that holds the sku of a row in another table. This is the link. | FK |
| Junction table | A table that holds only foreign keys (and sometimes data of its own) to connect two tables in a many-to-many relationship | join table, link table |
sku: a number, unique, never reused.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.”jt1_, jt2_ and name the tables they connect.Here is a store’s game prices kept in one big list. Look for what is repeated.
| 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 |
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)
| sku | first_name | last_name | subject |
|---|---|---|---|
| 1 | Sarah | Johnson | Math |
| 2 | Mark | Davis | English |
| 3 | Priya | Patel | Science |
| 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. Neither table can hold the link, so a junction table does. Each row says “this student is in this class.”
| sku | first_name | last_name |
|---|---|---|
| 1 | Ann | Smith |
| 2 | John | Doe |
| 3 | Emily | Brown |
| sku | title | r_teacher |
|---|---|---|
| 101 | Algebra I | 1 |
| 102 | Geometry | 1 |
| 103 | English II | 2 |
| 104 | Biology | 3 |
| sku | r_student | r_class |
|---|---|---|
| 1 | 1 | 101 |
| 2 | 1 | 103 |
| 3 | 2 | 101 |
| 4 | 2 | 104 |
| 5 | 3 | 101 |
r_class in the student table?
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)| sku | title | abbv |
|---|---|---|
| sku | title | r_state |
|---|---|---|
| sku | title |
|---|---|
| sku | r_store | r_city | address | phone |
|---|---|---|---|---|
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 yourjt1_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.
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.
You must show the price of every game, on every platform, at every store location.
Rules from the clientsku first, r_ columns that hold numbers only.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.
What the browser shows:
| pet | ||
|---|---|---|
| sku | title | r_owner |
| 1 | Rex | 1 |
| 2 | Mittens | 1 |
| 3 | Goldie | 2 |
colspan must equal the number of columns, or the title bar will not line up with the table below it.
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 onesku, and every sku is different.r_city = 2, never r_city = Syracuse.r_ number must match a sku that exists.Check each one when you feel confident you can do it.