CiTi · Computer Coding · Year One · Unit 4

Relational Databases: Why Lists Aren’t Enough

Solution: The Game Store Data Map

How to Use This Solution

This is one correct answer, not the only one. Your structure should match: 6 core tables and 3 junction tables, every sku unique, every r_ field holding a number that points at a real row. Your addresses, phone numbers, prices and which games each location carries will be different, and that is fine as long as the client’s rules hold.

Check your own map against it, then fix whatever does not match. The “Why” notes tell you what each table is for, so you can find the idea you missed and not just copy the numbers.

The Map at a Glance

Each arrow starts at a foreign key field (the dot) and ends at the sku of the table it points at. The thick boxes are the three junction tables.

state sku title abbv city sku title r_state store sku title jt1_store_city sku r_store r_city address phone jt1_jt2 sku r_jt1 r_jt2 price jt2_platform_game_developer sku r_platform r_game r_developer platform sku title game sku title developer sku title

Where r_tablename comes from

The r_ prefix is a Hungarian notation naming convention: a short prefix on a name tells you what kind of thing it holds, so you know without looking it up. The idea is named for Charles Simonyi, a Hungarian-born programmer. In our maps r_ stands for relatedto: the field is related to another table, and the rest of the name says which one. r_city reads as “related to city” and holds the sku of a row in city. The same habit gives us jt1_ for junction tables. Name fields this way and anyone reading your map can follow every arrow without a diagram.

Six core tables

state, city, store, platform, developer, game. Each holds one kind of thing, and every fact in it is typed once.

Three junction tables

jt1_store_city, jt2_platform_game_developer, jt1_jt2. They hold the connections, and the data that belongs to a connection.

The Six Core Tables

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

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
store
sku title
1 Walmart
2 Target
3 Best Buy
4 Game Stop
platform
sku title
1 PS4
2 PS5
3 Nintendo
4 XBOX
5 PC
developer
sku title
1 Hello Games
2 Naughty Dog
3 EA
4 Bethesda
game
sku title
1 No Man's Sky
2 Last of Us
3 Apex
4 Starfield
5 Madden
6 Skyrim

Why r_state is in city: one state has many cities, and each city has one state. The foreign key goes on the “many” side, so New York is typed once and Mexico and Syracuse both say 1.

Why platform has Nintendo even though no game uses it: the client listed it. A lookup table stands on its own. A row with no connections yet is fine, and it is ready when a game is added.

Junction Table 1: A Store in a City

Walmart is in two cities, and Mexico, NY has three stores, so store and city have a many-to-many relationship. Each row below is one store location. The address and phone number belong to a store in a city, not to the store or the city alone, so they live here.

jt1_store_city
sku r_store r_city address phone
1 1 1 3217 Main St 555-1111
2 1 2 2701 Erie Blvd 555-2222
3 2 1 81 Church St 666-1111
4 2 2 900 James St 666-2222
5 3 3 4500 Vine St 777-1111
6 4 1 12 Jefferson St 888-1111

Reading it: row 2 is store 1 (Walmart) in city 2 (Syracuse) at 2701 Erie Blvd, phone 555-2222. Client rule check: no two phone numbers share a city, and in fact every location here has its own number. (The addresses are made up.)

Junction Table 2: A Game on a Platform

A game runs on several platforms and a platform runs many games: many-to-many again. Each row means “this game, on this platform, made by this developer.” The rows follow the client’s list exactly: No Man’s Sky, Apex, Madden and Skyrim have four platforms each, Last of Us and Starfield have two, for 20 rows.

jt2_platform_game_developer
sku r_platform r_game r_developer
1 1 1 1
2 2 1 1
3 4 1 1
4 5 1 1
5 1 2 2
6 2 2 2
7 1 3 3
8 2 3 3
9 4 3 3
10 5 3 3
jt2_platform_game_developer (continued)
sku r_platform r_game r_developer
11 4 4 4
12 5 4 4
13 1 5 3
14 2 5 3
15 4 5 3
16 5 5 3
17 1 6 4
18 2 6 4
19 4 6 4
20 5 6 4

This table has 20 rows. It is displayed in two columns here for viewing convenience; read it top to bottom, then on to the next column.

Why the developer is in this table

In this data every game has one developer, so it would be tempting to put r_developer in the game table. But real games often have more than one developer: co-developers, or a port built by another studio. Keeping r_developer here plans for that. A second developer is just one more row, and no table has to change. Spotting that early is what future-proofs a schema. If you put r_developer in game and still ended up with 6 core and 3 junction tables, ask yourself what you would change when a game gets a second developer.

Junction Table 3: The Price

