← SQL and databases

Change and remove rows

Fix rows with UPDATE, remove them with DELETE, and always say which rows with WHERE.

One missing WHERE line, and every row in the table changes. Databases do exactly what you ask.

UPDATE changes values in rows that already exist. DELETE removes rows. Both act on every row unless you say which with WHERE.

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
UPDATE songs
SET plays = 0;

SELECT COUNT(*) FROM songs WHERE plays = 0;
This change has no WHERE line. What will the last line show?

RunRun it and compare the result with your prediction.

Run

Run it, then press Reset to put the songs back.

Part What it does
UPDATE songs Says which table to change
SET plays = 0 The new value for a column
WHERE Not here, which is why every row changed
lesson.sql
UPDATE songs
SET plays = 0;

SELECT COUNT(*) FROM songs WHERE plays = 0;
Try it yourselfYour edits are saved in this browser.
InvestigateTrace how each line of code connects to what you see.

Investigate

lesson.sql
DELETE FROM songs
WHERE genre = 'folk';

SELECT COUNT(*) FROM songs;

Check the count after.

Which rows change?

Run the query. How many songs are left after the folk songs are deleted?

Which line adds 100 plays to the song with id 3?

ModifyChange the code on purpose and check the result.

Modify

Song 6 is really from 2018. Change the query so it fixes that and shows the result.

Try it yourselfYour edits are saved in this browser.
Build up to it
Which line sets the genre of song 9 to 'country'?

Need a hint?

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

Change the column to year, the value to 2018 and the id to 6.

Another hint

UPDATE songs SET year = 2018 WHERE id = 6;

Fix song 6 so its year is 2018, then show its title and year.

    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

    Remove the songs that have been played fewer than 2000 times, then show the songs that are left.

    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

    The songs to remove are the ones with fewer than 2000 plays.

    Another hint

    DELETE FROM songs WHERE plays < 2000;

    Delete the songs with fewer than 2000 plays, then show the songs left.

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

      Now you can
      Next lesson9. Design your own tableMake a table with CREATE TABLE, choose a type for each column, and give every row a key.
      For teachers

      Students change rows with UPDATE and remove rows with DELETE, and learn that WHERE decides which rows. About 25 minutes.

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

      Students will be able to

      Running the lesson

      1. Predict: ask students to vote before running. The no-WHERE slip is the key idea of the lesson.
      2. Run: after the 15, press Reset so students see the table come back. In a real database there is no Reset, so people test with SELECT before they UPDATE or DELETE.
      3. Investigate: plays + 100 uses the old value. Ask what plays = 100 would do instead.
      4. Tip to share: write the WHERE as a SELECT first, check the rows, then change SELECT * to DELETE.

      The hard part (on purpose)

      Remembering WHERE. Teach the habit: write it as a SELECT first, then change the first word.

      When students get stuck

      Stretch and support

      Stretch: Raise the plays of every pop song by 10% with plays * 1.1, then look at the results.

      Support: Give the UPDATE with the WHERE line missing and ask students to complete it.

      Where it leads

      Next lesson: designing a table, with keys that make each row unique.

      What each step is for