← SQL and databases

Add a row

Put a new row into a table with INSERT INTO, then check it with SELECT.

Until now you only looked. A real app also has to save: a new song, a new order, a new sign-up.

Everything so far read data. INSERT INTO adds a row. The queries in this lesson have two parts: the change, then a SELECT so you can see what it did.

The change is kept while you work in the playground. Reset puts the table back as it started.

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
INSERT INTO songs
VALUES (16, 'Compass', 'Kito', 'electronic', 2024, 199, 0);

SELECT COUNT(*) FROM songs;
The songs table has 15 rows. What will the last line show?

RunRun it and compare the result with your prediction.

Run

Run it, then run it again without pressing Reset. Read the message.

Part What it does
INSERT INTO songs Says which table gets the new row
VALUES (...) The new row, one value for each column, in the table's column order
SELECT COUNT(*) Not part of the change: it checks the result

The second run fails because id 16 is already used. A key is never repeated.

lesson.sql
INSERT INTO songs
VALUES (16, 'Compass', 'Kito', 'electronic', 2024, 199, 0);

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

Investigate

lesson.sql
INSERT INTO songs (id, title, artist)
VALUES (16, 'Compass', 'Kito');

SELECT id, title, year FROM songs WHERE id = 16;

Not every column was given a value.

The values and columns must match.

Run the query. What does the year column hold for the new row?

Which line adds a song with title 'Dawn' and artist 'Kito', and nothing else?

ModifyChange the code on purpose and check the result.

Modify

Change the song so it is by Mira Vale instead of Kito.

Try it yourselfYour edits are saved in this browser.
Build up to it
The songs table has the columns id, title, artist, genre, year, seconds, plays. Which VALUES fits the table in that order?

Need a hint?

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

Change the artist and the genre, and keep the id.

Another hint

The artist is 'Mira Vale' and the genre is 'pop'.

Add a song by Mira Vale with id 16, then show it.

    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

    Add two songs of your own, with ids 16 and 17, then run it. The last line shows the new rows.

    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

    Use ids 16 and 17, because the table already has 1 to 15.

    Another hint

    Two INSERT lines, or one INSERT with two sets of values.

    Add two songs of your own with ids 16 and 17.

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

      Now you can
      Next lesson8. Change and remove rowsFix rows with UPDATE, remove them with DELETE, and always say which rows with WHERE.
      For teachers

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

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

      Students will be able to

      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.

      The hard part (on purpose)

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

      When students get stuck

      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.

      Where it leads

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

      What each step is for