…
…
Build the hall, event and ticket tables with their constraints, load the data, write an occupancy view, sell and cancel safely, and give the box office only the permissions it needs.
Student clubs want to sell tickets for events on campus. So far, tickets were kept by hand in one table: the same seat was sold to two people, a canceled seat could not be sold again, and nobody knew whether a hall was full. In this project, you will build a small but reliable ticket database from scratch.
Ready data: only the person table (id, first_name, last_name, email; 15 people). You will build the hall, event and ticket tables yourself.
Lessons used: subqueries (20–21), creating tables and constraints (28–30), INSERT and UPDATE (34–35), views (38), indexes (39), permissions (40). No new concept is taught; the only new piece, a partial unique index, comes ready in the cell.
Project cells are persistent: every command you run here stays in the database; it is not rolled back like in lesson exercises. That is why every cell is written so that it can be rebuilt from scratch: the cell that creates tables first removes the old ones with DROP TABLE IF EXISTS, and the cell that loads data first empties the tables. You can rerun a cell as often as you like; if you rerun an earlier cell, run the cells after it again, in order. Do not change the -- Check query lines at the end of the cells.
What you'll do
people.sqlDownload file
Reading file…
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
Start with two tables. Every hall has a name and a capacity; every event takes place in one hall, on one date, for one price. The rules: the hall name is unique, the capacity is positive, the event's hall must exist in the hall table, and the price cannot be negative (free events cost 0).
You decide which constraint writes each of these rules; the comment in the cell only lists the columns and requirements. Use the column names and types exactly; the check query looks at them, at the constraints and at the CHECK conditions. In a CHECK condition, write the column on the left and the limit as an integer on the right (as in capacity > 0).
This step's code cell is for members.Sign up free
The ticket table refers to two tables: event and person. A ticket's status can be sold or canceled. The main rule is this: the same seat at the same event cannot be sold twice, but a canceled seat must be sellable again. A simple UNIQUE (event_id, seat) would also have blocked reselling a canceled seat.
The solution is a partial unique index: it works like UNIQUE, but it checks uniqueness only among the rows that match the WHERE, here the tickets with status = 'sold'
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
ticketThis step's code cell is for members.Sign up free
The tables are ready; now you will load the data. The cell first empties the three tables with TRUNCATE ... RESTART IDENTITY, so ticket numbers start from the beginning when you rerun it. The halls, the events and the tickets of two events are ready.
The missing part is the tickets of Jazz Night (event 2): each of the people numbered 1–12 took the seat with the same number as their own. Add these 12 tickets with a single INSERT ... SELECT command.
This step's code cell is for members.Sign up free
At the box office everyone asks the same question: how many seats are left for this event? Instead of writing the calculation every time, you will save it as a view. The number of sold tickets must be calculated only from tickets with the sold status; canceled ones free the seat. An event with no tickets yet must also appear with 0 sold, so you LEFT JOIN the tickets and write the status condition in ON, not in WHERE.
Write the view yourself: its columns are id, name, capacity, sold and available (capacity − sold); then list the view in id order.
This step's code cell is for members.Sign up free
Person 13 wants tickets for two events: Jazz Night and the Short Film Screening. No rule in the table checks the capacity; a CHECK constraint cannot look at a capacity in another table. You will put the check into the sale itself: in the form INSERT ... SELECT ... WHERE, the SELECT produces a row only if the view shows a free seat; if there is none, no row is added.
The Short Film sale is ready. You write the Jazz Night sale: if the number of available seats for event 2 is greater than 0, sell seat 13 to person 13. The ON CONFLICT DO NOTHING at the end skips the row, instead of raising an error, when the row to be added hits a uniqueness rule (here the ticket_seat index); so when the cell runs a second time, the same seat is not sold again and the cell does not fail. In the end Jazz Night must stay full (available 0), and one more ticket must have been sold for the Short Film.
In a real system, so that two box offices do not sell the same last seat at the same time, this check and the sale are done in a transaction, locking the row (lesson 37).
This step's code cell is for members.Sign up free
Person 3 canceled their Jazz Night ticket. The ticket is not deleted; its status becomes canceled, so the record history is kept. Because seat 3 is free now, it can be sold to person 13: the partial index looks only at sold tickets, so the canceled ticket does not get in the way.
The ON CONFLICT DO NOTHING in the INSERT prevents the same seat from being sold again when the cell runs a second time. Write the UPDATE that cancels.
This step's code cell is for members.Sign up free
In the last step you will prepare a revenue report for the clubs and speed up the box office's "this person's tickets" search. Foreign key columns are not indexed automatically (lesson 39); add an index for person_id. IF NOT EXISTS prevents an error when the cell is rerun.
After creating the index, show each event's name, the number of sold tickets (tickets) and its revenue (revenue = number of tickets × price). An event with no tickets must also appear. Sort by revenue from high to low, and by event id when revenue is equal.
This step's code cell is for members.Sign up free
The system is ready; now the box office staff will use it. Staff sell tickets (INSERT), cancel them (UPDATE) and look at tickets (SELECT); they also answer the free-seat question from the occupancy view. Deleting tickets is not their job: even a cancellation is done without deleting. Following the principle of least privilege, you will give the box office only the permissions these jobs need (lesson 40).
Create a role named box_office; give it SELECT, INSERT and UPDATE on the ticket table, and only SELECT on the event_occupancy view. Do not give DELETE. The ready lines at the start of the cell remove the old role together with its permissions when you rerun the cell; so CREATE ROLE does not fail the second time either. The check query lists the granted permissions from the information_schema.role_table_grants view and asks whether the box office can delete tickets.
This step's code cell is for members.Sign up free