Event Ticketing ER diagram and database schema

The Event Ticketing database schema is a DB Planner template with 27 tables, 203 columns and 56 relationships, free to use. Core tables include events, hold_items, holds, order_fees. It can be opened as an interactive ER diagram or exported as PostgreSQL, MySQL, or SQL Server DDL.

ER diagram of the Event Ticketing database schema, showing 27 tables and their relationships
FIG. 1: entity relationship diagram, 27 tables
Tables
27
Columns
203
Relationships
56
Category
Marketplace
Exports
DBML, SQL, Prisma, C#
Price
Free

What this schema models

A complete event ticketing system database design, built for the moment thousands of people press buy at once: 27 tables, 16 enums and 56 relationships covering organizers and staff, venues and seat maps (sections, rows and seats), events and performances, ticket types and price tiers, per-performance seat and general admission inventory, expiring cart holds, orders with frozen prices and fees, promo codes, payments and itemized refunds, tickets with unique barcodes, transfers and append-only door scans. THE FOUR DESIGN RULES 1. Inventory is per PERFORMANCE, one row per seat. performance_seats holds every sellable seat of every performance. Claiming seats is one conditional UPDATE (only where the seat is available or its hold has expired), and if fewer rows change than were requested, the transaction rolls back. The database decides who got the seat, so two buyers can never both win it; a partial unique index on order items is a second guard. 2. Holds expire by timestamp, not by a cleanup job. A cart stamps hold_expires_at on its seats, and an expired hold is simply claimable by the next buyer. A sweeper can tidy up for reporting, but correctness never depends on it running. 3. General admission is a counter with a CHECK. performance_sections stores capacity and sold_count with CHECK (sold_count <= capacity). A GA claim locks that one row, subtracts quantities in unexpired holds and inserts its hold item, so the last place cannot be sold twice even if application code is wrong. 4. An ORDER ITEM is a sale; a TICKET is a credential. Price, fees and refunds attach to the order item and are frozen at checkout. The ticket carries the barcode, and a transfer voids it and issues a new ticket with a new barcode, so a screenshot of the old one fails at the door. DETAILS WORTH STEALING - Seat maps are venue configurations (end stage, in the round), and seats carry x/y coordinates plus accessible and obstructed-view flags for drawing the map. - Prices are a matrix: ticket type (Adult, Child, VIP) by price tier (Orchestra A, Balcony C), and a seat can move to a different tier per performance for weekend pricing. - Killed seats (camera positions, sightlines) and house holds are statuses on the performance inventory, never deletions from the map. - Promo codes are case-insensitive, capped by a redemption counter with a CHECK, and can unlock hidden ticket types for presales. - refund_items make partial refunds exact: return one ticket of four and only that one is covered, with fee refundability decided at sale. - Scans record every read, rejects included, keeping the device clock and the server receipt time apart, because door scanners work offline and sync later. WHAT YOU GET Clean DBML that imports directly into DB Planner, and a laid-out board with zones color-coded and framed: accounts and organizers, venues and seat maps, events and pricing, inventory and holds, orders and payments, and tickets and entry. Every table carries a note. Postgres-flavoured types that port cleanly to MySQL or SQL Server. Useful for developers building a box office, ticketing platform or event app, and for students who need a realistic event ticketing system ER diagram for a DBMS project. This is a schema design reference; taxes, consumer refund rules and payment processing belong in your own business logic.

Tables (27)

Every table and its columns

users

Buyers, ticket holders and organizer staff. A transfer recipient gets a row when they accept.

Columns of the users table
ColumnTypeConstraintsReferencesDescription
iduuidPK
emailvarchar(254)unique, not null
full_namevarchar(200)
phonevarchar(30)
marketing_opt_in_attimestamptzConsent as a timestamp, so you can prove when it was given
created_attimestamptznot null, default `now()`

organizers

Promoters, theatres, clubs and teams that put on events. Settlement and payouts are per organizer.

Columns of the organizers table
ColumnTypeConstraintsReferencesDescription
iduuidPK
namevarchar(200)not null
slugvarchar(80)unique, not null
currencychar(3)not nullSettlement currency for payouts
payout_account_tokenvarchar(200)Payment processor connected-account id; bank details never live here
created_attimestamptznot null, default `now()`

organizer_members

Organizer staff. Box office can sell and refund; door staff can only scan.

Columns of the organizer_members table
ColumnTypeConstraintsReferences
organizer_iduuidFK, not nullorganizers.id
user_iduuidFK, not nullusers.id
roleorganizer_rolenot null
added_attimestamptznot null, default `now()`

venues

Physical places. Shared across organizers, since many promoters book the same hall.

Columns of the venues table
ColumnTypeConstraintsReferencesDescription
iduuidPK
namevarchar(200)not null
address_line1varchar(200)not null
cityvarchar(120)not null
regionvarchar(120)
postal_codevarchar(20)
countrychar(2)not null
timezonevarchar(64)not nullIANA zone. Show times are displayed in it, wherever the buyer is.

seat_maps

One configuration of a venue. The same arena seats 18,000 for a concert and 6,000 for a theatre setup.

Columns of the seat_maps table
ColumnTypeConstraintsReferencesDescription
iduuidPK
venue_iduuidFK, not nullvenues.id
namevarchar(120)not nulle.g. End stage, In the round, Standing floor
total_capacityintegernot null
is_activebooleannot null, default true

sections

Areas of a seat map. A reserved section has rows and seats; a general admission section has only a capacity.

Columns of the sections table
ColumnTypeConstraintsReferencesDescription
iduuidPK
seat_map_iduuidFK, not nullseat_maps.id
namevarchar(100)not nulle.g. Orchestra, Balcony, Floor
kindsection_kindnot null
ga_capacityintegerGeneral admission only: how many people fit. Null for reserved sections.
sort_ordersmallintnot null

Indexes: (seat_map_id, name) unique

seat_rows

Rows within a reserved section.

Columns of the seat_rows table
ColumnTypeConstraintsReferencesDescription
iduuidPK
section_iduuidFK, not nullsections.id
labelvarchar(10)not nulle.g. K, AA. Not always alphabetical, so order by sort_order.
sort_ordersmallintnot null

Indexes: (section_id, label) unique

seats

The physical seat. It belongs to the map; whether it is for sale tonight, and at what price, belongs to performance_seats.

Columns of the seats table
ColumnTypeConstraintsReferencesDescription
iduuidPK
row_iduuidFK, not nullseat_rows.id
labelvarchar(10)not nulle.g. 12, 101. Odd and even sides are common.
xnumeric(8,2)not nullPosition on the drawn seat map
ynumeric(8,2)not null
is_accessiblebooleannot null, default false
is_obstructedbooleannot null, default falseRestricted view, disclosed before purchase

Indexes: (row_id, label) unique

events

The show, match or conference as marketed. A three-night run is one event with three performances.

Columns of the events table
ColumnTypeConstraintsReferencesDescription
iduuidPK
organizer_iduuidFK, not nullorganizers.id
titlevarchar(300)not null
slugvarchar(120)unique, not null
descriptiontext
categoryevent_categorynot null
min_agesmallintNull for all ages
currencychar(3)not null
statusevent_statusnot null, default 'draft'
published_attimestamptz
created_attimestamptznot null, default `now()`

performances

One date and time of an event. Inventory, tickets and door scans are always per performance.

Columns of the performances table
ColumnTypeConstraintsReferencesDescription
iduuidPK
event_iduuidFK, not nullevents.id
seat_map_iduuidFK, not nullseat_maps.idWhich venue and configuration; it can differ between dates of a run
starts_attimestamptznot null
doors_open_attimestamptz
on_sale_attimestamptznot null
off_sale_attimestamptz
statusperformance_statusnot null, default 'scheduled'
cancelled_attimestamptz

Indexes: (event_id, starts_at)

ticket_types

Who the ticket is for, and what it includes.

Columns of the ticket_types table
ColumnTypeConstraintsReferencesDescription
iduuidPK
event_iduuidFK, not nullevents.id
namevarchar(100)not nulle.g. Adult, Child, Student, VIP package
descriptionvarchar(500)
is_hiddenbooleannot null, default falseOnly offered when a promo code unlocks it, e.g. a presale
min_per_ordersmallintnot null, default 1
max_per_ordersmallintnot null, default 8Anti-scalping limit
sales_start_attimestamptz
sales_end_attimestamptz
sort_ordersmallintnot null

price_tiers

Seat quality bands. Each seat or GA section is placed in a tier per performance.

Columns of the price_tiers table
ColumnTypeConstraintsReferencesDescription
iduuidPK
event_iduuidFK, not nullevents.id
namevarchar(100)not nulle.g. Orchestra A, Balcony C, GA Floor
colorchar(7)not nullSeat map legend color
sort_ordersmallintnot null

ticket_prices

The price matrix: ticket type by price tier. A Child ticket in Balcony C has its own price.

Columns of the ticket_prices table
ColumnTypeConstraintsReferencesDescription
ticket_type_iduuidFK, not nullticket_types.id
price_tier_iduuidFK, not nullprice_tiers.id
face_value_centsintegernot null
service_fee_centsintegernot null, default 0Per-ticket booking fee shown before checkout

performance_seats

Per-performance seat inventory, one row per sellable seat, created when the performance goes on sale. A seat is claimed by one conditional UPDATE: SET status = held, hold_id = $1, hold_expires_at = now() + 10 minutes WHERE id = ANY($2) AND (status = available OR (status = held AND hold_expires_at < now())). If fewer rows changed than were asked for, someone else got a seat first and the transaction rolls back. Two buyers can never both win the same seat.

Columns of the performance_seats table
ColumnTypeConstraintsReferencesDescription
iduuidPK
performance_iduuidFK, not nullperformances.id
seat_iduuidFK, not nullseats.id
price_tier_iduuidFK, not nullprice_tiers.idTiers can differ per night, e.g. Saturday pricing
statusseat_statusnot null, default 'available'
hold_iduuidFKholds.id
hold_expires_attimestamptzA held seat whose hold has expired is available again, with no job needed
updated_attimestamptznot null, default `now()`

Indexes: (performance_id, seat_id) unique, (performance_id, status), (hold_id)

performance_sections

General admission inventory for one performance. CHECK (sold_count <= capacity). A GA claim locks this one row (SELECT ... FOR UPDATE), subtracts quantities in unexpired holds, and inserts its hold item; checkout increments sold_count. Expired holds stop counting the moment they expire, and the CHECK refuses an oversell even if app code is wrong.

Columns of the performance_sections table
ColumnTypeConstraintsReferencesDescription
iduuidPK
performance_iduuidFK, not nullperformances.id
section_iduuidFK, not nullsections.id
price_tier_iduuidFK, not nullprice_tiers.id
capacityintegernot nullUsually the section ga_capacity, lowered for a smaller show or a safety limit
sold_countintegernot null, default 0

Indexes: (performance_id, section_id) unique

holds

A cart. Seats and GA places are reserved for a few minutes while the buyer checks out, then quietly lapse.

Columns of the holds table
ColumnTypeConstraintsReferencesDescription
iduuidPK
user_iduuidFKusers.idNull for a guest checkout
session_tokenvarchar(100)not nullTies the hold to a browser session before login
statushold_statusnot null, default 'active'
expires_attimestamptznot nullTypically 10 to 15 minutes; extended once when payment starts
created_attimestamptznot null, default `now()`

hold_items

What is in the cart. Exactly one of performance_seat_id or performance_section_id is set.

Columns of the hold_items table
ColumnTypeConstraintsReferencesDescription
iduuidPK
hold_iduuidFK, not nullholds.id
ticket_type_iduuidFK, not nullticket_types.id
performance_seat_iduuidFKperformance_seats.idReserved seating: one item per seat
performance_section_iduuidFKperformance_sections.idGeneral admission: one item with a quantity
quantitysmallintnot null, default 1

Indexes: (hold_id), (performance_section_id)

promo_codes

Discounts and presale access codes.

Columns of the promo_codes table
ColumnTypeConstraintsReferencesDescription
iduuidPK
organizer_iduuidFK, not nullorganizers.id
event_iduuidFKevents.idNull for a code valid on every event of the organizer
codevarchar(40)not null
discount_kinddiscount_kindnot null
discount_valueintegernot null, default 0Basis points for percent (1500 = 15%), cents for amount, 0 for unlock only
unlocks_ticket_type_iduuidFKticket_types.idReveals a hidden ticket type, e.g. a fan club presale
max_redemptionsinteger
redemption_countintegernot null, default 0Incremented in the checkout transaction; CHECK (redemption_count <= max_redemptions)
valid_fromtimestamptz
valid_untiltimestamptz

Indexes: (`upper(code)`, organizer_id) unique

orders

A purchase. Every total is the sum of frozen lines below it, so a later price change never alters a paid order.

Columns of the orders table
ColumnTypeConstraintsReferencesDescription
iduuidPK
order_numbervarchar(20)unique, not nullShort, human-readable, given to support over the phone
user_iduuidFKusers.id
buyer_emailvarchar(254)not nullSnapshot; guests have no user row
hold_iduuidFK, uniqueholds.idThe cart this order was checked out from
promo_code_iduuidFKpromo_codes.id
statusorder_statusnot null, default 'pending_payment'
currencychar(3)not null
subtotal_centsintegernot null
fees_centsintegernot null
discount_centsintegernot null, default 0
tax_centsintegernot null, default 0
total_centsintegernot null
placed_attimestamptz
created_attimestamptznot null, default `now()`

order_items

One admission sold: the commercial record. Price, fees and refunds attach here. The barcode lives on the ticket, which can be reissued.

Columns of the order_items table
ColumnTypeConstraintsReferencesDescription
iduuidPK
order_iduuidFK, not nullorders.id
performance_iduuidFK, not nullperformances.id
ticket_type_iduuidFK, not nullticket_types.id
price_tier_iduuidFK, not nullprice_tiers.id
performance_seat_iduuidFKperformance_seats.idNull for general admission
performance_section_iduuidFKperformance_sections.idSet for general admission
seat_labelvarchar(100)Snapshot, e.g. Orchestra, Row K, Seat 12, so remapping a venue never changes a sold ticket
face_value_centsintegernot nullPrice snapshot at checkout
discount_centsintegernot null, default 0
statusorder_item_statusnot null, default 'active'

Indexes: (order_id), (performance_seat_id)

order_fees

Every fee line frozen at checkout: service, facility, processing, delivery. The fees shown before payment are exactly these rows.

Columns of the order_fees table
ColumnTypeConstraintsReferencesDescription
iduuidPK
order_iduuidFK, not nullorders.id
order_item_iduuidFKorder_items.idSet for per-ticket fees, null for per-order fees
kindfee_kindnot null
amount_centsintegernot null
is_refundablebooleannot nullDecided at sale, not at refund time

payments

Charges against an order. A failed attempt is a row too, which fraud review needs.

Columns of the payments table
ColumnTypeConstraintsReferencesDescription
iduuidPK
order_iduuidFK, not nullorders.id
amount_centsintegernot null
currencychar(3)not null
statuspayment_statusnot null
processor_referencevarchar(100)unique, not nullCard data stays with the payment processor
created_attimestamptznot null, default `now()`

refunds

Money returned. A cancelled performance produces one refund per order, issued in bulk.

Columns of the refunds table
ColumnTypeConstraintsReferencesDescription
iduuidPK
order_iduuidFK, not nullorders.id
payment_iduuidFK, not nullpayments.id
amount_centsintegernot null
reasonrefund_reasonnot null
statuspayment_statusnot null, default 'pending'
requested_byuuidFKusers.idBox office staff, or null for an automatic cancellation refund
processor_referencevarchar(100)
created_attimestamptznot null, default `now()`

refund_items

Which admissions a refund covers, so returning one ticket of four is exact and voids only that ticket.

Columns of the refund_items table
ColumnTypeConstraintsReferencesDescription
refund_iduuidFK, not nullrefunds.id
order_item_iduuidFK, not nullorder_items.id
amount_centsintegernot nullFace value plus any refundable fees for this admission

tickets

The credential, not the sale. A transfer or reissue voids this row and issues a new ticket with a new barcode, so a screenshot of the old one fails at the door, while the order item (and its refund rights) stays with the original buyer.

