← Data apps

Two tables that link

Keep playlists and songs in two tables joined by a shared value: each song names the playlist it belongs to. Clicking a playlist shows its songs, found with the same For each and condition as a filter. This is how databases keep data: flat tables, linked by a value they share.

A music app has playlists, and every playlist has songs. Behind the screen, the playlists and the songs are usually kept in two separate tables.

Some apps need two kinds of record: playlists and songs. Instead of putting a list of songs inside each playlist, keep two flat tables and link them:

To show a playlist, go through Songs and keep the rows whose playlist matches. That's a filter, which you already know. Databases work the same way, and the SQL course shows how they search linked tables.

Two tables side by side: Playlists with Study, Gym and Chill; and the Study playlist's songs, each with a playlist field saying Study.

How this lesson works: See it, Build, Check, Make it yours
See itTry the finished thing first, so you know what you are building.

Try the finished app

This is the app you'll build, open on the Run tab. Click each playlist. On the left, open the { } JSON view of Songs: two flat lists of records, linked only by the playlist's name. In the Songs table, click a playlist cell: it's a dropdown of the names in Playlists.

Try it yourselfYour edits are saved in this browser.

BuildWatch the short clip, then do the steps. Your app carries on from the step before.

Build 1: show a playlist's songs

  1. Add an event: An item in PlaylistTable is clicked → Set variable Chosen to ClickedItem.name, then Clear table Shown.
  2. Give it a sub-event: For each row in table Songs, and Compare LoopItem.playlist = Chosen.
  3. Its action: Add a row to table Shown, with each field from LoopItem.
  4. Run it and click each playlist.
Join two tables · Sound on for the voiceover
Try it yourselfYour edits are saved in this browser.

Clicking a playlist sets Chosen to its name, clears Shown, and copies every song whose playlist is Chosen into Shown.

    Run your program. This ticks when your code does all of the above.

    BuildWatch the short clip, then do the steps. Your app carries on from the step before.

    Build 2: a heading with a count

    Add a sub-event with no condition, below the loop: Set Heading text to Chosen & " (" & count(Shown) & " songs)"

    Try it yourselfYour edits are saved in this browser.

    After the loop, Heading shows the playlist's name and how many songs it has.

      Run your program. This ticks when your code does all of the above.

      CheckA quick check that the idea has stuck.

      Check: what links them?

      In Playlists, Gym is renamed Workout. The Songs table isn't changed. What happens when Workout is clicked?

      See itTry the finished thing first, so you know what you are building.

      Look at it nested

      On the left, open { } JSON under Playlists, and choose Songs grouped by playlist. Now each playlist has its songs inside it, the way an app often sends data to another app.

      You can't edit this view: App Builder builds it from the two flat tables each time. The app keeps the tables flat because then each song is stored once, in one place. Change a song's artist and every view of it changes too.

      Try it yourselfYour edits are saved in this browser.

      Make it yoursChange your app on purpose, then try it on someone.

      Make it yours: add to a playlist

      There are boxes for a title and an artist, and an Add to playlist button. Make it add a row to Songs, with Chosen as the playlist. Then click the playlist again: is the new song there?

      Add a song to one playlist, click another playlist, then click back. Is the song still in the right one?

      Try it yourselfYour edits are saved in this browser.

      Add to playlist adds a song to Songs, with Chosen as its playlist.

        Run your program. This ticks when your code does all of the above.

        Now you can
        Next lesson9. Build around a simulationAn Embed shows a ready-made pendulum simulation, made as one web page. The page sends its period and length; the app sends it gravity. Students build the app around it with events: show the period when the page sends it, change gravity with planet buttons, and record readings in a table to answer a science question. The simulation is a component with inputs and outputs, the way real apps use parts other people made.
        For teachers

        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. About 45 minutes.

        Open the teacher guide: answers, solutions, slides and a worksheet.

        Students will be able to

        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.

        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

        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.

        Where it leads

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

        What each step is for