Topic 22

UPDATE (and the Missing-WHERE Disaster)

SQL

The projector on screen 2 needs a repair, so tonight's Paper Lanterns is moving to screen 1. In the screenings table that showing is one row — screening 88 — and one value in it is now wrong. For the first time in this book, Lora is not adding data or reading it. She is editing a fact that already exists. The verb is UPDATE.

This page teaches the verb, and then it stages the most famous mistake in all of SQL — slowly, on paper, at zero cost, so that it happens to you here instead of at work. The mistake is small. It is the WHERE clause, missing.

The Anatomy of an UPDATE

An UPDATE has two working parts. SET says what changes — which column, and its new value. WHERE says which rows the change applies to. The WHERE is the same clause you have used since Chapter 3, with the same operators and the same behavior; only the consequence is new. In a SELECT, WHERE chose which rows you saw. Here, it chooses which rows are altered.

Moving screening 88 to screen 1
UPDATE screenings
SET screen = 1
WHERE screening_id = 88;

The database answers with a count: 1 row updated. That count is not decoration. Professionals read it every single time, because it is the first — and often the only — signal that the statement touched exactly what you meant. Expected one, got one: the edit landed, screening 88 now says screen 1, and the old value 2 is simply gone. UPDATE edits in place; it does not keep the previous version anywhere.

The Disaster, in Slow Motion

Now the version this page exists for. Same evening, same intention, same statement. Lora types the first two lines, and between them and the semicolon, the lobby phone rings. She answers it, comes back, sees a statement that looks complete, and sends it.

The mistake — the same UPDATE with the WHERE left off. Do not run this.
UPDATE screenings
SET screen = 1;

The database answers with a count: 212 rows updated.

Every screening the Marquee has ever scheduled is now on screen 1. Tonight's shows. Next week's. The past screenings from March, which were history and were correct until a moment ago. Both screens' entire calendars, folded into one. There was no error message, because from the database's point of view nothing went wrong: an UPDATE with no WHERE means all rows, and it did exactly that, promptly and thoroughly.

Sit with the two counts for a moment. 1 row updated and 212 rows updated came from statements that differ by one missing line. The old screen values are not stored anywhere; there is no undo command waiting. If you have ever pressed Replace All in a document when you meant to fix one selected word, you already know this accident from the inside — find-and-replace did what you said, across everything you pointed it at. WHERE is the selection. That is the whole mapping, and the last time this page needs it.

One missing line — the same statement, two blast radii
With WHEREUPDATE screenings SET screen = 1 WHERE screening_id = 88;
1 row updated. Screening 88 moves to screen 1. Every other row is untouched.
Without WHEREUPDATE screenings SET screen = 1;
212 rows updated. Every screening in the table — past and future, both screens — now says screen 1.

The SELECT-First Reflex

The professional defense is not general caution. It is one specific habit: before running an UPDATE, run a SELECT with the exact WHERE you are about to use, and read what comes back.

The rehearsal — same WHERE, safety on
SELECT screening_id, screen, starts_at
FROM screenings
WHERE screening_id = 88;

One row comes back: screening 88, currently on screen 2, tonight at 18:30. Those are the rows the UPDATE will touch — exactly those, no others. The reason the rehearsal is trustworthy is the identity you noticed earlier: WHERE in UPDATE is WHERE from Chapter 3, unchanged. The SELECT shows precisely what the UPDATE will match, and the number of rows it returns is the count the UPDATE will report. If the SELECT surprises you — two hundred rows instead of one, or zero — you have just been saved, for the price of one extra query.

Had Lora rehearsed her broken statement, the SELECT with no WHERE would have poured all 212 screenings onto her screen. Hard to miss, sitting there, before anything was at stake.

No Confirmation Dialog

Apps have trained you to expect a last chance: "Are you sure?", a red button, a countdown. The database offers none of that. It treats "change all 212 rows" as an ordinary request — because sometimes it is one; end-of-season price changes touch every row on purpose. The tool cannot tell your intentions from your accidents, so it executes what you wrote, immediately. The confirmation step is a habit you bring, not a feature it has.

One honest preview: Chapter 7 adds a real seatbelt — a transaction lets you make a change, inspect the result, and take it back before it becomes permanent. Until then, the SELECT-first reflex is your entire safety system, and it is a good one.

SET Does Arithmetic Too

One more capability rounds out the verb. The new value in SET does not have to be a constant — it can be computed from the row's current values. Lora wants ten percent off every remaining screening of The Long Rain:

Ten percent off — the new price computed from the old one
UPDATE screenings
SET price = price * 0.9
WHERE film_id = 1;

Read price = price * 0.9 as an instruction, not an equation: for each matched row, take that row's current price, multiply it by 0.9, and store the result back into price. A 9.50 screening becomes 8.55; an 11.00 screening becomes 9.90 — each row computes from its own value. This is the everyday shape of real-world updates: raises, discounts, counters ticking up. And notice the WHERE is still there, doing what it always does. The rows it matches are the blast radius; nothing else on this page changes that.

Common Confusions
  • "The database will warn me before a big change." It will not. "All rows" is a perfectly normal request as far as it can tell, and it executes instantly. The warning system is your SELECT-first habit — that is the whole safety net until Chapter 7.
  • "UPDATE adds a new, corrected row and keeps the old one." It edits in place. The old value is overwritten and kept nowhere; recovery is the business of backups and transactions, both coming in Chapter 7.
  • "WHERE probably works differently in UPDATE." It is identical to SELECT's WHERE — same operators, same matching. That identity is exactly why rehearsing with a SELECT tells you the truth about what the UPDATE will do.
Why It Matters
  • The missing-WHERE story is the industry's shared campfire tale — nearly everyone has one, or has cleaned up after one. Hearing it before your first real UPDATE is worth the whole chapter.
  • SELECT-before-UPDATE transfers unchanged into every professional's muscle memory; learn it as a reflex now and it will still be firing decades into your career.
  • Reading the row count after every write is the cheapest verification habit in all of software — one glance, and it catches both too-many and zero.

Knowledge Check

What does UPDATE screenings SET screen = 1; — with no WHERE — actually do?

  • It pauses and asks for confirmation before changing anything
  • It fails with a syntax error because WHERE is required
  • It sets screen to 1 on every row in the table
  • It updates only the most recently inserted row

Why does running a SELECT with the same WHERE first tell you what an UPDATE will do?

  • Because WHERE matches the same rows in SELECT and UPDATE alike
  • Because the SELECT locks the rows so the UPDATE can't miss them
  • Because the database remembers your last SELECT and reuses its rows
  • Because SELECT quietly performs the update in a reversible way

A screening costs 9.50 and another costs 11.00. After UPDATE screenings SET price = price * 0.9 matches both, what are their prices?

  • Both become 0.9, the value written by SET
  • 8.55 and 9.90 — each computed from its own old price
  • Only the cheaper one changes, to 8.55
  • They shrink by 10% again each and every time that they are read

After a successful UPDATE changes screen from 2 to 1, where is the old value 2?

  • In a history column the database maintains automatically
  • In the undo buffer, until you run the UNDO command
  • Nowhere — the edit happened in place and 2 is gone
  • In a duplicate row that now holds the previous version

You expected to update one screening, and the database reports "212 rows updated". What is the most likely explanation?

  • The count is an estimate and can safely be ignored
  • Other users just happened to be editing exactly 211 rows at the same moment
  • The database duplicated the row 212 times by mistake
  • Your WHERE was missing or matched far more rows than you intended

You got correct