Skip to content

Rows by Hand

You'll create a table and fill it yourself with INSERT, fix it with UPDATE, trim it with DELETE, and find out why WHERE is the most important word in all three.

About

Reading a database is half of SQL, and this badge is the other half. You'll make one table about something you own or do, like the books on a shelf, the recipes you cook or the climbs you've done, and type thirty rows in by hand. That sounds tedious and is exactly the point, because typing rows teaches you what a column is in a way importing never does. Along the way you'll run an UPDATE with no WHERE clause on purpose, watch every row change, and take it back with a transaction. That is the mistake everyone makes once in real life, and it's better to make it first on a table that doesn't matter. What counts is a table you created, rows you typed, and a change you undid.

At a glance

Per requirement
15 XP
Completion bonus
75 XP
Requirements
6
Takes
One evening
Skill level
Novice (never tried it)
Cost
Free
Supplies
Nothing special
Where
At home
Other people
On your own

Requirements

  1. 1
    Create a table with four columns and a primary key. Something like CREATE TABLE books (id INTEGER PRIMARY KEY, title TEXT NOT NULL, author TEXT, year INTEGER). Pick singular or plural table names and stick with it, because mixing them is the small mess that follows you for years. Take a screenshot of the CREATE statement and the empty table in the Browse Data tab or in .schema. Photo
  2. 2
    Insert thirty rows by hand from real things around you. Use the multi-row form: INSERT INTO books (title, author, year) VALUES ('...', '...', 1999), ('...', '...', 2004), and so on. Leave the id out and let the database number them. The first apostrophe in a title or a name like O'Brien ends the string early and breaks the statement, and the fix is to double it, as in 'O''Brien'. Take a screenshot of SELECT COUNT(*) returning 30 and a few of the rows. Photo
  3. 3
    Fix five rows with UPDATE, each with a WHERE on the id. Something like UPDATE books SET year = 2004 WHERE id = 12. Run a SELECT with the same WHERE first, so you see exactly which rows are about to change before they do. That habit, select first and then update, is the one people who do this for a living never skip. Take a screenshot showing the select, the update and the changed row. Photo
  4. 4
    Run an UPDATE with no WHERE inside a transaction, watch every row change, and roll it back. Type BEGIN; then UPDATE books SET author = 'oops'; then SELECT * FROM books; then ROLLBACK; then the SELECT again. Every author changes, and then every author comes back. In DB Browser, the Write Changes and Revert Changes buttons are the same idea with a different name. This is the single most expensive mistake in databases and you have now made it for free. Film the sequence, or take screenshots before, during and after. Photo or video
  5. 5
    Delete rows with a WHERE and count what's left. Something like DELETE FROM books WHERE year < 1950, or whatever fits your table. Run COUNT(*) before and after, and the difference should match what a SELECT with the same WHERE showed you first. Take a screenshot of the three statements and their results. Photo
  6. 6
    Export the table to CSV, open it in a spreadsheet, and say which tool you'd rather use for what. In DB Browser it's File, Export, Table as CSV. Open the file in Sheets, Excel or Calc. Take a screenshot of the CSV open, and in your write-up give two sentences on what the spreadsheet does better and what the table does better. Photo