Columns of the tickets table
ColumnTypeConstraintsReferencesDescription
iduuidPK
order_item_iduuidFK, not nullorder_items.id
performance_iduuidFK, not nullperformances.idCopied from the order item so the door lookup is one index hit
holder_user_iduuidFKusers.id
holder_namevarchar(200)
barcodevarchar(64)unique, not nullRandom and unguessable, never sequential
statusticket_statusnot null, default 'valid'
replaces_ticket_iduuidFKtickets.idThe ticket this one was reissued from after a transfer
issued_attimestamptznot null, default `now()`
voided_attimestamptz

Indexes: (order_item_id), (performance_id, status)

ticket_transfers

Sending a ticket to a friend. The original stays valid until the recipient accepts, so an unclaimed transfer never strands a ticket.

Columns of the ticket_transfers table
ColumnTypeConstraintsReferencesDescription
iduuidPK
from_ticket_iduuidFK, not nulltickets.id
to_ticket_iduuidFKtickets.idIssued on acceptance
from_user_iduuidFK, not nullusers.id
to_emailvarchar(254)not null
to_user_iduuidFKusers.idSet when the recipient accepts and signs in
statustransfer_statusnot null, default 'pending'
created_attimestamptznot null, default `now()`
expires_attimestamptznot null
accepted_attimestamptz

scans

Append-only: every read at the door, including rejected ones. The first admitted scan lets the holder in; later ones are logged as already_used, and the gate and time of the first are on record, which ends the argument at the turnstile.

Columns of the scans table
ColumnTypeConstraintsReferencesDescription
iduuidPK
performance_iduuidFK, not nullperformances.id
ticket_iduuidFKtickets.idNull when the barcode matched nothing
barcode_readvarchar(64)not nullExactly what the scanner read
resultscan_resultnot null
gatevarchar(40)
device_idvarchar(64)not null
scanned_byuuidFKusers.id
scanned_attimestamptznot nullDevice clock; scanners keep working offline and sync later
received_attimestamptznot null, default `now()`

Indexes: (ticket_id, scanned_at), (performance_id, scanned_at)

Enums (16)

organizer_role
owner, manager, box_office, door_staff
section_kind
reserved, general_admission
event_category
concert, theatre, sports, comedy, conference, festival, other
event_status
draft, published, cancelled, completed
performance_status
scheduled, on_sale, sold_out, postponed, cancelled, completed
seat_status
available, held, sold, killed, house_hold
hold_status
active, converted, released, expired
discount_kind
percent, amount, unlock_only
order_status
pending_payment, paid, partially_refunded, refunded, cancelled
order_item_status
active, refunded, cancelled
fee_kind
service, facility, processing, delivery
payment_status
pending, succeeded, failed
refund_reason
event_cancelled, customer_request, duplicate_order, fraud, goodwill
ticket_status
valid, used, transferred, void
transfer_status
pending, accepted, cancelled, expired
scan_result
admitted, already_used, wrong_performance, void_ticket, unknown_barcode

Relationships (56)

  • events.organizer_id references organizers.id many to one
  • hold_items.hold_id references holds.id many to one
  • hold_items.performance_seat_id references performance_seats.id many to one
  • hold_items.performance_section_id references performance_sections.id many to one
  • hold_items.ticket_type_id references ticket_types.id many to one
  • holds.user_id references users.id many to one
  • order_fees.order_id references orders.id many to one
  • order_fees.order_item_id references order_items.id many to one
  • order_items.order_id references orders.id many to one
  • order_items.performance_id references performances.id many to one
  • order_items.performance_seat_id references performance_seats.id many to one
  • order_items.performance_section_id references performance_sections.id many to one
  • order_items.price_tier_id references price_tiers.id many to one
  • order_items.ticket_type_id references ticket_types.id many to one
  • orders.hold_id references holds.id many to one
  • orders.promo_code_id references promo_codes.id many to one
  • orders.user_id references users.id many to one
  • organizer_members.organizer_id references organizers.id many to one
  • organizer_members.user_id references users.id many to one
  • payments.order_id references orders.id many to one
  • performance_seats.hold_id references holds.id many to one
  • performance_seats.performance_id references performances.id many to one
  • performance_seats.price_tier_id references price_tiers.id many to one
  • performance_seats.seat_id references seats.id many to one
  • performance_sections.performance_id references performances.id many to one
  • performance_sections.price_tier_id references price_tiers.id many to one
  • performance_sections.section_id references sections.id many to one
  • performances.event_id references events.id many to one
  • performances.seat_map_id references seat_maps.id many to one
  • price_tiers.event_id references events.id many to one
  • promo_codes.event_id references events.id many to one
  • promo_codes.organizer_id references organizers.id many to one
  • promo_codes.unlocks_ticket_type_id references ticket_types.id many to one
  • refund_items.order_item_id references order_items.id many to one
  • refund_items.refund_id references refunds.id many to one
  • refunds.order_id references orders.id many to one
  • refunds.payment_id references payments.id many to one
  • refunds.requested_by references users.id many to one
  • scans.performance_id references performances.id many to one
  • scans.scanned_by references users.id many to one
  • scans.ticket_id references tickets.id many to one
  • seat_maps.venue_id references venues.id many to one
  • seat_rows.section_id references sections.id many to one
  • seats.row_id references seat_rows.id many to one
  • sections.seat_map_id references seat_maps.id many to one
  • ticket_prices.price_tier_id references price_tiers.id many to one
  • ticket_prices.ticket_type_id references ticket_types.id many to one
  • ticket_transfers.from_ticket_id references tickets.id many to one
  • ticket_transfers.from_user_id references users.id many to one
  • ticket_transfers.to_ticket_id references tickets.id many to one
  • ticket_transfers.to_user_id references users.id many to one
  • ticket_types.event_id references events.id many to one
  • tickets.holder_user_id references users.id many to one
  • tickets.order_item_id references order_items.id many to one
  • tickets.performance_id references performances.id many to one
  • tickets.replaces_ticket_id references tickets.id many to one

Event Ticketing database schema in DBML

The schema as it is written: DBML, the text format DB Planner draws its diagrams from. Copy it into any DBML tool, or open it in the diagram above.

event-ticketing.dbml dbml
// Event Ticketing
// Zones (header colors): accounts and organizers (gray), venues and seat maps (teal),
// events and pricing (blue), inventory and holds (amber), orders and payments (purple),
// tickets and entry (coral).

// ---------------------------------------------------------------------------
// ACCOUNTS AND ORGANIZERS
// ---------------------------------------------------------------------------

Table users [headercolor: #90a4ae] {
  id uuid [pk]
  email varchar(254) [not null, unique]
  full_name varchar(200)
  phone varchar(30)
  marketing_opt_in_at timestamptz [note: 'Consent as a timestamp, so you can prove when it was given']
  created_at timestamptz [not null, default: `now()`]
  Note: 'Buyers, ticket holders and organizer staff. A transfer recipient gets a row when they accept.'
}

Table organizers [headercolor: #90a4ae] {
  id uuid [pk]
  name varchar(200) [not null]
  slug varchar(80) [not null, unique]
  currency char(3) [not null, note: 'Settlement currency for payouts']
  payout_account_token varchar(200) [note: 'Payment processor connected-account id; bank details never live here']
  created_at timestamptz [not null, default: `now()`]
  Note: 'Promoters, theatres, clubs and teams that put on events. Settlement and payouts are per organizer.'
}

Table organizer_members [headercolor: #90a4ae] {
  organizer_id uuid [not null, ref: > organizers.id]
  user_id uuid [not null, ref: > users.id]
  role organizer_role [not null]
  added_at timestamptz [not null, default: `now()`]

  indexes {
    (organizer_id, user_id) [pk]
  }

  Note: 'Organizer staff. Box office can sell and refund; door staff can only scan.'
}

// ---------------------------------------------------------------------------
// VENUES AND SEAT MAPS
// ---------------------------------------------------------------------------

Table venues [headercolor: #4cd0b0] {
  id uuid [pk]
  name varchar(200) [not null]
  address_line1 varchar(200) [not null]
  city varchar(120) [not null]
  region varchar(120)
  postal_code varchar(20)
  country char(2) [not null]
  timezone varchar(64) [not null, note: 'IANA zone. Show times are displayed in it, wherever the buyer is.']
  Note: 'Physical places. Shared across organizers, since many promoters book the same hall.'
}

Table seat_maps [headercolor: #4cd0b0] {
  id uuid [pk]
  venue_id uuid [not null, ref: > venues.id]
  name varchar(120) [not null, note: 'e.g. End stage, In the round, Standing floor']
  total_capacity integer [not null]
  is_active boolean [not null, default: true]
  Note: 'One configuration of a venue. The same arena seats 18,000 for a concert and 6,000 for a theatre setup.'
}

Table sections [headercolor: #4cd0b0] {
  id uuid [pk]
  seat_map_id uuid [not null, ref: > seat_maps.id]
  name varchar(100) [not null, note: 'e.g. Orchestra, Balcony, Floor']
  kind section_kind [not null]
  ga_capacity integer [note: 'General admission only: how many people fit. Null for reserved sections.']
  sort_order smallint [not null]

  indexes {
    (seat_map_id, name) [unique]
  }

  Note: 'Areas of a seat map. A reserved section has rows and seats; a general admission section has only a capacity.'
}

Table seat_rows [headercolor: #4cd0b0] {
  id uuid [pk]
  section_id uuid [not null, ref: > sections.id]
  label varchar(10) [not null, note: 'e.g. K, AA. Not always alphabetical, so order by sort_order.']
  sort_order smallint [not null]

  indexes {
    (section_id, label) [unique]
  }

  Note: 'Rows within a reserved section.'
}

Table seats [headercolor: #4cd0b0] {
  id uuid [pk]
  row_id uuid [not null, ref: > seat_rows.id]
  label varchar(10) [not null, note: 'e.g. 12, 101. Odd and even sides are common.']
  x numeric(8,2) [not null, note: 'Position on the drawn seat map']
  y numeric(8,2) [not null]
  is_accessible boolean [not null, default: false]
  is_obstructed boolean [not null, default: false, note: 'Restricted view, disclosed before purchase']

  indexes {
    (row_id, label) [unique]
  }

  Note: 'The physical seat. It belongs to the map; whether it is for sale tonight, and at what price, belongs to performance_seats.'
}

// ---------------------------------------------------------------------------
// EVENTS AND PRICING
// ---------------------------------------------------------------------------

Table events [headercolor: #4c8bf5] {
  id uuid [pk]
  organizer_id uuid [not null, ref: > organizers.id]
  title varchar(300) [not null]
  slug varchar(120) [not null, unique]
  description text
  category event_category [not null]
  min_age smallint [note: 'Null for all ages']
  currency char(3) [not null]
  status event_status [not null, default: 'draft']
  published_at timestamptz
  created_at timestamptz [not null, default: `now()`]
  Note: 'The show, match or conference as marketed. A three-night run is one event with three performances.'
}

Table performances [headercolor: #4c8bf5] {
  id uuid [pk]
  event_id uuid [not null, ref: > events.id]
  seat_map_id uuid [not null, ref: > seat_maps.id, note: 'Which venue and configuration; it can differ between dates of a run']
  starts_at timestamptz [not null]
  doors_open_at timestamptz
  on_sale_at timestamptz [not null]
  off_sale_at timestamptz
  status performance_status [not null, default: 'scheduled']
  cancelled_at timestamptz

  indexes {
    (event_id, starts_at)
  }

  Note: 'One date and time of an event. Inventory, tickets and door scans are always per performance.'
}

Table ticket_types [headercolor: #4c8bf5] {
  id uuid [pk]
  event_id uuid [not null, ref: > events.id]
  name varchar(100) [not null, note: 'e.g. Adult, Child, Student, VIP package']
  description varchar(500)
  is_hidden boolean [not null, default: false, note: 'Only offered when a promo code unlocks it, e.g. a presale']
  min_per_order smallint [not null, default: 1]
  max_per_order smallint [not null, default: 8, note: 'Anti-scalping limit']
  sales_start_at timestamptz
  sales_end_at timestamptz
  sort_order smallint [not null]
  Note: 'Who the ticket is for, and what it includes.'
}

Table price_tiers [headercolor: #4c8bf5] {
  id uuid [pk]
  event_id uuid [not null, ref: > events.id]
  name varchar(100) [not null, note: 'e.g. Orchestra A, Balcony C, GA Floor']
  color char(7) [not null, note: 'Seat map legend color']
  sort_order smallint [not null]
  Note: 'Seat quality bands. Each seat or GA section is placed in a tier per performance.'
}

Table ticket_prices [headercolor: #4c8bf5] {
  ticket_type_id uuid [not null, ref: > ticket_types.id]
  price_tier_id uuid [not null, ref: > price_tiers.id]
  face_value_cents integer [not null]
  service_fee_cents integer [not null, default: 0, note: 'Per-ticket booking fee shown before checkout']

  indexes {
    (ticket_type_id, price_tier_id) [pk]
  }

  Note: 'The price matrix: ticket type by price tier. A Child ticket in Balcony C has its own price.'
}

// ---------------------------------------------------------------------------
// INVENTORY AND HOLDS
// ---------------------------------------------------------------------------

Table performance_seats [headercolor: #f5b74c] {
  id uuid [pk]
  performance_id uuid [not null, ref: > performances.id]
  seat_id uuid [not null, ref: > seats.id]
  price_tier_id uuid [not null, ref: > price_tiers.id, note: 'Tiers can differ per night, e.g. Saturday pricing']
  status seat_status [not null, default: 'available']
  hold_id uuid [ref: > holds.id]
  hold_expires_at timestamptz [note: 'A held seat whose hold has expired is available again, with no job needed']
  updated_at timestamptz [not null, default: `now()`]

  indexes {
    (performance_id, seat_id) [unique]
    (performance_id, status) [note: 'Seat map availability']
    hold_id
  }

  Note: '''Per-performance seat inventory, one row per sellable seat, created when the performance goes on sale. A seat is claimed by one conditional UPDATE: SET status = held, hold_id = $1, hold_expires_at = now() + 10 minutes WHERE id = ANY($2) AND (status = available OR (status = held AND hold_expires_at < now())). If fewer rows changed than were asked for, someone else got a seat first and the transaction rolls back. Two buyers can never both win the same seat.'''
}

Table performance_sections [headercolor: #f5b74c] {
  id uuid [pk]
  performance_id uuid [not null, ref: > performances.id]
  section_id uuid [not null, ref: > sections.id]
  price_tier_id uuid [not null, ref: > price_tiers.id]
  capacity integer [not null, note: 'Usually the section ga_capacity, lowered for a smaller show or a safety limit']
  sold_count integer [not null, default: 0]

  indexes {
    (performance_id, section_id) [unique]
  }

  Note: '''General admission inventory for one performance. CHECK (sold_count <= capacity). A GA claim locks this one row (SELECT ... FOR UPDATE), subtracts quantities in unexpired holds, and inserts its hold item; checkout increments sold_count. Expired holds stop counting the moment they expire, and the CHECK refuses an oversell even if app code is wrong.'''
}

Table holds [headercolor: #f5b74c] {
  id uuid [pk]
  user_id uuid [ref: > users.id, note: 'Null for a guest checkout']
  session_token varchar(100) [not null, note: 'Ties the hold to a browser session before login']
  status hold_status [not null, default: 'active']
  expires_at timestamptz [not null, note: 'Typically 10 to 15 minutes; extended once when payment starts']
  created_at timestamptz [not null, default: `now()`]
  Note: 'A cart. Seats and GA places are reserved for a few minutes while the buyer checks out, then quietly lapse.'
}

Table hold_items [headercolor: #f5b74c] {
  id uuid [pk]
  hold_id uuid [not null, ref: > holds.id]
  ticket_type_id uuid [not null, ref: > ticket_types.id]
  performance_seat_id uuid [ref: > performance_seats.id, note: 'Reserved seating: one item per seat']
  performance_section_id uuid [ref: > performance_sections.id, note: 'General admission: one item with a quantity']
  quantity smallint [not null, default: 1]

  indexes {
    hold_id
    performance_section_id
  }

  Note: 'What is in the cart. Exactly one of performance_seat_id or performance_section_id is set.'
}

// ---------------------------------------------------------------------------
// ORDERS AND PAYMENTS
// ---------------------------------------------------------------------------

