Skip to content

Quiz: Databases and Changing a Live Schema

Test your understanding of tables and keys, transactions, the comparison operator bug, migrations, destructive changes and backups with these review questions.


1. What is a foreign key?

  1. A key that a vendor gives your app to call its API
  2. A column whose value is different in every row of its own table
  3. A sorted lookup structure that makes queries faster
  4. A column in one table that holds the primary key of a row in another table
Show Answer

The correct answer is D. The member_id column in scans is a foreign key. It points at a row in members and creates a relation between the two tables. Its delete rule (refuse, cascade or set to empty) decides what happens to a member's scans when the member is deleted. Option A is an API key. Option B is a primary key. Option C is an index.

Concept Tested: Foreign Key

See: Foreign Keys and Relations


2. What is an idempotent migration?

  1. A migration that leaves the database in the same state whether it runs once or several times
  2. A migration that can never be undone
  3. A migration that runs only on the test database
  4. A migration written by hand instead of by the migration tool
Show Answer

The correct answer is A. ADD COLUMN IF NOT EXISTS succeeds the second time with no change, while a bare ADD COLUMN fails. Data changes can be made safe to repeat with a guard such as WHERE score IS NULL. The dating-safety app's migrations could run twice safely, which is one of the things it got right. Option B describes a destructive migration, the opposite worry.

Concept Tested: Idempotent Migration

See: Idempotent Migrations


3. When a scan finishes, the app writes eight check results and then sets the trust score and status. Why should these writes go in one transaction?

  1. Transactions make each write faster
  2. A transaction encrypts the data while it is written
  3. If the server fails partway, either every write is saved or none is, so a scan can't end up "complete" with missing results
  4. Transactions let several members read the scan at once
Show Answer

The correct answer is C. A transaction groups changes that only make sense together. Without one, a crash after four check results could leave a scan with a score built on half the data, or results with no score. Ask for a transaction whenever two or more writes must agree, with money as the classic case. Transactions aren't about speed (A), encryption (B) or reading at the same time (D).

Concept Tested: Transaction

See: Transactions


4. Why are UUID primary keys called a second lock rather than a replacement for ownership checks?

  1. UUIDs are encrypted, so ownership checks are no longer needed
  2. UUIDs make guessing a neighbor's scan very hard, but a missing ownership check would still hand over any scan whose ID leaks
  3. UUIDs slow down guessing by forcing a wait between requests
  4. UUIDs only work when the database has an index on them
Show Answer

The correct answer is B. With counter keys, /scans/1042 invites a try at 1041, so one missed check exposes every scan in order. UUIDs make that guess nearly impossible, but IDs still appear in links, logs and shared screenshots. The ownership check on every request is the first lock. UUIDs aren't encrypted (A), add no wait (C), and don't depend on an extra index (D).

Concept Tested: Primary Key

See: Worked Example: Counter or UUID?


5. The scheduler sends message 103 and runs UPDATE scheduled_messages SET sent = true WHERE id <= 103. Messages 101 and 102 were still waiting. What happens?

  1. The database rejects the query with an error
  2. Only message 103 is marked sent
  3. The query marks 101 to 103 sent and then sends 101 and 102
  4. All three are marked sent, and 101 and 102 are never delivered
Show Answer

The correct answer is D. <= matches every ID up to 103, so three rows change. Nothing raises an error, and the query reports success. Messages 101 and 102 now look delivered and will never go out. This is the comparison operator bug from the dating-safety app. The fix is WHERE id = 103, plus a check that exactly one row changed and a test with neighboring rows.

Concept Tested: Comparison Operator Bug

See: The Comparison Operator Bug


6. You want to rename the column trust_score to score on a live database. What should you ask the agent to do?

  1. Add score, copy values across and write both, switch reads to score, and drop trust_score last in its own migration
  2. Rename the column in one migration during a quiet hour
  3. Drop trust_score and add score, since the tool will copy the data
  4. Change the name in the Drizzle schema file and run push
Show Answer

The correct answer is A. This is expand and contract, the same plan the school used for its cards. Each step keeps old and new code working. A one-step rename (B) breaks old server code still running during a deploy. Some tools treat a rename as drop-and-add (C), which deletes every score. Using push (D) skips the migration record and creates a second path for schema changes.

Concept Tested: Schema Change

See: Worked Example: Renaming a Score Without Losing It


7. The agent proposes a migration that drops a column it believes is unused. What rules should apply before it runs on production?

  1. Run it right away, since unused columns waste space
  2. Run it with the other migrations in the same file, so it's reviewed once
  3. Mark it destructive, put it in its own migration, take a fresh backup, and have a person approve it
  4. Ask the agent whether it is sure, and run it if it says yes
Show Answer

The correct answer is C. Dropping a column is a one-way door. The data can't come back from the database alone. Naming it and splitting it off makes it easy to review. A fresh backup gives a way back, and a person's approval keeps the agent's guess about "unused" from being the last word. Bundling it (B) hides it among safe changes. An agent's "yes" (D) is a claim, not evidence.

Concept Tested: Destructive Migration

See: Destructive Migrations Are One-Way Doors


8. Your provider takes nightly backups, and nobody has ever restored one. What is the most useful next step?

  1. Switch to hourly backups and trust them
  2. Restore the latest backup into a new, empty database, compare row counts with production, and time how long it took
  3. Restore the backup over production on a quiet night to test it
  4. Download the backup file and check that its size looks right
Show Answer

The correct answer is B. A backup only counts once a restore test has shown it works. Restoring into a separate database checks that the data is complete and the app can use it, and the time it takes tells you how long an outage would last. More frequent backups (A) are still untested. Restoring over production (C) risks the live data. A file size (D) says nothing about whether it restores.

Concept Tested: Restore Test

See: Restore Tests


9. The dating-safety app changed its schema through both the migration tool and hand-written startup SQL. Why did this let the live schema drift from the code?

  1. Startup SQL runs faster than migrations
  2. The migration tool deletes any table it didn't create
  3. Postgres refuses changes from more than one source
  4. Each path changed the database without the other knowing, and the tool trusts only its own tracking table
Show Answer

The correct answer is D. The migration tool decides what to run from its own tracking table. Changes made by startup SQL never appear there, so the tool's picture of the database and the real database pull apart, and neither path notices. The fix is one sentence: every schema change goes through one migration tool. The tool doesn't delete unknown tables (B), and Postgres accepts changes from any source (C).

Concept Tested: Competing Migration Paths

See: Competing Migration Paths


10. A destructive migration ran at 3:15 p.m. Your provider keeps nightly backups and also offers point-in-time recovery. Which restore loses the least?

  1. Point-in-time recovery to 3:14 p.m., which keeps everything up to the minute before the mistake
  2. Last night's backup, since it was tested most recently
  3. A backup from a week ago, to be safe
  4. No restore, and ask members to upload their scans again
Show Answer

The correct answer is A. Last night's backup (B) loses every scan, sign-up and payment since then, about fifteen hours of data. Point-in-time recovery restores the database to its state at a chosen moment within the window, here the minute before the migration. A week-old backup (C) loses far more. Asking members to redo work (D) loses payments and results that can't be rebuilt. Check whether your provider offers it and how far back it reaches.

Concept Tested: Point-in-Time Recovery

See: Point-in-Time Recovery