AI Hints
Home›PostgreSQL Tutorial›Project: Harbor Events, Part 1
PostgreSQL · Lesson 106 of 120

Project: Harbor Events, the Schema

Design the tables.

⏱ 2 min read▶ Video at 1:36:30🎓 Free
Watch this lesson in the PostgreSQL courseWatch on YouTube ↗Full PostgreSQL course

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.

  • Three venues
  • Shows
  • Ticket bookings

The plan

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.

TableKey columns
venuesname, city, capacity
eventsvenue, title, starts_at, price
bookingsevent, email, seats

The schema

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));
Output
CREATE TABLE
CREATE TABLE
CREATE TABLE
Also uses _empty
1

Summary

Three tables, with identity keys, foreign keys and checks, protect Harbor's data from the start. Next, loading data and testing the rules.

Key takeaways
  • Identity
  • Foreign keys
  • CHECK, UNIQUE
Next up: Project: Load and Check — fill it, then test it