Table promo_codes [headercolor: #b98cf0] {
  id uuid [pk]
  organizer_id uuid [not null, ref: > organizers.id]
  event_id uuid [ref: > events.id, note: 'Null for a code valid on every event of the organizer']
  code varchar(40) [not null]
  discount_kind discount_kind [not null]
  discount_value integer [not null, default: 0, note: 'Basis points for percent (1500 = 15%), cents for amount, 0 for unlock only']
  unlocks_ticket_type_id uuid [ref: > ticket_types.id, note: 'Reveals a hidden ticket type, e.g. a fan club presale']
  max_redemptions integer
  redemption_count integer [not null, default: 0, note: 'Incremented in the checkout transaction; CHECK (redemption_count <= max_redemptions)']
  valid_from timestamptz
  valid_until timestamptz

  indexes {
    (organizer_id, `upper(code)`) [unique, note: 'Codes are case-insensitive']
  }

  Note: 'Discounts and presale access codes.'
}

Table orders [headercolor: #b98cf0] {
  id uuid [pk]
  order_number varchar(20) [not null, unique, note: 'Short, human-readable, given to support over the phone']
  user_id uuid [ref: > users.id]
  buyer_email varchar(254) [not null, note: 'Snapshot; guests have no user row']
  hold_id uuid [unique, ref: > holds.id, note: 'The cart this order was checked out from']
  promo_code_id uuid [ref: > promo_codes.id]
  status order_status [not null, default: 'pending_payment']
  currency char(3) [not null]
  subtotal_cents integer [not null]
  fees_cents integer [not null]
  discount_cents integer [not null, default: 0]
  tax_cents integer [not null, default: 0]
  total_cents integer [not null]
  placed_at timestamptz
  created_at timestamptz [not null, default: `now()`]
  Note: 'A purchase. Every total is the sum of frozen lines below it, so a later price change never alters a paid order.'
}

Table order_items [headercolor: #b98cf0] {
  id uuid [pk]
  order_id uuid [not null, ref: > orders.id]
  performance_id uuid [not null, ref: > performances.id]
  ticket_type_id uuid [not null, ref: > ticket_types.id]
  price_tier_id uuid [not null, ref: > price_tiers.id]
  performance_seat_id uuid [ref: > performance_seats.id, note: 'Null for general admission']
  performance_section_id uuid [ref: > performance_sections.id, note: 'Set for general admission']
  seat_label varchar(100) [note: 'Snapshot, e.g. Orchestra, Row K, Seat 12, so remapping a venue never changes a sold ticket']
  face_value_cents integer [not null, note: 'Price snapshot at checkout']
  discount_cents integer [not null, default: 0]
  status order_item_status [not null, default: 'active']

  indexes {
    order_id
    performance_seat_id [note: 'Partial unique index WHERE status = active: a seat is sold at most once, a second guard behind the claim']
  }

  Note: 'One admission sold: the commercial record. Price, fees and refunds attach here. The barcode lives on the ticket, which can be reissued.'
}

Table order_fees [headercolor: #b98cf0] {
  id uuid [pk]
  order_id uuid [not null, ref: > orders.id]
  order_item_id uuid [ref: > order_items.id, note: 'Set for per-ticket fees, null for per-order fees']
  kind fee_kind [not null]
  amount_cents integer [not null]
  is_refundable boolean [not null, note: 'Decided at sale, not at refund time']
  Note: 'Every fee line frozen at checkout: service, facility, processing, delivery. The fees shown before payment are exactly these rows.'
}

Table payments [headercolor: #b98cf0] {
  id uuid [pk]
  order_id uuid [not null, ref: > orders.id]
  amount_cents integer [not null]
  currency char(3) [not null]
  status payment_status [not null]
  processor_reference varchar(100) [not null, unique, note: 'Card data stays with the payment processor']
  created_at timestamptz [not null, default: `now()`]
  Note: 'Charges against an order. A failed attempt is a row too, which fraud review needs.'
}

Table refunds [headercolor: #b98cf0] {
  id uuid [pk]
  order_id uuid [not null, ref: > orders.id]
  payment_id uuid [not null, ref: > payments.id]
  amount_cents integer [not null]
  reason refund_reason [not null]
  status payment_status [not null, default: 'pending']
  requested_by uuid [ref: > users.id, note: 'Box office staff, or null for an automatic cancellation refund']
  processor_reference varchar(100)
  created_at timestamptz [not null, default: `now()`]
  Note: 'Money returned. A cancelled performance produces one refund per order, issued in bulk.'
}

Table refund_items [headercolor: #b98cf0] {
  refund_id uuid [not null, ref: > refunds.id]
  order_item_id uuid [not null, ref: > order_items.id]
  amount_cents integer [not null, note: 'Face value plus any refundable fees for this admission']

  indexes {
    (refund_id, order_item_id) [pk]
  }

  Note: 'Which admissions a refund covers, so returning one ticket of four is exact and voids only that ticket.'
}

// ---------------------------------------------------------------------------
// TICKETS AND ENTRY
// ---------------------------------------------------------------------------

Table tickets [headercolor: #f07178] {
  id uuid [pk]
  order_item_id uuid [not null, ref: > order_items.id]
  performance_id uuid [not null, ref: > performances.id, note: 'Copied from the order item so the door lookup is one index hit']
  holder_user_id uuid [ref: > users.id]
  holder_name varchar(200)
  barcode varchar(64) [not null, unique, note: 'Random and unguessable, never sequential']
  status ticket_status [not null, default: 'valid']
  replaces_ticket_id uuid [ref: > tickets.id, note: 'The ticket this one was reissued from after a transfer']
  issued_at timestamptz [not null, default: `now()`]
  voided_at timestamptz

  indexes {
    order_item_id [note: 'Partial unique index WHERE status IN (valid, used): one live credential per admission']
    (performance_id, status)
  }

  Note: '''The credential, not the sale. A transfer or reissue voids this row and issues a new ticket with a new barcode, so a screenshot of the old one fails at the door, while the order item (and its refund rights) stays with the original buyer.'''
}

Table ticket_transfers [headercolor: #f07178] {
  id uuid [pk]
  from_ticket_id uuid [not null, ref: > tickets.id]
  to_ticket_id uuid [ref: > tickets.id, note: 'Issued on acceptance']
  from_user_id uuid [not null, ref: > users.id]
  to_email varchar(254) [not null]
  to_user_id uuid [ref: > users.id, note: 'Set when the recipient accepts and signs in']
  status transfer_status [not null, default: 'pending']
  created_at timestamptz [not null, default: `now()`]
  expires_at timestamptz [not null]
  accepted_at timestamptz
  Note: 'Sending a ticket to a friend. The original stays valid until the recipient accepts, so an unclaimed transfer never strands a ticket.'
}

Table scans [headercolor: #f07178] {
  id uuid [pk]
  performance_id uuid [not null, ref: > performances.id]
  ticket_id uuid [ref: > tickets.id, note: 'Null when the barcode matched nothing']
  barcode_read varchar(64) [not null, note: 'Exactly what the scanner read']
  result scan_result [not null]
  gate varchar(40)
  device_id varchar(64) [not null]
  scanned_by uuid [ref: > users.id]
  scanned_at timestamptz [not null, note: 'Device clock; scanners keep working offline and sync later']
  received_at timestamptz [not null, default: `now()`]

  indexes {
    (ticket_id, scanned_at)
    (performance_id, scanned_at) [note: 'Live entry count']
  }

  Note: '''Append-only: every read at the door, including rejected ones. The first admitted scan lets the holder in; later ones are logged as already_used, and the gate and time of the first are on record, which ends the argument at the turnstile.'''
}

// ---------------------------------------------------------------------------
// ENUMS
// ---------------------------------------------------------------------------

Enum organizer_role {
  owner
  manager
  box_office
  door_staff
}

Enum section_kind {
  reserved
  general_admission
}

Enum event_category {
  concert
  theatre
  sports
  comedy
  conference
  festival
  other
}

Enum event_status {
  draft
  published
  cancelled
  completed
}

Enum performance_status {
  scheduled
  on_sale
  sold_out
  postponed
  cancelled
  completed
}

Enum seat_status {
  available
  held
  sold
  killed [note: 'Taken out of sale for this performance: camera position, sightline, production']
  house_hold [note: 'Kept back for artist, sponsor or press allocation']
}

Enum hold_status {
  active
  converted
  released
  expired
}

Enum discount_kind {
  percent
  amount
  unlock_only
}

Enum order_status {
  pending_payment
  paid
  partially_refunded
  refunded
  cancelled
}

Enum order_item_status {
  active
  refunded
  cancelled
}

Enum fee_kind {
  service
  facility
  processing
  delivery
}

Enum payment_status {
  pending
  succeeded
  failed
}

Enum refund_reason {
  event_cancelled
  customer_request
  duplicate_order
  fraud
  goodwill
}

Enum ticket_status {
  valid
  used
  transferred
  void
}

Enum transfer_status {
  pending
  accepted
  cancelled
  expired
}

Enum scan_result {
  admitted
  already_used
  wrong_performance
  void_ticket
  unknown_barcode
}

Event Ticketing database schema as SQL

The same schema as CREATE TABLE statements, exported by DB Planner. PostgreSQL is shown first; MySQL and SQL Server are below it.

PostgreSQL
postgresql.sql PostgreSQL
CREATE TYPE "organizer_role" AS ENUM (
  'owner',
  'manager',
  'box_office',
  'door_staff'
);

CREATE TYPE "section_kind" AS ENUM (
  'reserved',
  'general_admission'
);

CREATE TYPE "event_category" AS ENUM (
  'concert',
  'theatre',
  'sports',
  'comedy',
  'conference',
  'festival',
  'other'
);

CREATE TYPE "event_status" AS ENUM (
  'draft',
  'published',
  'cancelled',
  'completed'
);

CREATE TYPE "performance_status" AS ENUM (
  'scheduled',
  'on_sale',
  'sold_out',
  'postponed',
  'cancelled',
  'completed'
);

CREATE TYPE "seat_status" AS ENUM (
  'available',
  'held',
  'sold',
  'killed',
  'house_hold'
);

CREATE TYPE "hold_status" AS ENUM (
  'active',
  'converted',
  'released',
  'expired'
);

CREATE TYPE "discount_kind" AS ENUM (
  'percent',
  'amount',
  'unlock_only'
);

CREATE TYPE "order_status" AS ENUM (
  'pending_payment',
  'paid',
  'partially_refunded',
  'refunded',
  'cancelled'
);

CREATE TYPE "order_item_status" AS ENUM (
  'active',
  'refunded',
  'cancelled'
);

CREATE TYPE "fee_kind" AS ENUM (
  'service',
  'facility',
  'processing',
  'delivery'
);

CREATE TYPE "payment_status" AS ENUM (
  'pending',
  'succeeded',
  'failed'
);

CREATE TYPE "refund_reason" AS ENUM (
  'event_cancelled',
  'customer_request',
  'duplicate_order',
  'fraud',
  'goodwill'
);

CREATE TYPE "ticket_status" AS ENUM (
  'valid',
  'used',
  'transferred',
  'void'
);

CREATE TYPE "transfer_status" AS ENUM (
  'pending',
  'accepted',
  'cancelled',
  'expired'
);

CREATE TYPE "scan_result" AS ENUM (
  'admitted',
  'already_used',
  'wrong_performance',
  'void_ticket',
  'unknown_barcode'
);

