Skip to content

One Character, Many Rows

Run the One Character, Many Rows MicroSim Fullscreen

About This MicroSim

A scheduler sends message 103 and then marks it as sent. If the update says WHERE id <= 103 instead of WHERE id = 103, it also marks 101 and 102, which never went out. Nothing raises an error. Only the data shows the damage.

This sim lets you try five operators against the same illustrative queue. Each run shows the rows affected and an "expect 1 row" check, the cheap habit that catches this bug.

Learning objective (Apply: execute): Execute a WHERE condition against a six-row message queue and predict exactly which rows it changes, for five operators.

How to Use

  1. Read the condition in the dark box.
  2. Click the rows you think the update will mark as sent.
  3. Press Run. Marked rows turn green. Rows marked by mistake get a red outline and the words "wrongly marked."
  4. Press Next round for the next operator. Your first run of each round is scored.
  5. Tick Use a one-row test queue and run rounds 1 and 2 again. With only row 103, the two conditions can't be told apart.
  6. Press Reset to clear your score and start over.

Iframe Embed Code

You can add this MicroSim to any web page by adding this to your HTML:

<iframe src="https://arunbatchu.github.io/good-enough-engineering/sims/comparison-operator-rows/main.html"
        height="509px"
        width="100%"
        scrolling="no"></iframe>

Lesson Plan

Grade Level

Adult professional (Good Enough Engineering)

Duration

10-15 minutes

Prerequisites

Row, primary key, SQL query, WHERE clause, UPDATE, rows affected and the comparison operator bug, from the chapter sections before the sim.

Activities

  1. Exploration (5 min): Play all five rounds and note the rows affected for each.
  2. Guided Practice (5 min): Switch on the one-row test queue and compare rounds 1 and 2. Write a three-row test that would catch the bug.
  3. Assessment (5 min): Reach 4 of 5. Then find one update in your own code meant to change one row and check it verifies the count.

Common Misconceptions

  • An update that runs without error changed only the row you meant.
  • A test with one row proves the condition is right.
  • Rows already marked sent can't be harmed by a wider update.

Assessment

  • Predicts the marked rows correctly in 4 of 5 rounds on the first attempt.
  • States why the expect-1-row check catches rounds 2 to 5.
  • Explains why id = 103 and id <= 103 look the same with a one-row test queue.

References

  1. PostgreSQL documentation: UPDATE - the command's output reports how many rows it changed.
  2. PostgreSQL documentation: Comparison operators - lists the operators =, <, <=, >, >= and <>.