← Teacher guide

Two tables that link

Optional extension: students keep playlists and songs in two flat tables linked by a shared value (each song's playlist field names its playlist). Clicking a playlist sets Chosen and copies the songs whose playlist = Chosen into a Shown table: a join, built with the filter pattern from lesson 5. The Check shows the link breaking when a name changes in one table only. Make it yours: add a song to the open playlist.

Data apps · Linked tables · about 45 minutes · Open the student lesson

Classroom resources

Open these in Waypoints Prepare with one click. To share a file another way, copy its address and paste it into Load URL, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Try 5 min · Build 1–2 20 min · Check 5 min · Make it yours 15 min

Before class

This is how relational databases keep data: flat tables, linked by a shared value, rather than tables inside tables. Real databases link by an id number that never changes; here the name does the job, which the Check shows is fragile.

The JSON view (the { } JSON button under each table) shows both tables as flat lists of records. Students who go on to Python or web will meet nested JSON, and building it from flat tables is a good exercise there.

The SQL course (code/sql) is the natural next step.

Running the lesson

  1. Before Build 1 (whole class): how would you store playlists and their songs? Collect ideas; then show the two tables.
  2. Check: rename a playlist live on the projector and click it.

In class

On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.

The hard part (on purpose)

Seeing that the link is just a value: open the JSON view of both tables side by side and point to the matching names.

When students get stuck

What you will seeA question to askThe fix
No songs show.What does each song's playlist field say, exactly?The comparison is exact: Study and study are different. Check spelling and capitals in both tables.
Songs from the last playlist stay.Where is Shown cleared?Clear table Shown on the parent event, before the loop.

Answers to the checks

  1. Check: what links them?: In Playlists, Gym is renamed Workout. The Songs table isn't changed. What happens when Workout is clicked?
    No songs show: the songs still say Gym
    The only link between the tables is the name they share. Change it in one table and the songs no longer match. When you rename a playlist in the tables on the left, App Builder changes the songs' playlist field for you, because it's a linked field; an event in your app changing a name doesn't. Real databases give each playlist a number that never changes, and link by that.

Solutions

One possible solution for each task. Other correct programs are fine.

Make it yours: add to a playlist › Add a song

{"version":1,"name":"Linked playlists","device":"web","features":{"level":4},"variables":[{"name":"Playlists","table":{"fields":[{"name":"name","type":"text"}]},"value":[{"name":"Study"},{"name":"Gym"},{"name":"Chill"}]},{"name":"Songs","table":{"fields":[{"name":"title","type":"text"},{"name":"artist","type":"text"},{"name":"playlist","type":"link","to":"Playlists"}]},"value":[{"title":"Blue Sky","artist":"Kai","playlist":"Study"},{"title":"Arc","artist":"Mo","playlist":"Gym"},{"title":"Cove","artist":"Lu","playlist":"Chill"},{"title":"Nova","artist":"Kai","playlist":"Study"},{"title":"Tidal","artist":"Rae","playlist":"Gym"},{"title":"Lanterns","artist":"Mo","playlist":"Study"}]},{"name":"Shown","table":{"fields":[{"name":"title","type":"text"},{"name":"artist","type":"text"},{"name":"playlist","type":"text"}]},"value":[]},{"name":"Chosen","value":""}],"screens":[{"id":"home","name":"Home","objects":[{"name":"Title","kind":"text","instanceVars":[],"instances":[{"id":"h1","x":24,"y":16,"w":500,"h":40,"text":"Playlists","visible":true,"vars":{}}]},{"name":"PlaylistTable","kind":"table","instanceVars":[],"instances":[{"id":"T1","x":24,"y":70,"w":200,"h":200,"text":"","visible":true,"vars":{},"source":"Playlists","columns":""}]},{"name":"Heading","kind":"text","instanceVars":[],"instances":[{"id":"t1","x":250,"y":70,"w":520,"h":36,"text":"Click a playlist","visible":true,"vars":{}}]},{"name":"ShownTable","kind":"table","instanceVars":[],"instances":[{"id":"T2","x":250,"y":112,"w":520,"h":260,"text":"","visible":true,"vars":{},"source":"Shown","columns":"title, artist"}]},{"name":"AllLabel","kind":"text","instanceVars":[],"instances":[{"id":"l1","x":24,"y":290,"w":200,"h":30,"text":"Every song is in Songs.","visible":true,"vars":{}}]},{"name":"TitleInput","kind":"input","instanceVars":[],"instances":[{"id":"i1","x":250,"y":390,"w":200,"h":42,"text":"","visible":true,"vars":{}}]},{"name":"ArtistInput","kind":"input","instanceVars":[],"instances":[{"id":"i2","x":462,"y":390,"w":150,"h":42,"text":"","visible":true,"vars":{}}]},{"name":"AddButton","kind":"button","instanceVars":[],"instances":[{"id":"b1","x":624,"y":388,"w":146,"h":46,"text":"Add to playlist","visible":true,"vars":{}}]}],"events":[{"id":"e1","else":false,"conds":[{"type":"onItemClicked","target":"PlaylistTable"}],"actions":[{"type":"setVar","var":"Chosen","value":"ClickedItem.name"},{"type":"tableClear","table":"Shown"}],"children":[{"id":"e1a","else":false,"conds":[{"type":"forEachRow","table":"Songs"},{"type":"compareValues","left":"LoopItem.playlist","op":"=","right":"Chosen"}],"actions":[{"type":"tableAdd","table":"Shown","values":{"title":"LoopItem.title","artist":"LoopItem.artist","playlist":"LoopItem.playlist"}}],"children":[]},{"id":"e1b","else":false,"conds":[],"actions":[{"type":"setText","target":"Heading","value":"Chosen & \" (\" & count(Shown) & \" songs)\""}],"children":[]}]},{"id":"e2","else":false,"conds":[{"type":"onClicked","target":"AddButton"}],"actions":[{"type":"tableAdd","table":"Songs","values":{"title":"TitleInput.text","artist":"ArtistInput.text","playlist":"Chosen"}}],"children":[]}]}]}

Make it yours: add to a playlist › Check it stays put

Open task: any program that does what the step asks.

Stretch and support

Stretch: Add a Delete playlist button that also removes its songs. Why does that need a loop over Songs too?

Support: Build 1 together; pairs do the heading and Make it yours.

Exit ticket

A school app has a Classes table and a Students table. What field would link each student to their class, and how would you show one class's students?

A class field on each student. For each student, if their class is the chosen class, add them to a results table.

What each step is for

Where it leads

Next: the SQL course, where linked tables are searched with JOIN.