← Teacher guide

Add a row

Students add rows with INSERT INTO, match values to columns, meet NULL, and check the change with SELECT.

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 9 min · Investigate 6 min · Modify and Make 10 min

Running the lesson

  1. Predict: most students get this. The point is that a query can now change the table.
  2. Run: the second run fails, which is the lesson on keys: an id belongs to one row. Ask why a table would refuse a repeat.
  3. Investigate: NULL is a new idea. Ask students what a missing year should show.
  4. Reset undoes every change, so students can experiment freely.

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)

Keeping values in column order. Write the column names above the values.

When students get stuck

What you will seeA question to askThe fix
UNIQUE constraint failedHas this id been used already?Choose an id that is not in the table, or press Reset and run once.
table songs has 7 columns but 3 values were suppliedHow many columns does the table have?Give a value for every column, or name the columns you are filling.
Text without quotes.Which values are text?Text values go in single quotes. Numbers do not.

Answers to the checks

  1. Predict: The songs table has 15 rows. What will the last line show?
    16. The INSERT adds one row to the table, and COUNT(*) counts it with the others.
  2. Look at the empty columns: Run the query. What does the year column hold for the new row?
    NULL, which means no value
    Columns you leave out are NULL: not zero, not empty text, just no value.
  3. Which line works?: Which line adds a song with title 'Dawn' and artist 'Kito', and nothing else?
    INSERT INTO songs (title, artist) VALUES ('Dawn', 'Kito');
    Name the columns in brackets, then give the values in the same order.

Solutions

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

Modify

INSERT INTO songs
VALUES (16, 'Compass', 'Mira Vale', 'pop', 2024, 199, 0);

SELECT title, artist FROM songs WHERE id = 16;

Make

INSERT INTO songs VALUES (16, 'Compass', 'Kito', 'electronic', 2024, 199, 0);
INSERT INTO songs VALUES (17, 'Sparks', 'Mira Vale', 'pop', 2024, 190, 0);

SELECT id, title FROM songs WHERE id > 15;

Stretch and support

Stretch: Add a song with a column list that leaves plays out. What does plays hold?

Support: Give the INSERT with one value blank for each task.

Exit ticket

What goes in the brackets after VALUES?

One value for each column, in the table's column order, with text in single quotes.

What each step is for

Where it leads

Next lesson: change and remove rows with UPDATE and DELETE, and why WHERE matters.