A price belongs to one game, on one platform, at one store location. That is a row from jt1_store_city (the location) paired with a row from jt2_platform_game_developer (the game on a platform). So the price table is a junction of two junctions: r_jt1 points at a store location, r_jt2 points at a game on a platform, and price is the data that belongs to that pairing.

What is stocked where. Each of the six locations carries at least 3 games, and for every game a location carries, every platform that game comes on is priced. That makes 64 price rows.

Store locationGames carriedPrice rows
Walmart, MexicoNo Man's Sky, Apex, Madden12
Walmart, SyracuseLast of Us, Starfield, Skyrim8
Target, MexicoNo Man's Sky, Starfield, Skyrim10
Target, SyracuseLast of Us, Apex, Madden10
Best Buy, CincinnatiNo Man's Sky, Starfield, Madden, Skyrim14
Game Stop, MexicoLast of Us, Apex, Skyrim10
jt1_jt2
sku r_jt1 r_jt2 price
1 1 1 40
2 1 2 50
3 1 3 45
4 1 4 35
5 1 7 20
6 1 8 30
7 1 9 25
8 1 10 15
9 1 13 60
10 1 14 70
11 1 15 65
12 1 16 55
13 2 5 32
14 2 6 42
15 2 11 67
16 2 12 57
jt1_jt2 (continued)
sku r_jt1 r_jt2 price
17 2 17 42
18 2 18 52
19 2 19 47
20 2 20 37
21 3 1 38
22 3 2 48
23 3 3 43
24 3 4 33
25 3 11 63
26 3 12 53
27 3 17 38
28 3 18 48
29 3 19 43
30 3 20 33
31 4 5 33
32 4 6 43
jt1_jt2 (continued)
sku r_jt1 r_jt2 price
33 4 7 23
34 4 8 33
35 4 9 28
36 4 10 18
37 4 13 63
38 4 14 73
39 4 15 68
40 4 16 58
41 5 1 41
42 5 2 51
43 5 3 46
44 5 4 36
45 5 11 66
46 5 12 56
47 5 13 61
48 5 14 71
jt1_jt2 (continued)
sku r_jt1 r_jt2 price
49 5 15 66
50 5 16 56
51 5 17 41
52 5 18 51
53 5 19 46
54 5 20 36
55 6 5 29
56 6 6 39
57 6 7 19
58 6 8 29
59 6 9 24
60 6 10 14
61 6 17 39
62 6 18 49
63 6 19 44
64 6 20 34

The price table is one table of 64 rows. It is displayed in four columns here for viewing convenience; read it top to bottom, then on to the next column.

Reading a row: sku 1 says store location 1 and game-on-platform 1 cost $40. Following the keys: location 1 is Walmart in Mexico, and game-on-platform 1 is No Man's Sky on PS4. Prices are made up and differ by platform and by location.

Follow the Keys

The test of a good map is that you can answer questions using nothing but numbers.

1. What does Last of Us cost on PS4 at the Target in Syracuse?

Target is store 2 and Syracuse is city 2, so in jt1_store_city that is location 4. Last of Us is game 2 and PS4 is platform 1, so in jt2_platform_game_developer that is row 5. Find the row in jt1_jt2 with r_jt1 = 4 and r_jt2 = 5: $33.

2. What does Skyrim cost on PC at the Best Buy in Cincinnati?

Best Buy in Cincinnati is location 5. Skyrim on PC is jt2 row 20. The price row is sku 54: $36.

3. Which stores carry Starfield?

Starfield is game 4. Find its jt2 rows (11, 12), then every jt1_jt2 row whose r_jt2 is one of those, then each row’s r_jt1 location, then the store. Answer: Walmart (Syracuse), Target (Mexico), Best Buy (Cincinnati).

Check Your Map Against the Client’s Rules

GameSold at (different stores)Stores
No Man's SkyWalmart (Mexico), Target (Mexico), Best Buy (Cincinnati)3
Last of UsWalmart (Syracuse), Target (Syracuse), Game Stop (Mexico)3
ApexWalmart (Mexico), Target (Syracuse), Game Stop (Mexico)3
StarfieldWalmart (Syracuse), Target (Mexico), Best Buy (Cincinnati)3
MaddenWalmart (Mexico), Target (Syracuse), Best Buy (Cincinnati)3
SkyrimWalmart (Syracuse), Target (Mexico), Best Buy (Cincinnati), Game Stop (Mexico)4

Mistakes This Map Avoids

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:

<table border=1 cellpadding=5> <tr><th colspan="3">city</th></tr> <tr><th>sku</th><th>title</th><th>r_state</th></tr> <tr><td>1</td><td>Mexico</td><td>1</td></tr> <tr><td>2</td><td>Syracuse</td><td>1</td></tr> <tr><td>3</td><td>Cincinnati</td><td>2</td></tr> </table>

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