Vocabulary for this course

The terms the chapters assume, in plain words. You can open this from any chapter.

alias
A short second name given to a table or column in a query, so the rest of the query can refer to it briefly.
Cartesian product
Every row of one table paired with every row of another, which happens when a join has no matching rule.
column
One named field in a table, with the same kind of value in every row, such as a title or a year.
constraint
A rule a table enforces on its data, such as requiring a value or making one column unique.
distinct
A word in a query that removes repeated rows from the result, so each one appears once.
foreign key
A column that refers to the primary key of another table, so each row can be tied to one row there.
index
A stored lookup that helps the database find rows quickly without reading the whole table.
inner join
The default way of combining two tables: only pairs of rows present on both sides appear in the result.
join
A query operation that combines rows from two tables by matching values they share, usually a shared identifier.
junction table
A bridge between two tables that lets a row on either side pair with many on the other; each of its rows holds one pair.
left join
A combination that keeps every row of the first table named, filling in nulls where the second has no match.
many-to-many
A relationship where one row on each side can pair with many rows on the other, usually held in a junction table.
null
A marker for a value that is missing or unknown, which is not the same as zero or an empty string.
outer join
A join that keeps rows from one or both tables even when no row on the other side matches.
primary key
The column or columns that together identify each row uniquely, and which can never be missing.
query
A request written in SQL that asks the database for an answer, usually a table of rows.
row
One record in a table, with one value for each column that table defines.
schema
The stored plan of a database: its tables, the columns in each, and the rules those columns must follow.
table
A named set of rows that all share the same columns, such as one holding every book in a library.

SQL from the ground up

Adding, changing, removing

INSERT, UPDATE and DELETE: the three statements that change what is stored, the WHERE clause they all need, and how to experiment safely.

Part 7 of 12

Intermediate14 min readBack to Courses

INSERT puts rows in

INSERT INTO books (title, author, year) VALUES ('Dune', 'Frank Herbert', 1965); names the columns it is filling and hands over the values in the same order. One statement can carry several rows: after VALUES, list more parentheses, separated by commas.

Naming the columns is the habit worth keeping. Without the list, the values must line up with the table's storage order — which is invisible in the query, and quietly wrong the day a column is added.

UPDATE and DELETE need a WHERE

UPDATE books SET year = 1965 WHERE title = 'Dune'; changes the rows the condition keeps, and only those. DELETE FROM books WHERE year < 1960; removes the rows it matches. Both read a table and a condition: what to change, and which rows are the ones you mean.

Without a WHERE, both apply to every row in the table — which is occasionally the intent and more often the mistake everyone makes once. The habit that prevents it: write the SELECT with the same condition first, look at what it returns, and change it into the write you meant.

Every write reports how many rows it touched, and that number is worth reading. A delete that reports zero has not failed — it has matched nothing, which usually means the condition is wrong rather than the data being gone. The count is the cheapest check that the statement did what you described, and it is there before the next statement runs.

A run is a fresh start here

Every Run in this course rebuilds the database from setup.sql before executing query.sql. Data you insert is gone on the next run, and updates do not accumulate — which makes the examples safe to experiment with, and makes the write statements behave the same today and tomorrow.

A real database does not work that way, and the same statements are used there — only the safety net of the rebuild is missing. That is the reason for the WHERE habit, not a reason to distrust the statements.

A write is all-or-nothing

A single INSERT, UPDATE or DELETE runs as a transaction even when you do not ask for one: either the whole statement takes effect or none of it does. If a constraint fails halfway through an UPDATE, the rows already changed by that statement are put back.

Statements in a file are separate transactions by default, so a later failure does not undo the earlier statements. Wrap a group in BEGIN and COMMIT when they must succeed together, and use ROLLBACK when you want to abandon the group — that is the safety net a real database gives you, and the one this course's rebuild gives you for free.

A transaction is also why a write can be interrupted safely: the database keeps the old version until the commit, so a crash leaves either the before or the after, never half of each.

What a write sees

UPDATE reads the row as it was at the start of the statement. SET year = year + 1 uses the old year for each row, not a value another row just received, so every row moves exactly once. That is what makes an arithmetic update safe.

A DEFAULT fills a column when the INSERT omits it; an explicit NULL is a value and is stored as one. The same distinction decides whether a NOT NULL column can be left out: it can, if a default supplies the value.

Constraints are checked as each row is written. An INSERT of five rows that fails on the fourth writes none of them, because the statement is one transaction — the count is the row count you asked for, or nothing.

The three writes, each with its condition

INSERT INTO books (title, author, year) VALUES ('The Word for World is Forest', 'Ursula K. Le Guin', 1972);

UPDATE books SET year = 1966 WHERE title = 'Dune';

DELETE FROM books WHERE year < 1960;

Three ways a write surprises you: UPDATE or DELETE with no WHERE, which means every row; a WHERE that matches nothing, where the statement succeeds and changes nothing, so check the count; and deleting a parent row while child rows still point at it, which a database with foreign keys enabled will refuse.

Try these next

  • Write the SELECT for a condition first, then turn that same condition into a DELETE.
  • Copy the rows you are about to delete into an archive table with INSERT INTO ... SELECT.
  • Run the same UPDATE twice and explain why year = year + 1 is not idempotent.

Change the shelves

Add a book of your own, raise one book's year by one, delete everything published before 1960, then show what is left.

Live Preview

A small stock adjustment

Run the file, then move the `INSERT` above the `DELETE` and explain why the desk tray disappears from the final table.

Live Preview

Statements run in the order they are written, each one seeing what the one before it did. A SELECT at the end of the file is how you look at the result of the changes you made above it.

Check what you learned

One question at a time. A wrong answer explains itself — take another go.

Part 1 of 5

What happens to UPDATE books SET year = 2000; with no WHERE clause?