← Teacher guide

Design your own table

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

SQL and databases · More than one table · about 25 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

Predict and Run 8 min · Investigate 6 min · Modify and Make 11 min

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.

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)

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

When students get stuck

What you will seeA question to askThe fix
table playlists already existsDid you run the CREATE line twice?Press Reset and run once.
A syntax error near a bracket.Is there a comma between each column, and none after the last?Commas separate columns. The last column has none.
A mismatch of values and columns.How many columns does the table have?Give one value for each column.

Answers to the checks

  1. Predict: What will the last line show?
    One row with three columns. CREATE TABLE makes the table, INSERT adds a row, and SELECT shows it.
  2. Pick the type: A table of songs needs a column for how many plays each song has. Which type?
    INTEGER
    Plays is a whole number, so INTEGER. TEXT would not add up.
  3. Why a key?: Why does every table get an id column as its primary key?
    Two people can share a name, but an id belongs to one row only
    Names repeat, but an id is never repeated, so it is a safe way to point at exactly one row.

Solutions

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

Modify

CREATE TABLE playlists (
  id INTEGER PRIMARY KEY,
  name TEXT,
  owner TEXT,
  description TEXT
);

INSERT INTO playlists VALUES (1, 'Road trip', 'Sam', 'Songs for a long drive');

SELECT * FROM playlists;

Make

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

INSERT INTO gigs VALUES (1, 'Town Hall', 35);
INSERT INTO gigs VALUES (2, 'The Barn', 20);

SELECT * FROM gigs;

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.

Exit ticket

What is a primary key?

A column whose value is different in every row, so each row can be found and pointed at.

What each step is for

Where it leads

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