CREATE TABLE "users" (
  "id" uuid PRIMARY KEY,
  "email" varchar(254) UNIQUE NOT NULL,
  "full_name" varchar(200),
  "phone" varchar(30),
  "marketing_opt_in_at" timestamptz,
  "created_at" timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE "organizers" (
  "id" uuid PRIMARY KEY,
  "name" varchar(200) NOT NULL,
  "slug" varchar(80) UNIQUE NOT NULL,
  "currency" char(3) NOT NULL,
  "payout_account_token" varchar(200),
  "created_at" timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE "organizer_members" (
  "organizer_id" uuid NOT NULL,
  "user_id" uuid NOT NULL,
  "role" organizer_role NOT NULL,
  "added_at" timestamptz NOT NULL DEFAULT (now()),
  PRIMARY KEY ("organizer_id", "user_id")
);

CREATE TABLE "venues" (
  "id" uuid PRIMARY KEY,
  "name" varchar(200) NOT NULL,
  "address_line1" varchar(200) NOT NULL,
  "city" varchar(120) NOT NULL,
  "region" varchar(120),
  "postal_code" varchar(20),
  "country" char(2) NOT NULL,
  "timezone" varchar(64) NOT NULL
);

CREATE TABLE "seat_maps" (
  "id" uuid PRIMARY KEY,
  "venue_id" uuid NOT NULL,
  "name" varchar(120) NOT NULL,
  "total_capacity" integer NOT NULL,
  "is_active" boolean NOT NULL DEFAULT true
);

CREATE TABLE "sections" (
  "id" uuid PRIMARY KEY,
  "seat_map_id" uuid NOT NULL,
  "name" varchar(100) NOT NULL,
  "kind" section_kind NOT NULL,
  "ga_capacity" integer,
  "sort_order" smallint NOT NULL
);

CREATE TABLE "seat_rows" (
  "id" uuid PRIMARY KEY,
  "section_id" uuid NOT NULL,
  "label" varchar(10) NOT NULL,
  "sort_order" smallint NOT NULL
);

CREATE TABLE "seats" (
  "id" uuid PRIMARY KEY,
  "row_id" uuid NOT NULL,
  "label" varchar(10) NOT NULL,
  "x" numeric(8,2) NOT NULL,
  "y" numeric(8,2) NOT NULL,
  "is_accessible" boolean NOT NULL DEFAULT false,
  "is_obstructed" boolean NOT NULL DEFAULT false
);

CREATE TABLE "events" (
  "id" uuid PRIMARY KEY,
  "organizer_id" uuid NOT NULL,
  "title" varchar(300) NOT NULL,
  "slug" varchar(120) UNIQUE NOT NULL,
  "description" text,
  "category" event_category NOT NULL,
  "min_age" smallint,
  "currency" char(3) NOT NULL,
  "status" event_status NOT NULL DEFAULT 'draft',
  "published_at" timestamptz,
  "created_at" timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE "performances" (
  "id" uuid PRIMARY KEY,
  "event_id" uuid NOT NULL,
  "seat_map_id" uuid NOT NULL,
  "starts_at" timestamptz NOT NULL,
  "doors_open_at" timestamptz,
  "on_sale_at" timestamptz NOT NULL,
  "off_sale_at" timestamptz,
  "status" performance_status NOT NULL DEFAULT 'scheduled',
  "cancelled_at" timestamptz
);

CREATE TABLE "ticket_types" (
  "id" uuid PRIMARY KEY,
  "event_id" uuid NOT NULL,
  "name" varchar(100) NOT NULL,
  "description" varchar(500),
  "is_hidden" boolean NOT NULL DEFAULT false,
  "min_per_order" smallint NOT NULL DEFAULT 1,
  "max_per_order" smallint NOT NULL DEFAULT 8,
  "sales_start_at" timestamptz,
  "sales_end_at" timestamptz,
  "sort_order" smallint NOT NULL
);

CREATE TABLE "price_tiers" (
  "id" uuid PRIMARY KEY,
  "event_id" uuid NOT NULL,
  "name" varchar(100) NOT NULL,
  "color" char(7) NOT NULL,
  "sort_order" smallint NOT NULL
);

CREATE TABLE "ticket_prices" (
  "ticket_type_id" uuid NOT NULL,
  "price_tier_id" uuid NOT NULL,
  "face_value_cents" integer NOT NULL,
  "service_fee_cents" integer NOT NULL DEFAULT 0,
  PRIMARY KEY ("ticket_type_id", "price_tier_id")
);

CREATE TABLE "performance_seats" (
  "id" uuid PRIMARY KEY,
  "performance_id" uuid NOT NULL,
  "seat_id" uuid NOT NULL,
  "price_tier_id" uuid NOT NULL,
  "status" seat_status NOT NULL DEFAULT 'available',
  "hold_id" uuid,
  "hold_expires_at" timestamptz,
  "updated_at" timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE "performance_sections" (
  "id" uuid PRIMARY KEY,
  "performance_id" uuid NOT NULL,
  "section_id" uuid NOT NULL,
  "price_tier_id" uuid NOT NULL,
  "capacity" integer NOT NULL,
  "sold_count" integer NOT NULL DEFAULT 0
);

CREATE TABLE "holds" (
  "id" uuid PRIMARY KEY,
  "user_id" uuid,
  "session_token" varchar(100) NOT NULL,
  "status" hold_status NOT NULL DEFAULT 'active',
  "expires_at" timestamptz NOT NULL,
  "created_at" timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE "hold_items" (
  "id" uuid PRIMARY KEY,
  "hold_id" uuid NOT NULL,
  "ticket_type_id" uuid NOT NULL,
  "performance_seat_id" uuid,
  "performance_section_id" uuid,
  "quantity" smallint NOT NULL DEFAULT 1
);

CREATE TABLE "promo_codes" (
  "id" uuid PRIMARY KEY,
  "organizer_id" uuid NOT NULL,
  "event_id" uuid,
  "code" varchar(40) NOT NULL,
  "discount_kind" discount_kind NOT NULL,
  "discount_value" integer NOT NULL DEFAULT 0,
  "unlocks_ticket_type_id" uuid,
  "max_redemptions" integer,
  "redemption_count" integer NOT NULL DEFAULT 0,
  "valid_from" timestamptz,
  "valid_until" timestamptz
);

CREATE TABLE "orders" (
  "id" uuid PRIMARY KEY,
  "order_number" varchar(20) UNIQUE NOT NULL,
  "user_id" uuid,
  "buyer_email" varchar(254) NOT NULL,
  "hold_id" uuid UNIQUE,
  "promo_code_id" uuid,
  "status" order_status NOT NULL DEFAULT 'pending_payment',
  "currency" char(3) NOT NULL,
  "subtotal_cents" integer NOT NULL,
  "fees_cents" integer NOT NULL,
  "discount_cents" integer NOT NULL DEFAULT 0,
  "tax_cents" integer NOT NULL DEFAULT 0,
  "total_cents" integer NOT NULL,
  "placed_at" timestamptz,
  "created_at" timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE "order_items" (
  "id" uuid PRIMARY KEY,
  "order_id" uuid NOT NULL,
  "performance_id" uuid NOT NULL,
  "ticket_type_id" uuid NOT NULL,
  "price_tier_id" uuid NOT NULL,
  "performance_seat_id" uuid,
  "performance_section_id" uuid,
  "seat_label" varchar(100),
  "face_value_cents" integer NOT NULL,
  "discount_cents" integer NOT NULL DEFAULT 0,
  "status" order_item_status NOT NULL DEFAULT 'active'
);

CREATE TABLE "order_fees" (
  "id" uuid PRIMARY KEY,
  "order_id" uuid NOT NULL,
  "order_item_id" uuid,
  "kind" fee_kind NOT NULL,
  "amount_cents" integer NOT NULL,
  "is_refundable" boolean NOT NULL
);

CREATE TABLE "payments" (
  "id" uuid PRIMARY KEY,
  "order_id" uuid NOT NULL,
  "amount_cents" integer NOT NULL,
  "currency" char(3) NOT NULL,
  "status" payment_status NOT NULL,
  "processor_reference" varchar(100) UNIQUE NOT NULL,
  "created_at" timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE "refunds" (
  "id" uuid PRIMARY KEY,
  "order_id" uuid NOT NULL,
  "payment_id" uuid NOT NULL,
  "amount_cents" integer NOT NULL,
  "reason" refund_reason NOT NULL,
  "status" payment_status NOT NULL DEFAULT 'pending',
  "requested_by" uuid,
  "processor_reference" varchar(100),
  "created_at" timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE "refund_items" (
  "refund_id" uuid NOT NULL,
  "order_item_id" uuid NOT NULL,
  "amount_cents" integer NOT NULL,
  PRIMARY KEY ("refund_id", "order_item_id")
);

CREATE TABLE "tickets" (
  "id" uuid PRIMARY KEY,
  "order_item_id" uuid NOT NULL,
  "performance_id" uuid NOT NULL,
  "holder_user_id" uuid,
  "holder_name" varchar(200),
  "barcode" varchar(64) UNIQUE NOT NULL,
  "status" ticket_status NOT NULL DEFAULT 'valid',
  "replaces_ticket_id" uuid,
  "issued_at" timestamptz NOT NULL DEFAULT (now()),
  "voided_at" timestamptz
);

CREATE TABLE "ticket_transfers" (
  "id" uuid PRIMARY KEY,
  "from_ticket_id" uuid NOT NULL,
  "to_ticket_id" uuid,
  "from_user_id" uuid NOT NULL,
  "to_email" varchar(254) NOT NULL,
  "to_user_id" uuid,
  "status" transfer_status NOT NULL DEFAULT 'pending',
  "created_at" timestamptz NOT NULL DEFAULT (now()),
  "expires_at" timestamptz NOT NULL,
  "accepted_at" timestamptz
);

CREATE TABLE "scans" (
  "id" uuid PRIMARY KEY,
  "performance_id" uuid NOT NULL,
  "ticket_id" uuid,
  "barcode_read" varchar(64) NOT NULL,
  "result" scan_result NOT NULL,
  "gate" varchar(40),
  "device_id" varchar(64) NOT NULL,
  "scanned_by" uuid,
  "scanned_at" timestamptz NOT NULL,
  "received_at" timestamptz NOT NULL DEFAULT (now())
);

CREATE UNIQUE INDEX ON "sections" ("seat_map_id", "name");

CREATE UNIQUE INDEX ON "seat_rows" ("section_id", "label");

CREATE UNIQUE INDEX ON "seats" ("row_id", "label");

CREATE INDEX ON "performances" ("event_id", "starts_at");

CREATE UNIQUE INDEX ON "performance_seats" ("performance_id", "seat_id");

CREATE INDEX ON "performance_seats" ("performance_id", "status");

CREATE INDEX ON "performance_seats" ("hold_id");

CREATE UNIQUE INDEX ON "performance_sections" ("performance_id", "section_id");

CREATE INDEX ON "hold_items" ("hold_id");

CREATE INDEX ON "hold_items" ("performance_section_id");

CREATE UNIQUE INDEX ON "promo_codes" ((upper(code)), "organizer_id");

CREATE INDEX ON "order_items" ("order_id");

CREATE INDEX ON "order_items" ("performance_seat_id");

CREATE INDEX ON "tickets" ("order_item_id");

CREATE INDEX ON "tickets" ("performance_id", "status");

CREATE INDEX ON "scans" ("ticket_id", "scanned_at");

CREATE INDEX ON "scans" ("performance_id", "scanned_at");

COMMENT ON TABLE "users" IS 'Buyers, ticket holders and organizer staff. A transfer recipient gets a row when they accept.';

COMMENT ON COLUMN "users"."marketing_opt_in_at" IS 'Consent as a timestamp, so you can prove when it was given';

COMMENT ON TABLE "organizers" IS 'Promoters, theatres, clubs and teams that put on events. Settlement and payouts are per organizer.';

COMMENT ON COLUMN "organizers"."currency" IS 'Settlement currency for payouts';

COMMENT ON COLUMN "organizers"."payout_account_token" IS 'Payment processor connected-account id; bank details never live here';

COMMENT ON TABLE "organizer_members" IS 'Organizer staff. Box office can sell and refund; door staff can only scan.';

COMMENT ON TABLE "venues" IS 'Physical places. Shared across organizers, since many promoters book the same hall.';

COMMENT ON COLUMN "venues"."timezone" IS 'IANA zone. Show times are displayed in it, wherever the buyer is.';

COMMENT ON TABLE "seat_maps" IS 'One configuration of a venue. The same arena seats 18,000 for a concert and 6,000 for a theatre setup.';

COMMENT ON COLUMN "seat_maps"."name" IS 'e.g. End stage, In the round, Standing floor';

COMMENT ON TABLE "sections" IS 'Areas of a seat map. A reserved section has rows and seats; a general admission section has only a capacity.';

COMMENT ON COLUMN "sections"."name" IS 'e.g. Orchestra, Balcony, Floor';

COMMENT ON COLUMN "sections"."ga_capacity" IS 'General admission only: how many people fit. Null for reserved sections.';

COMMENT ON TABLE "seat_rows" IS 'Rows within a reserved section.';

COMMENT ON COLUMN "seat_rows"."label" IS 'e.g. K, AA. Not always alphabetical, so order by sort_order.';

COMMENT ON TABLE "seats" IS 'The physical seat. It belongs to the map; whether it is for sale tonight, and at what price, belongs to performance_seats.';

COMMENT ON COLUMN "seats"."label" IS 'e.g. 12, 101. Odd and even sides are common.';

COMMENT ON COLUMN "seats"."x" IS 'Position on the drawn seat map';

COMMENT ON COLUMN "seats"."is_obstructed" IS 'Restricted view, disclosed before purchase';

COMMENT ON TABLE "events" IS 'The show, match or conference as marketed. A three-night run is one event with three performances.';

COMMENT ON COLUMN "events"."min_age" IS 'Null for all ages';

COMMENT ON TABLE "performances" IS 'One date and time of an event. Inventory, tickets and door scans are always per performance.';

COMMENT ON COLUMN "performances"."seat_map_id" IS 'Which venue and configuration; it can differ between dates of a run';

COMMENT ON TABLE "ticket_types" IS 'Who the ticket is for, and what it includes.';

COMMENT ON COLUMN "ticket_types"."name" IS 'e.g. Adult, Child, Student, VIP package';

COMMENT ON COLUMN "ticket_types"."is_hidden" IS 'Only offered when a promo code unlocks it, e.g. a presale';

COMMENT ON COLUMN "ticket_types"."max_per_order" IS 'Anti-scalping limit';

COMMENT ON TABLE "price_tiers" IS 'Seat quality bands. Each seat or GA section is placed in a tier per performance.';

COMMENT ON COLUMN "price_tiers"."name" IS 'e.g. Orchestra A, Balcony C, GA Floor';

COMMENT ON COLUMN "price_tiers"."color" IS 'Seat map legend color';

COMMENT ON TABLE "ticket_prices" IS 'The price matrix: ticket type by price tier. A Child ticket in Balcony C has its own price.';

COMMENT ON COLUMN "ticket_prices"."service_fee_cents" IS 'Per-ticket booking fee shown before checkout';

COMMENT ON TABLE "performance_seats" IS 'Per-performance seat inventory, one row per sellable seat, created when the performance goes on sale. A seat is claimed by one conditional UPDATE: SET status = held, hold_id = $1, hold_expires_at = now() + 10 minutes WHERE id = ANY($2) AND (status = available OR (status = held AND hold_expires_at < now())). If fewer rows changed than were asked for, someone else got a seat first and the transaction rolls back. Two buyers can never both win the same seat.';

COMMENT ON COLUMN "performance_seats"."price_tier_id" IS 'Tiers can differ per night, e.g. Saturday pricing';

COMMENT ON COLUMN "performance_seats"."hold_expires_at" IS 'A held seat whose hold has expired is available again, with no job needed';

COMMENT ON TABLE "performance_sections" IS 'General admission inventory for one performance. CHECK (sold_count <= capacity). A GA claim locks this one row (SELECT ... FOR UPDATE), subtracts quantities in unexpired holds, and inserts its hold item; checkout increments sold_count. Expired holds stop counting the moment they expire, and the CHECK refuses an oversell even if app code is wrong.';

COMMENT ON COLUMN "performance_sections"."capacity" IS 'Usually the section ga_capacity, lowered for a smaller show or a safety limit';

COMMENT ON TABLE "holds" IS 'A cart. Seats and GA places are reserved for a few minutes while the buyer checks out, then quietly lapse.';

COMMENT ON COLUMN "holds"."user_id" IS 'Null for a guest checkout';

COMMENT ON COLUMN "holds"."session_token" IS 'Ties the hold to a browser session before login';

COMMENT ON COLUMN "holds"."expires_at" IS 'Typically 10 to 15 minutes; extended once when payment starts';

COMMENT ON TABLE "hold_items" IS 'What is in the cart. Exactly one of performance_seat_id or performance_section_id is set.';

COMMENT ON COLUMN "hold_items"."performance_seat_id" IS 'Reserved seating: one item per seat';

COMMENT ON COLUMN "hold_items"."performance_section_id" IS 'General admission: one item with a quantity';

COMMENT ON TABLE "promo_codes" IS 'Discounts and presale access codes.';

COMMENT ON COLUMN "promo_codes"."event_id" IS 'Null for a code valid on every event of the organizer';

COMMENT ON COLUMN "promo_codes"."discount_value" IS 'Basis points for percent (1500 = 15%), cents for amount, 0 for unlock only';

COMMENT ON COLUMN "promo_codes"."unlocks_ticket_type_id" IS 'Reveals a hidden ticket type, e.g. a fan club presale';

COMMENT ON COLUMN "promo_codes"."redemption_count" IS 'Incremented in the checkout transaction; CHECK (redemption_count <= max_redemptions)';

COMMENT ON TABLE "orders" IS 'A purchase. Every total is the sum of frozen lines below it, so a later price change never alters a paid order.';

COMMENT ON COLUMN "orders"."order_number" IS 'Short, human-readable, given to support over the phone';

COMMENT ON COLUMN "orders"."buyer_email" IS 'Snapshot; guests have no user row';

COMMENT ON COLUMN "orders"."hold_id" IS 'The cart this order was checked out from';

COMMENT ON TABLE "order_items" IS 'One admission sold: the commercial record. Price, fees and refunds attach here. The barcode lives on the ticket, which can be reissued.';

COMMENT ON COLUMN "order_items"."performance_seat_id" IS 'Null for general admission';

COMMENT ON COLUMN "order_items"."performance_section_id" IS 'Set for general admission';

COMMENT ON COLUMN "order_items"."seat_label" IS 'Snapshot, e.g. Orchestra, Row K, Seat 12, so remapping a venue never changes a sold ticket';

COMMENT ON COLUMN "order_items"."face_value_cents" IS 'Price snapshot at checkout';

COMMENT ON TABLE "order_fees" IS 'Every fee line frozen at checkout: service, facility, processing, delivery. The fees shown before payment are exactly these rows.';

COMMENT ON COLUMN "order_fees"."order_item_id" IS 'Set for per-ticket fees, null for per-order fees';

COMMENT ON COLUMN "order_fees"."is_refundable" IS 'Decided at sale, not at refund time';

COMMENT ON TABLE "payments" IS 'Charges against an order. A failed attempt is a row too, which fraud review needs.';

COMMENT ON COLUMN "payments"."processor_reference" IS 'Card data stays with the payment processor';

COMMENT ON TABLE "refunds" IS 'Money returned. A cancelled performance produces one refund per order, issued in bulk.';

COMMENT ON COLUMN "refunds"."requested_by" IS 'Box office staff, or null for an automatic cancellation refund';

COMMENT ON TABLE "refund_items" IS 'Which admissions a refund covers, so returning one ticket of four is exact and voids only that ticket.';

COMMENT ON COLUMN "refund_items"."amount_cents" IS 'Face value plus any refundable fees for this admission';

COMMENT ON TABLE "tickets" IS 'The credential, not the sale. A transfer or reissue voids this row and issues a new ticket with a new barcode, so a screenshot of the old one fails at the door, while the order item (and its refund rights) stays with the original buyer.';

COMMENT ON COLUMN "tickets"."performance_id" IS 'Copied from the order item so the door lookup is one index hit';

COMMENT ON COLUMN "tickets"."barcode" IS 'Random and unguessable, never sequential';

COMMENT ON COLUMN "tickets"."replaces_ticket_id" IS 'The ticket this one was reissued from after a transfer';

COMMENT ON TABLE "ticket_transfers" IS 'Sending a ticket to a friend. The original stays valid until the recipient accepts, so an unclaimed transfer never strands a ticket.';

COMMENT ON COLUMN "ticket_transfers"."to_ticket_id" IS 'Issued on acceptance';

COMMENT ON COLUMN "ticket_transfers"."to_user_id" IS 'Set when the recipient accepts and signs in';

COMMENT ON TABLE "scans" IS 'Append-only: every read at the door, including rejected ones. The first admitted scan lets the holder in; later ones are logged as already_used, and the gate and time of the first are on record, which ends the argument at the turnstile.';

COMMENT ON COLUMN "scans"."ticket_id" IS 'Null when the barcode matched nothing';

COMMENT ON COLUMN "scans"."barcode_read" IS 'Exactly what the scanner read';

COMMENT ON COLUMN "scans"."scanned_at" IS 'Device clock; scanners keep working offline and sync later';

ALTER TABLE "organizer_members" ADD FOREIGN KEY ("organizer_id") REFERENCES "organizers" ("id");

ALTER TABLE "organizer_members" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");

ALTER TABLE "seat_maps" ADD FOREIGN KEY ("venue_id") REFERENCES "venues" ("id");

ALTER TABLE "sections" ADD FOREIGN KEY ("seat_map_id") REFERENCES "seat_maps" ("id");

ALTER TABLE "seat_rows" ADD FOREIGN KEY ("section_id") REFERENCES "sections" ("id");

ALTER TABLE "seats" ADD FOREIGN KEY ("row_id") REFERENCES "seat_rows" ("id");

ALTER TABLE "events" ADD FOREIGN KEY ("organizer_id") REFERENCES "organizers" ("id");

ALTER TABLE "performances" ADD FOREIGN KEY ("event_id") REFERENCES "events" ("id");

ALTER TABLE "performances" ADD FOREIGN KEY ("seat_map_id") REFERENCES "seat_maps" ("id");

ALTER TABLE "ticket_types" ADD FOREIGN KEY ("event_id") REFERENCES "events" ("id");

ALTER TABLE "price_tiers" ADD FOREIGN KEY ("event_id") REFERENCES "events" ("id");

ALTER TABLE "ticket_prices" ADD FOREIGN KEY ("ticket_type_id") REFERENCES "ticket_types" ("id");

ALTER TABLE "ticket_prices" ADD FOREIGN KEY ("price_tier_id") REFERENCES "price_tiers" ("id");

ALTER TABLE "performance_seats" ADD FOREIGN KEY ("performance_id") REFERENCES "performances" ("id");

ALTER TABLE "performance_seats" ADD FOREIGN KEY ("seat_id") REFERENCES "seats" ("id");

ALTER TABLE "performance_seats" ADD FOREIGN KEY ("price_tier_id") REFERENCES "price_tiers" ("id");

ALTER TABLE "performance_seats" ADD FOREIGN KEY ("hold_id") REFERENCES "holds" ("id");

ALTER TABLE "performance_sections" ADD FOREIGN KEY ("performance_id") REFERENCES "performances" ("id");

ALTER TABLE "performance_sections" ADD FOREIGN KEY ("section_id") REFERENCES "sections" ("id");

ALTER TABLE "performance_sections" ADD FOREIGN KEY ("price_tier_id") REFERENCES "price_tiers" ("id");

ALTER TABLE "holds" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");

ALTER TABLE "hold_items" ADD FOREIGN KEY ("hold_id") REFERENCES "holds" ("id");

ALTER TABLE "hold_items" ADD FOREIGN KEY ("ticket_type_id") REFERENCES "ticket_types" ("id");

ALTER TABLE "hold_items" ADD FOREIGN KEY ("performance_seat_id") REFERENCES "performance_seats" ("id");

ALTER TABLE "hold_items" ADD FOREIGN KEY ("performance_section_id") REFERENCES "performance_sections" ("id");

ALTER TABLE "promo_codes" ADD FOREIGN KEY ("organizer_id") REFERENCES "organizers" ("id");

ALTER TABLE "promo_codes" ADD FOREIGN KEY ("event_id") REFERENCES "events" ("id");

ALTER TABLE "promo_codes" ADD FOREIGN KEY ("unlocks_ticket_type_id") REFERENCES "ticket_types" ("id");

ALTER TABLE "orders" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");

ALTER TABLE "orders" ADD FOREIGN KEY ("hold_id") REFERENCES "holds" ("id");

ALTER TABLE "orders" ADD FOREIGN KEY ("promo_code_id") REFERENCES "promo_codes" ("id");

ALTER TABLE "order_items" ADD FOREIGN KEY ("order_id") REFERENCES "orders" ("id");

ALTER TABLE "order_items" ADD FOREIGN KEY ("performance_id") REFERENCES "performances" ("id");

ALTER TABLE "order_items" ADD FOREIGN KEY ("ticket_type_id") REFERENCES "ticket_types" ("id");

ALTER TABLE "order_items" ADD FOREIGN KEY ("price_tier_id") REFERENCES "price_tiers" ("id");

ALTER TABLE "order_items" ADD FOREIGN KEY ("performance_seat_id") REFERENCES "performance_seats" ("id");

ALTER TABLE "order_items" ADD FOREIGN KEY ("performance_section_id") REFERENCES "performance_sections" ("id");

ALTER TABLE "order_fees" ADD FOREIGN KEY ("order_id") REFERENCES "orders" ("id");

ALTER TABLE "order_fees" ADD FOREIGN KEY ("order_item_id") REFERENCES "order_items" ("id");

ALTER TABLE "payments" ADD FOREIGN KEY ("order_id") REFERENCES "orders" ("id");

ALTER TABLE "refunds" ADD FOREIGN KEY ("order_id") REFERENCES "orders" ("id");

ALTER TABLE "refunds" ADD FOREIGN KEY ("payment_id") REFERENCES "payments" ("id");

ALTER TABLE "refunds" ADD FOREIGN KEY ("requested_by") REFERENCES "users" ("id");

ALTER TABLE "refund_items" ADD FOREIGN KEY ("refund_id") REFERENCES "refunds" ("id");

ALTER TABLE "refund_items" ADD FOREIGN KEY ("order_item_id") REFERENCES "order_items" ("id");

ALTER TABLE "tickets" ADD FOREIGN KEY ("order_item_id") REFERENCES "order_items" ("id");

ALTER TABLE "tickets" ADD FOREIGN KEY ("performance_id") REFERENCES "performances" ("id");

ALTER TABLE "tickets" ADD FOREIGN KEY ("holder_user_id") REFERENCES "users" ("id");

ALTER TABLE "tickets" ADD FOREIGN KEY ("replaces_ticket_id") REFERENCES "tickets" ("id");

ALTER TABLE "ticket_transfers" ADD FOREIGN KEY ("from_ticket_id") REFERENCES "tickets" ("id");

ALTER TABLE "ticket_transfers" ADD FOREIGN KEY ("to_ticket_id") REFERENCES "tickets" ("id");

ALTER TABLE "ticket_transfers" ADD FOREIGN KEY ("from_user_id") REFERENCES "users" ("id");

ALTER TABLE "ticket_transfers" ADD FOREIGN KEY ("to_user_id") REFERENCES "users" ("id");

ALTER TABLE "scans" ADD FOREIGN KEY ("performance_id") REFERENCES "performances" ("id");

ALTER TABLE "scans" ADD FOREIGN KEY ("ticket_id") REFERENCES "tickets" ("id");

ALTER TABLE "scans" ADD FOREIGN KEY ("scanned_by") REFERENCES "users" ("id");
MySQL
mysql.sql MySQL
CREATE TABLE `users` (
  `id` uuid PRIMARY KEY,
  `email` varchar(254) UNIQUE NOT NULL,
  `full_name` varchar(200),
  `phone` varchar(30),
  `marketing_opt_in_at` timestamptz COMMENT 'Consent as a timestamp, so you can prove when it was given',
  `created_at` timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE `organizers` (
  `id` uuid PRIMARY KEY,
  `name` varchar(200) NOT NULL,
  `slug` varchar(80) UNIQUE NOT NULL,
  `currency` char(3) NOT NULL COMMENT 'Settlement currency for payouts',
  `payout_account_token` varchar(200) COMMENT 'Payment processor connected-account id; bank details never live here',
  `created_at` timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE `organizer_members` (
  `organizer_id` uuid NOT NULL,
  `user_id` uuid NOT NULL,
  `role` ENUM ('owner', 'manager', 'box_office', 'door_staff') NOT NULL,
  `added_at` timestamptz NOT NULL DEFAULT (now()),
  PRIMARY KEY (`organizer_id`, `user_id`)
);

CREATE TABLE `venues` (
  `id` uuid PRIMARY KEY,
  `name` varchar(200) NOT NULL,
  `address_line1` varchar(200) NOT NULL,
  `city` varchar(120) NOT NULL,
  `region` varchar(120),
  `postal_code` varchar(20),
  `country` char(2) NOT NULL,
  `timezone` varchar(64) NOT NULL COMMENT 'IANA zone. Show times are displayed in it, wherever the buyer is.'
);

CREATE TABLE `seat_maps` (
  `id` uuid PRIMARY KEY,
  `venue_id` uuid NOT NULL,
  `name` varchar(120) NOT NULL COMMENT 'e.g. End stage, In the round, Standing floor',
  `total_capacity` integer NOT NULL,
  `is_active` boolean NOT NULL DEFAULT true
);

CREATE TABLE `sections` (
  `id` uuid PRIMARY KEY,
  `seat_map_id` uuid NOT NULL,
  `name` varchar(100) NOT NULL COMMENT 'e.g. Orchestra, Balcony, Floor',
  `kind` ENUM ('reserved', 'general_admission') NOT NULL,
  `ga_capacity` integer COMMENT 'General admission only: how many people fit. Null for reserved sections.',
  `sort_order` smallint NOT NULL
);

CREATE TABLE `seat_rows` (
  `id` uuid PRIMARY KEY,
  `section_id` uuid NOT NULL,
  `label` varchar(10) NOT NULL COMMENT 'e.g. K, AA. Not always alphabetical, so order by sort_order.',
  `sort_order` smallint NOT NULL
);

CREATE TABLE `seats` (
  `id` uuid PRIMARY KEY,
  `row_id` uuid NOT NULL,
  `label` varchar(10) NOT NULL COMMENT 'e.g. 12, 101. Odd and even sides are common.',
  `x` numeric(8,2) NOT NULL COMMENT 'Position on the drawn seat map',
  `y` numeric(8,2) NOT NULL,
  `is_accessible` boolean NOT NULL DEFAULT false,
  `is_obstructed` boolean NOT NULL DEFAULT false COMMENT 'Restricted view, disclosed before purchase'
);

CREATE TABLE `events` (
  `id` uuid PRIMARY KEY,
  `organizer_id` uuid NOT NULL,
  `title` varchar(300) NOT NULL,
  `slug` varchar(120) UNIQUE NOT NULL,
  `description` text,
  `category` ENUM ('concert', 'theatre', 'sports', 'comedy', 'conference', 'festival', 'other') NOT NULL,
  `min_age` smallint COMMENT 'Null for all ages',
  `currency` char(3) NOT NULL,
  `status` ENUM ('draft', 'published', 'cancelled', 'completed') NOT NULL DEFAULT 'draft',
  `published_at` timestamptz,
  `created_at` timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE `performances` (
  `id` uuid PRIMARY KEY,
  `event_id` uuid NOT NULL,
  `seat_map_id` uuid NOT NULL COMMENT 'Which venue and configuration; it can differ between dates of a run',
  `starts_at` timestamptz NOT NULL,
  `doors_open_at` timestamptz,
  `on_sale_at` timestamptz NOT NULL,
  `off_sale_at` timestamptz,
  `status` ENUM ('scheduled', 'on_sale', 'sold_out', 'postponed', 'cancelled', 'completed') NOT NULL DEFAULT 'scheduled',
  `cancelled_at` timestamptz
);

CREATE TABLE `ticket_types` (
  `id` uuid PRIMARY KEY,
  `event_id` uuid NOT NULL,
  `name` varchar(100) NOT NULL COMMENT 'e.g. Adult, Child, Student, VIP package',
  `description` varchar(500),
  `is_hidden` boolean NOT NULL DEFAULT false COMMENT 'Only offered when a promo code unlocks it, e.g. a presale',
  `min_per_order` smallint NOT NULL DEFAULT 1,
  `max_per_order` smallint NOT NULL DEFAULT 8 COMMENT 'Anti-scalping limit',
  `sales_start_at` timestamptz,
  `sales_end_at` timestamptz,
  `sort_order` smallint NOT NULL
);

CREATE TABLE `price_tiers` (
  `id` uuid PRIMARY KEY,
  `event_id` uuid NOT NULL,
  `name` varchar(100) NOT NULL COMMENT 'e.g. Orchestra A, Balcony C, GA Floor',
  `color` char(7) NOT NULL COMMENT 'Seat map legend color',
  `sort_order` smallint NOT NULL
);

CREATE TABLE `ticket_prices` (
  `ticket_type_id` uuid NOT NULL,
  `price_tier_id` uuid NOT NULL,
  `face_value_cents` integer NOT NULL,
  `service_fee_cents` integer NOT NULL DEFAULT 0 COMMENT 'Per-ticket booking fee shown before checkout',
  PRIMARY KEY (`ticket_type_id`, `price_tier_id`)
);

CREATE TABLE `performance_seats` (
  `id` uuid PRIMARY KEY,
  `performance_id` uuid NOT NULL,
  `seat_id` uuid NOT NULL,
  `price_tier_id` uuid NOT NULL COMMENT 'Tiers can differ per night, e.g. Saturday pricing',
  `status` ENUM ('available', 'held', 'sold', 'killed', 'house_hold') NOT NULL DEFAULT 'available',
  `hold_id` uuid,
  `hold_expires_at` timestamptz COMMENT 'A held seat whose hold has expired is available again, with no job needed',
  `updated_at` timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE `performance_sections` (
  `id` uuid PRIMARY KEY,
  `performance_id` uuid NOT NULL,
  `section_id` uuid NOT NULL,
  `price_tier_id` uuid NOT NULL,
  `capacity` integer NOT NULL COMMENT 'Usually the section ga_capacity, lowered for a smaller show or a safety limit',
  `sold_count` integer NOT NULL DEFAULT 0
);

CREATE TABLE `holds` (
  `id` uuid PRIMARY KEY,
  `user_id` uuid COMMENT 'Null for a guest checkout',
  `session_token` varchar(100) NOT NULL COMMENT 'Ties the hold to a browser session before login',
  `status` ENUM ('active', 'converted', 'released', 'expired') NOT NULL DEFAULT 'active',
  `expires_at` timestamptz NOT NULL COMMENT 'Typically 10 to 15 minutes; extended once when payment starts',
  `created_at` timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE `hold_items` (
  `id` uuid PRIMARY KEY,
  `hold_id` uuid NOT NULL,
  `ticket_type_id` uuid NOT NULL,
  `performance_seat_id` uuid COMMENT 'Reserved seating: one item per seat',
  `performance_section_id` uuid COMMENT 'General admission: one item with a quantity',
  `quantity` smallint NOT NULL DEFAULT 1
);

CREATE TABLE `promo_codes` (
  `id` uuid PRIMARY KEY,
  `organizer_id` uuid NOT NULL,
  `event_id` uuid COMMENT 'Null for a code valid on every event of the organizer',
  `code` varchar(40) NOT NULL,
  `discount_kind` ENUM ('percent', 'amount', 'unlock_only') NOT NULL,
  `discount_value` integer NOT NULL DEFAULT 0 COMMENT 'Basis points for percent (1500 = 15%), cents for amount, 0 for unlock only',
  `unlocks_ticket_type_id` uuid COMMENT 'Reveals a hidden ticket type, e.g. a fan club presale',
  `max_redemptions` integer,
  `redemption_count` integer NOT NULL DEFAULT 0 COMMENT 'Incremented in the checkout transaction; CHECK (redemption_count <= max_redemptions)',
  `valid_from` timestamptz,
  `valid_until` timestamptz
);

CREATE TABLE `orders` (
  `id` uuid PRIMARY KEY,
  `order_number` varchar(20) UNIQUE NOT NULL COMMENT 'Short, human-readable, given to support over the phone',
  `user_id` uuid,
  `buyer_email` varchar(254) NOT NULL COMMENT 'Snapshot; guests have no user row',
  `hold_id` uuid UNIQUE COMMENT 'The cart this order was checked out from',
  `promo_code_id` uuid,
  `status` ENUM ('pending_payment', 'paid', 'partially_refunded', 'refunded', 'cancelled') NOT NULL DEFAULT 'pending_payment',
  `currency` char(3) NOT NULL,
  `subtotal_cents` integer NOT NULL,
  `fees_cents` integer NOT NULL,
  `discount_cents` integer NOT NULL DEFAULT 0,
  `tax_cents` integer NOT NULL DEFAULT 0,
  `total_cents` integer NOT NULL,
  `placed_at` timestamptz,
  `created_at` timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE `order_items` (
  `id` uuid PRIMARY KEY,
  `order_id` uuid NOT NULL,
  `performance_id` uuid NOT NULL,
  `ticket_type_id` uuid NOT NULL,
  `price_tier_id` uuid NOT NULL,
  `performance_seat_id` uuid COMMENT 'Null for general admission',
  `performance_section_id` uuid COMMENT 'Set for general admission',
  `seat_label` varchar(100) COMMENT 'Snapshot, e.g. Orchestra, Row K, Seat 12, so remapping a venue never changes a sold ticket',
  `face_value_cents` integer NOT NULL COMMENT 'Price snapshot at checkout',
  `discount_cents` integer NOT NULL DEFAULT 0,
  `status` ENUM ('active', 'refunded', 'cancelled') NOT NULL DEFAULT 'active'
);

CREATE TABLE `order_fees` (
  `id` uuid PRIMARY KEY,
  `order_id` uuid NOT NULL,
  `order_item_id` uuid COMMENT 'Set for per-ticket fees, null for per-order fees',
  `kind` ENUM ('service', 'facility', 'processing', 'delivery') NOT NULL,
  `amount_cents` integer NOT NULL,
  `is_refundable` boolean NOT NULL COMMENT 'Decided at sale, not at refund time'
);

CREATE TABLE `payments` (
  `id` uuid PRIMARY KEY,
  `order_id` uuid NOT NULL,
  `amount_cents` integer NOT NULL,
  `currency` char(3) NOT NULL,
  `status` ENUM ('pending', 'succeeded', 'failed') NOT NULL,
  `processor_reference` varchar(100) UNIQUE NOT NULL COMMENT 'Card data stays with the payment processor',
  `created_at` timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE `refunds` (
  `id` uuid PRIMARY KEY,
  `order_id` uuid NOT NULL,
  `payment_id` uuid NOT NULL,
  `amount_cents` integer NOT NULL,
  `reason` ENUM ('event_cancelled', 'customer_request', 'duplicate_order', 'fraud', 'goodwill') NOT NULL,
  `status` ENUM ('pending', 'succeeded', 'failed') NOT NULL DEFAULT 'pending',
  `requested_by` uuid COMMENT 'Box office staff, or null for an automatic cancellation refund',
  `processor_reference` varchar(100),
  `created_at` timestamptz NOT NULL DEFAULT (now())
);

CREATE TABLE `refund_items` (
  `refund_id` uuid NOT NULL,
  `order_item_id` uuid NOT NULL,
  `amount_cents` integer NOT NULL COMMENT 'Face value plus any refundable fees for this admission',
  PRIMARY KEY (`refund_id`, `order_item_id`)
);

CREATE TABLE `tickets` (
  `id` uuid PRIMARY KEY,
  `order_item_id` uuid NOT NULL,
  `performance_id` uuid NOT NULL COMMENT 'Copied from the order item so the door lookup is one index hit',
  `holder_user_id` uuid,
  `holder_name` varchar(200),
  `barcode` varchar(64) UNIQUE NOT NULL COMMENT 'Random and unguessable, never sequential',
  `status` ENUM ('valid', 'used', 'transferred', 'void') NOT NULL DEFAULT 'valid',
  `replaces_ticket_id` uuid COMMENT 'The ticket this one was reissued from after a transfer',
  `issued_at` timestamptz NOT NULL DEFAULT (now()),
  `voided_at` timestamptz
);

CREATE TABLE `ticket_transfers` (
  `id` uuid PRIMARY KEY,
  `from_ticket_id` uuid NOT NULL,
  `to_ticket_id` uuid COMMENT 'Issued on acceptance',
  `from_user_id` uuid NOT NULL,
  `to_email` varchar(254) NOT NULL,
  `to_user_id` uuid COMMENT 'Set when the recipient accepts and signs in',
  `status` ENUM ('pending', 'accepted', 'cancelled', 'expired') NOT NULL DEFAULT 'pending',
  `created_at` timestamptz NOT NULL DEFAULT (now()),
  `expires_at` timestamptz NOT NULL,
  `accepted_at` timestamptz
);

CREATE TABLE `scans` (
  `id` uuid PRIMARY KEY,
  `performance_id` uuid NOT NULL,
  `ticket_id` uuid COMMENT 'Null when the barcode matched nothing',
  `barcode_read` varchar(64) NOT NULL COMMENT 'Exactly what the scanner read',
  `result` ENUM ('admitted', 'already_used', 'wrong_performance', 'void_ticket', 'unknown_barcode') NOT NULL,
  `gate` varchar(40),
  `device_id` varchar(64) NOT NULL,
  `scanned_by` uuid,
  `scanned_at` timestamptz NOT NULL COMMENT 'Device clock; scanners keep working offline and sync later',
  `received_at` timestamptz NOT NULL DEFAULT (now())
);

CREATE UNIQUE INDEX `sections_index_0` ON `sections` (`seat_map_id`, `name`);

CREATE UNIQUE INDEX `seat_rows_index_1` ON `seat_rows` (`section_id`, `label`);

CREATE UNIQUE INDEX `seats_index_2` ON `seats` (`row_id`, `label`);

CREATE INDEX `performances_index_3` ON `performances` (`event_id`, `starts_at`);

CREATE UNIQUE INDEX `performance_seats_index_4` ON `performance_seats` (`performance_id`, `seat_id`);

CREATE INDEX `performance_seats_index_5` ON `performance_seats` (`performance_id`, `status`);

CREATE INDEX `performance_seats_index_6` ON `performance_seats` (`hold_id`);

CREATE UNIQUE INDEX `performance_sections_index_7` ON `performance_sections` (`performance_id`, `section_id`);

CREATE INDEX `hold_items_index_8` ON `hold_items` (`hold_id`);

CREATE INDEX `hold_items_index_9` ON `hold_items` (`performance_section_id`);

CREATE UNIQUE INDEX `promo_codes_index_10` ON `promo_codes` ((upper(code)), `organizer_id`);

CREATE INDEX `order_items_index_11` ON `order_items` (`order_id`);

CREATE INDEX `order_items_index_12` ON `order_items` (`performance_seat_id`);

CREATE INDEX `tickets_index_13` ON `tickets` (`order_item_id`);

CREATE INDEX `tickets_index_14` ON `tickets` (`performance_id`, `status`);

CREATE INDEX `scans_index_15` ON `scans` (`ticket_id`, `scanned_at`);

CREATE INDEX `scans_index_16` ON `scans` (`performance_id`, `scanned_at`);

ALTER TABLE `users` COMMENT = 'Buyers, ticket holders and organizer staff. A transfer recipient gets a row when they accept.';

ALTER TABLE `organizers` COMMENT = 'Promoters, theatres, clubs and teams that put on events. Settlement and payouts are per organizer.';

ALTER TABLE `organizer_members` COMMENT = 'Organizer staff. Box office can sell and refund; door staff can only scan.';

ALTER TABLE `venues` COMMENT = 'Physical places. Shared across organizers, since many promoters book the same hall.';

ALTER TABLE `seat_maps` COMMENT = 'One configuration of a venue. The same arena seats 18,000 for a concert and 6,000 for a theatre setup.';

ALTER TABLE `sections` COMMENT = 'Areas of a seat map. A reserved section has rows and seats; a general admission section has only a capacity.';

ALTER TABLE `seat_rows` COMMENT = 'Rows within a reserved section.';

ALTER TABLE `seats` COMMENT = 'The physical seat. It belongs to the map; whether it is for sale tonight, and at what price, belongs to performance_seats.';

ALTER TABLE `events` COMMENT = 'The show, match or conference as marketed. A three-night run is one event with three performances.';

ALTER TABLE `performances` COMMENT = 'One date and time of an event. Inventory, tickets and door scans are always per performance.';

ALTER TABLE `ticket_types` COMMENT = 'Who the ticket is for, and what it includes.';

ALTER TABLE `price_tiers` COMMENT = 'Seat quality bands. Each seat or GA section is placed in a tier per performance.';

ALTER TABLE `ticket_prices` COMMENT = 'The price matrix: ticket type by price tier. A Child ticket in Balcony C has its own price.';

ALTER TABLE `performance_seats` COMMENT = 'Per-performance seat inventory, one row per sellable seat, created when the performance goes on sale. A seat is claimed by one conditional UPDATE: SET status = held, hold_id = $1, hold_expires_at = now() + 10 minutes WHERE id = ANY($2) AND (status = available OR (status = held AND hold_expires_at < now())). If fewer rows changed than were asked for, someone else got a seat first and the transaction rolls back. Two buyers can never both win the same seat.';

ALTER TABLE `performance_sections` COMMENT = 'General admission inventory for one performance. CHECK (sold_count <= capacity). A GA claim locks this one row (SELECT ... FOR UPDATE), subtracts quantities in unexpired holds, and inserts its hold item; checkout increments sold_count. Expired holds stop counting the moment they expire, and the CHECK refuses an oversell even if app code is wrong.';

ALTER TABLE `holds` COMMENT = 'A cart. Seats and GA places are reserved for a few minutes while the buyer checks out, then quietly lapse.';

ALTER TABLE `hold_items` COMMENT = 'What is in the cart. Exactly one of performance_seat_id or performance_section_id is set.';

ALTER TABLE `promo_codes` COMMENT = 'Discounts and presale access codes.';

ALTER TABLE `orders` COMMENT = 'A purchase. Every total is the sum of frozen lines below it, so a later price change never alters a paid order.';

ALTER TABLE `order_items` COMMENT = 'One admission sold: the commercial record. Price, fees and refunds attach here. The barcode lives on the ticket, which can be reissued.';

ALTER TABLE `order_fees` COMMENT = 'Every fee line frozen at checkout: service, facility, processing, delivery. The fees shown before payment are exactly these rows.';

ALTER TABLE `payments` COMMENT = 'Charges against an order. A failed attempt is a row too, which fraud review needs.';

ALTER TABLE `refunds` COMMENT = 'Money returned. A cancelled performance produces one refund per order, issued in bulk.';

ALTER TABLE `refund_items` COMMENT = 'Which admissions a refund covers, so returning one ticket of four is exact and voids only that ticket.';

ALTER TABLE `tickets` COMMENT = 'The credential, not the sale. A transfer or reissue voids this row and issues a new ticket with a new barcode, so a screenshot of the old one fails at the door, while the order item (and its refund rights) stays with the original buyer.';

ALTER TABLE `ticket_transfers` COMMENT = 'Sending a ticket to a friend. The original stays valid until the recipient accepts, so an unclaimed transfer never strands a ticket.';

ALTER TABLE `scans` COMMENT = 'Append-only: every read at the door, including rejected ones. The first admitted scan lets the holder in; later ones are logged as already_used, and the gate and time of the first are on record, which ends the argument at the turnstile.';

ALTER TABLE `organizer_members` ADD FOREIGN KEY (`organizer_id`) REFERENCES `organizers` (`id`);

ALTER TABLE `organizer_members` ADD FOREIGN KEY (`user_id`) REFERENCES `users` (`id`);

ALTER TABLE `seat_maps` ADD FOREIGN KEY (`venue_id`) REFERENCES `venues` (`id`);

ALTER TABLE `sections` ADD FOREIGN KEY (`seat_map_id`) REFERENCES `seat_maps` (`id`);

ALTER TABLE `seat_rows` ADD FOREIGN KEY (`section_id`) REFERENCES `sections` (`id`);

ALTER TABLE `seats` ADD FOREIGN KEY (`row_id`) REFERENCES `seat_rows` (`id`);

ALTER TABLE `events` ADD FOREIGN KEY (`organizer_id`) REFERENCES `organizers` (`id`);

ALTER TABLE `performances` ADD FOREIGN KEY (`event_id`) REFERENCES `events` (`id`);

ALTER TABLE `performances` ADD FOREIGN KEY (`seat_map_id`) REFERENCES `seat_maps` (`id`);

ALTER TABLE `ticket_types` ADD FOREIGN KEY (`event_id`) REFERENCES `events` (`id`);

ALTER TABLE `price_tiers` ADD FOREIGN KEY (`event_id`) REFERENCES `events` (`id`);

ALTER TABLE `ticket_prices` ADD FOREIGN KEY (`ticket_type_id`) REFERENCES `ticket_types` (`id`);

ALTER TABLE `ticket_prices` ADD FOREIGN KEY (`price_tier_id`) REFERENCES `price_tiers` (`id`);

ALTER TABLE `performance_seats` ADD FOREIGN KEY (`performance_id`) REFERENCES `performances` (`id`);

ALTER TABLE `performance_seats` ADD FOREIGN KEY (`seat_id`) REFERENCES `seats` (`id`);

ALTER TABLE `performance_seats` ADD FOREIGN KEY (`price_tier_id`) REFERENCES `price_tiers` (`id`);

ALTER TABLE `performance_seats` ADD FOREIGN KEY (`hold_id`) REFERENCES `holds` (`id`);

ALTER TABLE `performance_sections` ADD FOREIGN KEY (`performance_id`) REFERENCES `performances` (`id`);

ALTER TABLE `performance_sections` ADD FOREIGN KEY (`section_id`) REFERENCES `sections` (`id`);

ALTER TABLE `performance_sections` ADD FOREIGN KEY (`price_tier_id`) REFERENCES `price_tiers` (`id`);

ALTER TABLE `holds` ADD FOREIGN KEY (`user_id`) REFERENCES `users` (`id`);

ALTER TABLE `hold_items` ADD FOREIGN KEY (`hold_id`) REFERENCES `holds` (`id`);

ALTER TABLE `hold_items` ADD FOREIGN KEY (`ticket_type_id`) REFERENCES `ticket_types` (`id`);

ALTER TABLE `hold_items` ADD FOREIGN KEY (`performance_seat_id`) REFERENCES `performance_seats` (`id`);

ALTER TABLE `hold_items` ADD FOREIGN KEY (`performance_section_id`) REFERENCES `performance_sections` (`id`);

ALTER TABLE `promo_codes` ADD FOREIGN KEY (`organizer_id`) REFERENCES `organizers` (`id`);

ALTER TABLE `promo_codes` ADD FOREIGN KEY (`event_id`) REFERENCES `events` (`id`);

ALTER TABLE `promo_codes` ADD FOREIGN KEY (`unlocks_ticket_type_id`) REFERENCES `ticket_types` (`id`);

ALTER TABLE `orders` ADD FOREIGN KEY (`user_id`) REFERENCES `users` (`id`);

ALTER TABLE `orders` ADD FOREIGN KEY (`hold_id`) REFERENCES `holds` (`id`);

ALTER TABLE `orders` ADD FOREIGN KEY (`promo_code_id`) REFERENCES `promo_codes` (`id`);

ALTER TABLE `order_items` ADD FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`);

ALTER TABLE `order_items` ADD FOREIGN KEY (`performance_id`) REFERENCES `performances` (`id`);

ALTER TABLE `order_items` ADD FOREIGN KEY (`ticket_type_id`) REFERENCES `ticket_types` (`id`);

ALTER TABLE `order_items` ADD FOREIGN KEY (`price_tier_id`) REFERENCES `price_tiers` (`id`);

ALTER TABLE `order_items` ADD FOREIGN KEY (`performance_seat_id`) REFERENCES `performance_seats` (`id`);

ALTER TABLE `order_items` ADD FOREIGN KEY (`performance_section_id`) REFERENCES `performance_sections` (`id`);

ALTER TABLE `order_fees` ADD FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`);

ALTER TABLE `order_fees` ADD FOREIGN KEY (`order_item_id`) REFERENCES `order_items` (`id`);

ALTER TABLE `payments` ADD FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`);

ALTER TABLE `refunds` ADD FOREIGN KEY (`order_id`) REFERENCES `orders` (`id`);

ALTER TABLE `refunds` ADD FOREIGN KEY (`payment_id`) REFERENCES `payments` (`id`);

ALTER TABLE `refunds` ADD FOREIGN KEY (`requested_by`) REFERENCES `users` (`id`);

ALTER TABLE `refund_items` ADD FOREIGN KEY (`refund_id`) REFERENCES `refunds` (`id`);

ALTER TABLE `refund_items` ADD FOREIGN KEY (`order_item_id`) REFERENCES `order_items` (`id`);

ALTER TABLE `tickets` ADD FOREIGN KEY (`order_item_id`) REFERENCES `order_items` (`id`);

ALTER TABLE `tickets` ADD FOREIGN KEY (`performance_id`) REFERENCES `performances` (`id`);

ALTER TABLE `tickets` ADD FOREIGN KEY (`holder_user_id`) REFERENCES `users` (`id`);

ALTER TABLE `tickets` ADD FOREIGN KEY (`replaces_ticket_id`) REFERENCES `tickets` (`id`);

ALTER TABLE `ticket_transfers` ADD FOREIGN KEY (`from_ticket_id`) REFERENCES `tickets` (`id`);

ALTER TABLE `ticket_transfers` ADD FOREIGN KEY (`to_ticket_id`) REFERENCES `tickets` (`id`);

ALTER TABLE `ticket_transfers` ADD FOREIGN KEY (`from_user_id`) REFERENCES `users` (`id`);

ALTER TABLE `ticket_transfers` ADD FOREIGN KEY (`to_user_id`) REFERENCES `users` (`id`);

ALTER TABLE `scans` ADD FOREIGN KEY (`performance_id`) REFERENCES `performances` (`id`);

ALTER TABLE `scans` ADD FOREIGN KEY (`ticket_id`) REFERENCES `tickets` (`id`);

ALTER TABLE `scans` ADD FOREIGN KEY (`scanned_by`) REFERENCES `users` (`id`);
SQL Server
sql-server.sql SQL Server
CREATE TABLE [users] (
  [id] uuid PRIMARY KEY,
  [email] varchar(254) UNIQUE NOT NULL,
  [full_name] varchar(200),
  [phone] varchar(30),
  [marketing_opt_in_at] timestamptz,
  [created_at] timestamptz NOT NULL DEFAULT (now())
)
GO

CREATE TABLE [organizers] (
  [id] uuid PRIMARY KEY,
  [name] varchar(200) NOT NULL,
  [slug] varchar(80) UNIQUE NOT NULL,
  [currency] char(3) NOT NULL,
  [payout_account_token] varchar(200),
  [created_at] timestamptz NOT NULL DEFAULT (now())
)
GO

CREATE TABLE [organizer_members] (
  [organizer_id] uuid NOT NULL,
  [user_id] uuid NOT NULL,
  [role] nvarchar(255) NOT NULL CHECK ([role] IN ('owner', 'manager', 'box_office', 'door_staff')),
  [added_at] timestamptz NOT NULL DEFAULT (now()),
  PRIMARY KEY ([organizer_id], [user_id])
)
GO

CREATE TABLE [venues] (
  [id] uuid PRIMARY KEY,
  [name] varchar(200) NOT NULL,
  [address_line1] varchar(200) NOT NULL,
  [city] varchar(120) NOT NULL,
  [region] varchar(120),
  [postal_code] varchar(20),
  [country] char(2) NOT NULL,
  [timezone] varchar(64) NOT NULL
)
GO

CREATE TABLE [seat_maps] (
  [id] uuid PRIMARY KEY,
  [venue_id] uuid NOT NULL,
  [name] varchar(120) NOT NULL,
  [total_capacity] integer NOT NULL,
  [is_active] boolean NOT NULL DEFAULT (true)
)
GO

CREATE TABLE [sections] (
  [id] uuid PRIMARY KEY,
  [seat_map_id] uuid NOT NULL,
  [name] varchar(100) NOT NULL,
  [kind] nvarchar(255) NOT NULL CHECK ([kind] IN ('reserved', 'general_admission')),
  [ga_capacity] integer,
  [sort_order] smallint NOT NULL
)
GO

CREATE TABLE [seat_rows] (
  [id] uuid PRIMARY KEY,
  [section_id] uuid NOT NULL,
  [label] varchar(10) NOT NULL,
  [sort_order] smallint NOT NULL
)
GO

CREATE TABLE [seats] (
  [id] uuid PRIMARY KEY,
  [row_id] uuid NOT NULL,
  [label] varchar(10) NOT NULL,
  [x] numeric(8,2) NOT NULL,
  [y] numeric(8,2) NOT NULL,
  [is_accessible] boolean NOT NULL DEFAULT (false),
  [is_obstructed] boolean NOT NULL DEFAULT (false)
)
GO

CREATE TABLE [events] (
  [id] uuid PRIMARY KEY,
  [organizer_id] uuid NOT NULL,
  [title] varchar(300) NOT NULL,
  [slug] varchar(120) UNIQUE NOT NULL,
  [description] text,
  [category] nvarchar(255) NOT NULL CHECK ([category] IN ('concert', 'theatre', 'sports', 'comedy', 'conference', 'festival', 'other')),
  [min_age] smallint,
  [currency] char(3) NOT NULL,
  [status] nvarchar(255) NOT NULL CHECK ([status] IN ('draft', 'published', 'cancelled', 'completed')) DEFAULT 'draft',
  [published_at] timestamptz,
  [created_at] timestamptz NOT NULL DEFAULT (now())
)
GO

CREATE TABLE [performances] (
  [id] uuid PRIMARY KEY,
  [event_id] uuid NOT NULL,
  [seat_map_id] uuid NOT NULL,
  [starts_at] timestamptz NOT NULL,
  [doors_open_at] timestamptz,
  [on_sale_at] timestamptz NOT NULL,
  [off_sale_at] timestamptz,
  [status] nvarchar(255) NOT NULL CHECK ([status] IN ('scheduled', 'on_sale', 'sold_out', 'postponed', 'cancelled', 'completed')) DEFAULT 'scheduled',
  [cancelled_at] timestamptz
)
GO

CREATE TABLE [ticket_types] (
  [id] uuid PRIMARY KEY,
  [event_id] uuid NOT NULL,
  [name] varchar(100) NOT NULL,
  [description] varchar(500),
  [is_hidden] boolean NOT NULL DEFAULT (false),
  [min_per_order] smallint NOT NULL DEFAULT (1),
  [max_per_order] smallint NOT NULL DEFAULT (8),
  [sales_start_at] timestamptz,
  [sales_end_at] timestamptz,
  [sort_order] smallint NOT NULL
)
GO

CREATE TABLE [price_tiers] (
  [id] uuid PRIMARY KEY,
  [event_id] uuid NOT NULL,
  [name] varchar(100) NOT NULL,
  [color] char(7) NOT NULL,
  [sort_order] smallint NOT NULL
)
GO

CREATE TABLE [ticket_prices] (
  [ticket_type_id] uuid NOT NULL,
  [price_tier_id] uuid NOT NULL,
  [face_value_cents] integer NOT NULL,
  [service_fee_cents] integer NOT NULL DEFAULT (0),
  PRIMARY KEY ([ticket_type_id], [price_tier_id])
)
GO

CREATE TABLE [performance_seats] (
  [id] uuid PRIMARY KEY,
  [performance_id] uuid NOT NULL,
  [seat_id] uuid NOT NULL,
  [price_tier_id] uuid NOT NULL,
  [status] nvarchar(255) NOT NULL CHECK ([status] IN ('available', 'held', 'sold', 'killed', 'house_hold')) DEFAULT 'available',
  [hold_id] uuid,
  [hold_expires_at] timestamptz,
  [updated_at] timestamptz NOT NULL DEFAULT (now())
)
GO

CREATE TABLE [performance_sections] (
  [id] uuid PRIMARY KEY,
  [performance_id] uuid NOT NULL,
  [section_id] uuid NOT NULL,
  [price_tier_id] uuid NOT NULL,
  [capacity] integer NOT NULL,
  [sold_count] integer NOT NULL DEFAULT (0)
)
GO

CREATE TABLE [holds] (
  [id] uuid PRIMARY KEY,
  [user_id] uuid,
  [session_token] varchar(100) NOT NULL,
  [status] nvarchar(255) NOT NULL CHECK ([status] IN ('active', 'converted', 'released', 'expired')) DEFAULT 'active',
  [expires_at] timestamptz NOT NULL,
  [created_at] timestamptz NOT NULL DEFAULT (now())
)
GO

CREATE TABLE [hold_items] (
  [id] uuid PRIMARY KEY,
  [hold_id] uuid NOT NULL,
  [ticket_type_id] uuid NOT NULL,
  [performance_seat_id] uuid,
  [performance_section_id] uuid,
  [quantity] smallint NOT NULL DEFAULT (1)
)
GO

CREATE TABLE [promo_codes] (
  [id] uuid PRIMARY KEY,
  [organizer_id] uuid NOT NULL,
  [event_id] uuid,
  [code] varchar(40) NOT NULL,
  [discount_kind] nvarchar(255) NOT NULL CHECK ([discount_kind] IN ('percent', 'amount', 'unlock_only')),
  [discount_value] integer NOT NULL DEFAULT (0),
  [unlocks_ticket_type_id] uuid,
  [max_redemptions] integer,
  [redemption_count] integer NOT NULL DEFAULT (0),
  [valid_from] timestamptz,
  [valid_until] timestamptz
)
GO

CREATE TABLE [orders] (
  [id] uuid PRIMARY KEY,
  [order_number] varchar(20) UNIQUE NOT NULL,
  [user_id] uuid,
  [buyer_email] varchar(254) NOT NULL,
  [hold_id] uuid UNIQUE,
  [promo_code_id] uuid,
  [status] nvarchar(255) NOT NULL CHECK ([status] IN ('pending_payment', 'paid', 'partially_refunded', 'refunded', 'cancelled')) DEFAULT 'pending_payment',
  [currency] char(3) NOT NULL,
  [subtotal_cents] integer NOT NULL,
  [fees_cents] integer NOT NULL,
  [discount_cents] integer NOT NULL DEFAULT (0),
  [tax_cents] integer NOT NULL DEFAULT (0),
  [total_cents] integer NOT NULL,
  [placed_at] timestamptz,
  [created_at] timestamptz NOT NULL DEFAULT (now())
)
GO

CREATE TABLE [order_items] (
  [id] uuid PRIMARY KEY,
  [order_id] uuid NOT NULL,
  [performance_id] uuid NOT NULL,
  [ticket_type_id] uuid NOT NULL,
  [price_tier_id] uuid NOT NULL,
  [performance_seat_id] uuid,
  [performance_section_id] uuid,
  [seat_label] varchar(100),
  [face_value_cents] integer NOT NULL,
  [discount_cents] integer NOT NULL DEFAULT (0),
  [status] nvarchar(255) NOT NULL CHECK ([status] IN ('active', 'refunded', 'cancelled')) DEFAULT 'active'
)
GO

CREATE TABLE [order_fees] (
  [id] uuid PRIMARY KEY,
  [order_id] uuid NOT NULL,
  [order_item_id] uuid,
  [kind] nvarchar(255) NOT NULL CHECK ([kind] IN ('service', 'facility', 'processing', 'delivery')),
  [amount_cents] integer NOT NULL,
  [is_refundable] boolean NOT NULL
)
GO

CREATE TABLE [payments] (
  [id] uuid PRIMARY KEY,
  [order_id] uuid NOT NULL,
  [amount_cents] integer NOT NULL,
  [currency] char(3) NOT NULL,
  [status] nvarchar(255) NOT NULL CHECK ([status] IN ('pending', 'succeeded', 'failed')),
  [processor_reference] varchar(100) UNIQUE NOT NULL,
  [created_at] timestamptz NOT NULL DEFAULT (now())
)
GO

CREATE TABLE [refunds] (
  [id] uuid PRIMARY KEY,
  [order_id] uuid NOT NULL,
  [payment_id] uuid NOT NULL,
  [amount_cents] integer NOT NULL,
  [reason] nvarchar(255) NOT NULL CHECK ([reason] IN ('event_cancelled', 'customer_request', 'duplicate_order', 'fraud', 'goodwill')),
  [status] nvarchar(255) NOT NULL CHECK ([status] IN ('pending', 'succeeded', 'failed')) DEFAULT 'pending',
  [requested_by] uuid,
  [processor_reference] varchar(100),
  [created_at] timestamptz NOT NULL DEFAULT (now())
)
GO

CREATE TABLE [refund_items] (
  [refund_id] uuid NOT NULL,
  [order_item_id] uuid NOT NULL,
  [amount_cents] integer NOT NULL,
  PRIMARY KEY ([refund_id], [order_item_id])
)
GO

CREATE TABLE [tickets] (
  [id] uuid PRIMARY KEY,
  [order_item_id] uuid NOT NULL,
  [performance_id] uuid NOT NULL,
  [holder_user_id] uuid,
  [holder_name] varchar(200),
  [barcode] varchar(64) UNIQUE NOT NULL,
  [status] nvarchar(255) NOT NULL CHECK ([status] IN ('valid', 'used', 'transferred', 'void')) DEFAULT 'valid',
  [replaces_ticket_id] uuid,
  [issued_at] timestamptz NOT NULL DEFAULT (now()),
  [voided_at] timestamptz
)
GO

CREATE TABLE [ticket_transfers] (
  [id] uuid PRIMARY KEY,
  [from_ticket_id] uuid NOT NULL,
  [to_ticket_id] uuid,
  [from_user_id] uuid NOT NULL,
  [to_email] varchar(254) NOT NULL,
  [to_user_id] uuid,
  [status] nvarchar(255) NOT NULL CHECK ([status] IN ('pending', 'accepted', 'cancelled', 'expired')) DEFAULT 'pending',
  [created_at] timestamptz NOT NULL DEFAULT (now()),
  [expires_at] timestamptz NOT NULL,
  [accepted_at] timestamptz
)
GO

CREATE TABLE [scans] (
  [id] uuid PRIMARY KEY,
  [performance_id] uuid NOT NULL,
  [ticket_id] uuid,
  [barcode_read] varchar(64) NOT NULL,
  [result] nvarchar(255) NOT NULL CHECK ([result] IN ('admitted', 'already_used', 'wrong_performance', 'void_ticket', 'unknown_barcode')),
  [gate] varchar(40),
  [device_id] varchar(64) NOT NULL,
  [scanned_by] uuid,
  [scanned_at] timestamptz NOT NULL,
  [received_at] timestamptz NOT NULL DEFAULT (now())
)
GO

CREATE UNIQUE INDEX [sections_index_0] ON [sections] ("seat_map_id", "name")
GO

CREATE UNIQUE INDEX [seat_rows_index_1] ON [seat_rows] ("section_id", "label")
GO

CREATE UNIQUE INDEX [seats_index_2] ON [seats] ("row_id", "label")
GO

CREATE INDEX [performances_index_3] ON [performances] ("event_id", "starts_at")
GO

CREATE UNIQUE INDEX [performance_seats_index_4] ON [performance_seats] ("performance_id", "seat_id")
GO

CREATE INDEX [performance_seats_index_5] ON [performance_seats] ("performance_id", "status")
GO

CREATE INDEX [performance_seats_index_6] ON [performance_seats] ("hold_id")
GO

CREATE UNIQUE INDEX [performance_sections_index_7] ON [performance_sections] ("performance_id", "section_id")
GO

CREATE INDEX [hold_items_index_8] ON [hold_items] ("hold_id")
GO

CREATE INDEX [hold_items_index_9] ON [hold_items] ("performance_section_id")
GO

CREATE UNIQUE INDEX [promo_codes_index_10] ON [promo_codes] ((upper(code)), "organizer_id")
GO

CREATE INDEX [order_items_index_11] ON [order_items] ("order_id")
GO

CREATE INDEX [order_items_index_12] ON [order_items] ("performance_seat_id")
GO

CREATE INDEX [tickets_index_13] ON [tickets] ("order_item_id")
GO

CREATE INDEX [tickets_index_14] ON [tickets] ("performance_id", "status")
GO

CREATE INDEX [scans_index_15] ON [scans] ("ticket_id", "scanned_at")
GO

CREATE INDEX [scans_index_16] ON [scans] ("performance_id", "scanned_at")
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Buyers, ticket holders and organizer staff. A transfer recipient gets a row when they accept.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'users';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Consent as a timestamp, so you can prove when it was given',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'users',
@level2type = N'Column', @level2name = 'marketing_opt_in_at';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Promoters, theatres, clubs and teams that put on events. Settlement and payouts are per organizer.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'organizers';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Settlement currency for payouts',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'organizers',
@level2type = N'Column', @level2name = 'currency';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Payment processor connected-account id; bank details never live here',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'organizers',
@level2type = N'Column', @level2name = 'payout_account_token';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Organizer staff. Box office can sell and refund; door staff can only scan.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'organizer_members';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Physical places. Shared across organizers, since many promoters book the same hall.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'venues';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'IANA zone. Show times are displayed in it, wherever the buyer is.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'venues',
@level2type = N'Column', @level2name = 'timezone';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'One configuration of a venue. The same arena seats 18,000 for a concert and 6,000 for a theatre setup.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'seat_maps';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. End stage, In the round, Standing floor',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'seat_maps',
@level2type = N'Column', @level2name = 'name';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Areas of a seat map. A reserved section has rows and seats; a general admission section has only a capacity.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'sections';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. Orchestra, Balcony, Floor',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'sections',
@level2type = N'Column', @level2name = 'name';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'General admission only: how many people fit. Null for reserved sections.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'sections',
@level2type = N'Column', @level2name = 'ga_capacity';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Rows within a reserved section.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'seat_rows';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. K, AA. Not always alphabetical, so order by sort_order.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'seat_rows',
@level2type = N'Column', @level2name = 'label';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'The physical seat. It belongs to the map; whether it is for sale tonight, and at what price, belongs to performance_seats.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'seats';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. 12, 101. Odd and even sides are common.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'seats',
@level2type = N'Column', @level2name = 'label';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Position on the drawn seat map',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'seats',
@level2type = N'Column', @level2name = 'x';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Restricted view, disclosed before purchase',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'seats',
@level2type = N'Column', @level2name = 'is_obstructed';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'The show, match or conference as marketed. A three-night run is one event with three performances.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'events';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Null for all ages',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'events',
@level2type = N'Column', @level2name = 'min_age';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'One date and time of an event. Inventory, tickets and door scans are always per performance.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'performances';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Which venue and configuration; it can differ between dates of a run',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'performances',
@level2type = N'Column', @level2name = 'seat_map_id';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Who the ticket is for, and what it includes.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'ticket_types';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. Adult, Child, Student, VIP package',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'ticket_types',
@level2type = N'Column', @level2name = 'name';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Only offered when a promo code unlocks it, e.g. a presale',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'ticket_types',
@level2type = N'Column', @level2name = 'is_hidden';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Anti-scalping limit',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'ticket_types',
@level2type = N'Column', @level2name = 'max_per_order';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Seat quality bands. Each seat or GA section is placed in a tier per performance.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'price_tiers';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'e.g. Orchestra A, Balcony C, GA Floor',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'price_tiers',
@level2type = N'Column', @level2name = 'name';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Seat map legend color',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'price_tiers',
@level2type = N'Column', @level2name = 'color';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'The price matrix: ticket type by price tier. A Child ticket in Balcony C has its own price.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'ticket_prices';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Per-ticket booking fee shown before checkout',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'ticket_prices',
@level2type = N'Column', @level2name = 'service_fee_cents';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Per-performance seat inventory, one row per sellable seat, created when the performance goes on sale. A seat is claimed by one conditional UPDATE: SET status = held, hold_id = $1, hold_expires_at = now() + 10 minutes WHERE id = ANY($2) AND (status = available OR (status = held AND hold_expires_at < now())). If fewer rows changed than were asked for, someone else got a seat first and the transaction rolls back. Two buyers can never both win the same seat.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'performance_seats';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Tiers can differ per night, e.g. Saturday pricing',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'performance_seats',
@level2type = N'Column', @level2name = 'price_tier_id';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'A held seat whose hold has expired is available again, with no job needed',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'performance_seats',
@level2type = N'Column', @level2name = 'hold_expires_at';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'General admission inventory for one performance. CHECK (sold_count <= capacity). A GA claim locks this one row (SELECT ... FOR UPDATE), subtracts quantities in unexpired holds, and inserts its hold item; checkout increments sold_count. Expired holds stop counting the moment they expire, and the CHECK refuses an oversell even if app code is wrong.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'performance_sections';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Usually the section ga_capacity, lowered for a smaller show or a safety limit',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'performance_sections',
@level2type = N'Column', @level2name = 'capacity';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'A cart. Seats and GA places are reserved for a few minutes while the buyer checks out, then quietly lapse.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'holds';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Null for a guest checkout',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'holds',
@level2type = N'Column', @level2name = 'user_id';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Ties the hold to a browser session before login',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'holds',
@level2type = N'Column', @level2name = 'session_token';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Typically 10 to 15 minutes; extended once when payment starts',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'holds',
@level2type = N'Column', @level2name = 'expires_at';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'What is in the cart. Exactly one of performance_seat_id or performance_section_id is set.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'hold_items';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Reserved seating: one item per seat',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'hold_items',
@level2type = N'Column', @level2name = 'performance_seat_id';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'General admission: one item with a quantity',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'hold_items',
@level2type = N'Column', @level2name = 'performance_section_id';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Discounts and presale access codes.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'promo_codes';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Null for a code valid on every event of the organizer',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'promo_codes',
@level2type = N'Column', @level2name = 'event_id';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Basis points for percent (1500 = 15%), cents for amount, 0 for unlock only',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'promo_codes',
@level2type = N'Column', @level2name = 'discount_value';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Reveals a hidden ticket type, e.g. a fan club presale',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'promo_codes',
@level2type = N'Column', @level2name = 'unlocks_ticket_type_id';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Incremented in the checkout transaction; CHECK (redemption_count <= max_redemptions)',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'promo_codes',
@level2type = N'Column', @level2name = 'redemption_count';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'A purchase. Every total is the sum of frozen lines below it, so a later price change never alters a paid order.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'orders';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Short, human-readable, given to support over the phone',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'orders',
@level2type = N'Column', @level2name = 'order_number';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Snapshot; guests have no user row',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'orders',
@level2type = N'Column', @level2name = 'buyer_email';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'The cart this order was checked out from',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'orders',
@level2type = N'Column', @level2name = 'hold_id';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'One admission sold: the commercial record. Price, fees and refunds attach here. The barcode lives on the ticket, which can be reissued.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'order_items';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Null for general admission',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'order_items',
@level2type = N'Column', @level2name = 'performance_seat_id';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Set for general admission',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'order_items',
@level2type = N'Column', @level2name = 'performance_section_id';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Snapshot, e.g. Orchestra, Row K, Seat 12, so remapping a venue never changes a sold ticket',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'order_items',
@level2type = N'Column', @level2name = 'seat_label';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Price snapshot at checkout',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'order_items',
@level2type = N'Column', @level2name = 'face_value_cents';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Every fee line frozen at checkout: service, facility, processing, delivery. The fees shown before payment are exactly these rows.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'order_fees';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Set for per-ticket fees, null for per-order fees',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'order_fees',
@level2type = N'Column', @level2name = 'order_item_id';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Decided at sale, not at refund time',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'order_fees',
@level2type = N'Column', @level2name = 'is_refundable';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Charges against an order. A failed attempt is a row too, which fraud review needs.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'payments';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Card data stays with the payment processor',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'payments',
@level2type = N'Column', @level2name = 'processor_reference';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Money returned. A cancelled performance produces one refund per order, issued in bulk.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'refunds';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Box office staff, or null for an automatic cancellation refund',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'refunds',
@level2type = N'Column', @level2name = 'requested_by';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Which admissions a refund covers, so returning one ticket of four is exact and voids only that ticket.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'refund_items';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Face value plus any refundable fees for this admission',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'refund_items',
@level2type = N'Column', @level2name = 'amount_cents';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'The credential, not the sale. A transfer or reissue voids this row and issues a new ticket with a new barcode, so a screenshot of the old one fails at the door, while the order item (and its refund rights) stays with the original buyer.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'tickets';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Copied from the order item so the door lookup is one index hit',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'tickets',
@level2type = N'Column', @level2name = 'performance_id';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Random and unguessable, never sequential',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'tickets',
@level2type = N'Column', @level2name = 'barcode';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'The ticket this one was reissued from after a transfer',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'tickets',
@level2type = N'Column', @level2name = 'replaces_ticket_id';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Sending a ticket to a friend. The original stays valid until the recipient accepts, so an unclaimed transfer never strands a ticket.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'ticket_transfers';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Issued on acceptance',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'ticket_transfers',
@level2type = N'Column', @level2name = 'to_ticket_id';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Set when the recipient accepts and signs in',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'ticket_transfers',
@level2type = N'Column', @level2name = 'to_user_id';
GO

EXEC sp_addextendedproperty
@name = N'Table_Description',
@value = 'Append-only: every read at the door, including rejected ones. The first admitted scan lets the holder in; later ones are logged as already_used, and the gate and time of the first are on record, which ends the argument at the turnstile.',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'scans';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Null when the barcode matched nothing',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'scans',
@level2type = N'Column', @level2name = 'ticket_id';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Exactly what the scanner read',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'scans',
@level2type = N'Column', @level2name = 'barcode_read';
GO

EXEC sp_addextendedproperty
@name = N'Column_Description',
@value = 'Device clock; scanners keep working offline and sync later',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table',  @level1name = 'scans',
@level2type = N'Column', @level2name = 'scanned_at';
GO

ALTER TABLE [organizer_members] ADD FOREIGN KEY ([organizer_id]) REFERENCES [organizers] ([id])
GO

ALTER TABLE [organizer_members] ADD FOREIGN KEY ([user_id]) REFERENCES [users] ([id])
GO

ALTER TABLE [seat_maps] ADD FOREIGN KEY ([venue_id]) REFERENCES [venues] ([id])
GO

ALTER TABLE [sections] ADD FOREIGN KEY ([seat_map_id]) REFERENCES [seat_maps] ([id])
GO

ALTER TABLE [seat_rows] ADD FOREIGN KEY ([section_id]) REFERENCES [sections] ([id])
GO

ALTER TABLE [seats] ADD FOREIGN KEY ([row_id]) REFERENCES [seat_rows] ([id])
GO

ALTER TABLE [events] ADD FOREIGN KEY ([organizer_id]) REFERENCES [organizers] ([id])
GO

ALTER TABLE [performances] ADD FOREIGN KEY ([event_id]) REFERENCES [events] ([id])
GO

ALTER TABLE [performances] ADD FOREIGN KEY ([seat_map_id]) REFERENCES [seat_maps] ([id])
GO

ALTER TABLE [ticket_types] ADD FOREIGN KEY ([event_id]) REFERENCES [events] ([id])
GO

ALTER TABLE [price_tiers] ADD FOREIGN KEY ([event_id]) REFERENCES [events] ([id])
GO

ALTER TABLE [ticket_prices] ADD FOREIGN KEY ([ticket_type_id]) REFERENCES [ticket_types] ([id])
GO

ALTER TABLE [ticket_prices] ADD FOREIGN KEY ([price_tier_id]) REFERENCES [price_tiers] ([id])
GO

ALTER TABLE [performance_seats] ADD FOREIGN KEY ([performance_id]) REFERENCES [performances] ([id])
GO

ALTER TABLE [performance_seats] ADD FOREIGN KEY ([seat_id]) REFERENCES [seats] ([id])
GO

ALTER TABLE [performance_seats] ADD FOREIGN KEY ([price_tier_id]) REFERENCES [price_tiers] ([id])
GO

ALTER TABLE [performance_seats] ADD FOREIGN KEY ([hold_id]) REFERENCES [holds] ([id])
GO

ALTER TABLE [performance_sections] ADD FOREIGN KEY ([performance_id]) REFERENCES [performances] ([id])
GO

ALTER TABLE [performance_sections] ADD FOREIGN KEY ([section_id]) REFERENCES [sections] ([id])
GO

ALTER TABLE [performance_sections] ADD FOREIGN KEY ([price_tier_id]) REFERENCES [price_tiers] ([id])
GO

ALTER TABLE [holds] ADD FOREIGN KEY ([user_id]) REFERENCES [users] ([id])
GO

ALTER TABLE [hold_items] ADD FOREIGN KEY ([hold_id]) REFERENCES [holds] ([id])
GO

ALTER TABLE [hold_items] ADD FOREIGN KEY ([ticket_type_id]) REFERENCES [ticket_types] ([id])
GO

ALTER TABLE [hold_items] ADD FOREIGN KEY ([performance_seat_id]) REFERENCES [performance_seats] ([id])
GO

ALTER TABLE [hold_items] ADD FOREIGN KEY ([performance_section_id]) REFERENCES [performance_sections] ([id])
GO

ALTER TABLE [promo_codes] ADD FOREIGN KEY ([organizer_id]) REFERENCES [organizers] ([id])
GO

ALTER TABLE [promo_codes] ADD FOREIGN KEY ([event_id]) REFERENCES [events] ([id])
GO

ALTER TABLE [promo_codes] ADD FOREIGN KEY ([unlocks_ticket_type_id]) REFERENCES [ticket_types] ([id])
GO

ALTER TABLE [orders] ADD FOREIGN KEY ([user_id]) REFERENCES [users] ([id])
GO

ALTER TABLE [orders] ADD FOREIGN KEY ([hold_id]) REFERENCES [holds] ([id])
GO

ALTER TABLE [orders] ADD FOREIGN KEY ([promo_code_id]) REFERENCES [promo_codes] ([id])
GO

ALTER TABLE [order_items] ADD FOREIGN KEY ([order_id]) REFERENCES [orders] ([id])
GO

ALTER TABLE [order_items] ADD FOREIGN KEY ([performance_id]) REFERENCES [performances] ([id])
GO

ALTER TABLE [order_items] ADD FOREIGN KEY ([ticket_type_id]) REFERENCES [ticket_types] ([id])
GO

ALTER TABLE [order_items] ADD FOREIGN KEY ([price_tier_id]) REFERENCES [price_tiers] ([id])
GO

ALTER TABLE [order_items] ADD FOREIGN KEY ([performance_seat_id]) REFERENCES [performance_seats] ([id])
GO

ALTER TABLE [order_items] ADD FOREIGN KEY ([performance_section_id]) REFERENCES [performance_sections] ([id])
GO

ALTER TABLE [order_fees] ADD FOREIGN KEY ([order_id]) REFERENCES [orders] ([id])
GO

ALTER TABLE [order_fees] ADD FOREIGN KEY ([order_item_id]) REFERENCES [order_items] ([id])
GO

ALTER TABLE [payments] ADD FOREIGN KEY ([order_id]) REFERENCES [orders] ([id])
GO

ALTER TABLE [refunds] ADD FOREIGN KEY ([order_id]) REFERENCES [orders] ([id])
GO

ALTER TABLE [refunds] ADD FOREIGN KEY ([payment_id]) REFERENCES [payments] ([id])
GO

ALTER TABLE [refunds] ADD FOREIGN KEY ([requested_by]) REFERENCES [users] ([id])
GO

ALTER TABLE [refund_items] ADD FOREIGN KEY ([refund_id]) REFERENCES [refunds] ([id])
GO

ALTER TABLE [refund_items] ADD FOREIGN KEY ([order_item_id]) REFERENCES [order_items] ([id])
GO

ALTER TABLE [tickets] ADD FOREIGN KEY ([order_item_id]) REFERENCES [order_items] ([id])
GO

ALTER TABLE [tickets] ADD FOREIGN KEY ([performance_id]) REFERENCES [performances] ([id])
GO

ALTER TABLE [tickets] ADD FOREIGN KEY ([holder_user_id]) REFERENCES [users] ([id])
GO

ALTER TABLE [tickets] ADD FOREIGN KEY ([replaces_ticket_id]) REFERENCES [tickets] ([id])
GO

ALTER TABLE [ticket_transfers] ADD FOREIGN KEY ([from_ticket_id]) REFERENCES [tickets] ([id])
GO

ALTER TABLE [ticket_transfers] ADD FOREIGN KEY ([to_ticket_id]) REFERENCES [tickets] ([id])
GO

ALTER TABLE [ticket_transfers] ADD FOREIGN KEY ([from_user_id]) REFERENCES [users] ([id])
GO

ALTER TABLE [ticket_transfers] ADD FOREIGN KEY ([to_user_id]) REFERENCES [users] ([id])
GO

ALTER TABLE [scans] ADD FOREIGN KEY ([performance_id]) REFERENCES [performances] ([id])
GO

ALTER TABLE [scans] ADD FOREIGN KEY ([ticket_id]) REFERENCES [tickets] ([id])
GO

ALTER TABLE [scans] ADD FOREIGN KEY ([scanned_by]) REFERENCES [users] ([id])
GO

How to use this template

  1. Open the ER diagram to see the tables and how they relate.
  2. Copy it into your workspace. You get your own editable copy, in DBML.
  3. Export it as PostgreSQL, MySQL, or SQL Server DDL, or as Prisma, Drizzle, SQLAlchemy, TypeScript, Zod or C# models.

The copy opens in DB Planner’s ER diagram tool, with the schema as text in the DBML editor beside it. Have a database already? Turn its SQL into an ER diagram instead.

Frequently asked questions

Which table is at the centre of the Event Ticketing database schema?
users. It is referenced by 7 other tables: holds, orders, organizer_members, refunds, scans and 2 more.
How is holds related to users?
holds.user_id references users.id, a many to one relationship. All 56 relationships in the schema are listed on this page.
Which table in the Event Ticketing database schema has the most columns?
orders, with 15 columns, followed by events (11) and order_items (11). The schema has 203 columns across 27 tables.
Can I export it to PostgreSQL or MySQL?
Yes. Every DB Planner template exports as DBML, as PostgreSQL, MySQL, or SQL Server DDL, and as C# models.
Is this template free?
Yes. You can open it in the browser without an account, and copy it into your own workspace with a free DB Planner account.
Can I change it after copying it?
Yes. A copy is yours: you can rename tables, add columns, and export it. Changes the author makes later do not affect your copy.