Skip to content
Lab 4 of 4

Transactions & Rollback: Booking Without Double-Booking

Build a campus room-booking flow that can never leave the data half-done. Book a seat atomically, make the database refuse an impossible state, prove ROLLBACK undoes a failed booking, and stop the same student booking a slot twice.
  • Intermediate
  • 45 min
  • SQL
  • Transactions
  • Constraints
  • DBMS

Overview

A booking is two writes: add a row to bookings, and take one seat off the slot. Do only one of them and the system lies — a booking with no seat taken, or a seat gone with nobody booked.

In this experiment you work on a small campus booking database (rooms, slots, bookings). You will make the two writes happen together or not at all with a transaction, have the database itself refuse a negative seat count, watch a failed booking get rolled back completely, and add a rule that stops double-booking.

Keep notes as you go in Your work — what you tried, what the database said, and why. They stay with this attempt after the machine is gone, and go into its printable practical record along with the SQL file you save at the end.

What you will learn

  • Read the state of a booking system with a join and a view
  • Make two writes atomic with BEGIN / COMMIT
  • Make the database enforce an invariant with a CHECK constraint
  • Show that ROLLBACK leaves no partial row after a failure
  • Prevent duplicate bookings with a UNIQUE constraint

Before you start

  • Indexes, Transactions & EXPLAIN first, or comfort with UPDATE and INSERT
  • You can run queries in psql

Tasks

  1. 01See the bookings at a glance
  2. 02Book a seat atomically
  3. 03Make an impossible state impossible
  4. 04Watch ROLLBACK undo a failed booking
  5. 05Stop double-booking
  6. 06Save your booking transaction