Harbor Events sells tickets for shows in three venues by the sea. Jazz nights, comedy, folk sessions. Let's build its database from scratch, using everything from the course.
Venues have a name, a city and a capacity. Events happen at a venue, at a time, with a price. Bookings link a guest's email to an event, with a number of seats.
Identity columns give IDs. Unique stops duplicate venue names. Check rules keep capacity, prices and seats sensible. Foreign keys link bookings to events, and events to venues. And unique event and email stops double bookings. Run it. Three tables, full of rules.
query.sqlsql
CREATE TABLE venues ( venue_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY, name text NOT NULL UNIQUE, city text NOT NULL, capacity integer NOT NULL CHECK (capacity > 0));CREATE TABLE events ( event_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY, venue_id integer NOT NULL REFERENCES venues, title text NOT NULL, starts_at timestamptz NOT NULL, price numeric(6,2) NOT NULL CHECK (price >= 0));CREATE TABLE bookings ( booking_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY, event_id integer NOT NULL REFERENCES events, email text NOT NULL, seats integer NOT NULL CHECK (seats BETWEEN 1 AND 10), booked_at timestamptz NOT NULL DEFAULT now(), UNIQUE (event_id, email));