← SQL and databases

Design your own table

Make a table with CREATE TABLE, choose a type for each column, and give every row a key.

Every table you have used was designed by someone. Now you design one.

A table starts as a list of columns. For each one you choose a name and a type: INTEGER for whole numbers, TEXT for text. One column is the primary key: its value is different in every row, so a row can always be found by it.

How this lesson works: Predict, Run, Investigate, Modify, Make
PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

lesson.sql
CREATE TABLE playlists (
  id INTEGER PRIMARY KEY,
  name TEXT,
  owner TEXT
);

INSERT INTO playlists VALUES (1, 'Road trip', 'Sam');

SELECT * FROM playlists;
What will the last line show?

RunRun it and compare the result with your prediction.

Run

Run it and open Tables in this database. Then run it again and read the message.

Part What it does
CREATE TABLE playlists (...) Makes a new table with these columns
name TEXT A column called name that holds text
id INTEGER PRIMARY KEY A whole-number column that is different in every row

The second run fails because the table is already made. Reset starts again.

lesson.sql
CREATE TABLE playlists (
  id INTEGER PRIMARY KEY,
  name TEXT,
  owner TEXT
);

INSERT INTO playlists VALUES (1, 'Road trip', 'Sam');

SELECT * FROM playlists;
Try it yourselfYour edits are saved in this browser.
InvestigateTrace how each line of code connects to what you see.

Investigate

lesson.sql
CREATE TABLE playlists (
  id INTEGER PRIMARY KEY,
  name TEXT,
  owner TEXT
);

INSERT INTO playlists VALUES (1, 'Road trip', 'Sam');

SELECT * FROM playlists;

Numbers you add up are numbers.

What goes wrong if two rows look the same?

A table of songs needs a column for how many plays each song has. Which type?

Why does every table get an id column as its primary key?

ModifyChange the code on purpose and check the result.

Modify

Add a fourth column called description, and fill it in for the playlist.

Try it yourselfYour edits are saved in this browser.
Build up to it
The table is (id INTEGER PRIMARY KEY, name TEXT, owner TEXT). Which row fits it?

Need a hint?

Before a hint: what kind of stuck is this? Try a strategy first

Add description TEXT to the columns, then give the INSERT a fourth value.

Another hint

The new row needs four values in the same order as the columns.

Add a description column to the playlists table and fill it in.

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

    MakeBuild something new using what you have learned. Expect a few tries; that is normal.

    Make

    Design a table called gigs for concerts. Give it a key, a place and a ticket price, add two rows, and show them.

    Try it yourselfYour edits are saved in this browser.
    Need a hint?

    Before a hint: what kind of stuck is this? Try a strategy first

    Name the columns first: id as the key, then a place and a price.

    Another hint

    CREATE TABLE gigs (id INTEGER PRIMARY KEY, place TEXT, price INTEGER);

    Create a table called gigs with a primary key, add two rows and show them.

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

      Now you can
      Next lesson10. Link tables with keysStore another table's id in a column, so a row can point at exactly one row elsewhere.
      For teachers

      Students create a table, choose a type for each column, give it a primary key and fill it with INSERT. About 25 minutes.

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

      Students will be able to

      Running the lesson

      1. Predict: the empty-table option is a good one. A table with no rows still has its columns.
      2. Run: the second run fails. Ask what the database is protecting.
      3. Investigate: ask students for a column that could not be a key (name, year level) and why.
      4. Make: let students choose their own columns before they type. A good table is one you could fill from a real list.

      The hard part (on purpose)

      Choosing columns. Start from a real list on paper and ask what one row is.

      When students get stuck

      Stretch and support

      Stretch: Add a second table of your own and join it to the first.

      Support: Give the CREATE TABLE and ask students to write the INSERT.

      Where it leads

      Next lesson: link two tables with a key, so a review knows which song it is about.

      What each step is for