Goal: Prove that foreign keys reject missing parents, cascade through a dedicated test chain, and protect other referenced rows from deletion.
Before exploring foreign keys, connect to beer_db. Run psql -U beer_db -d beer_db to open a psql session.
Inspect both \d beers and \d beer_ratings. A beer references its brewery; a rating references its beer. Find ON DELETE CASCADE on each of those two relationships. Also notice that other tables reference beers without CASCADE: not every branch follows the same deletion policy.
Insert a rating using the provided all-zero UUID, which has no beer in this dataset. All other required values are valid. Expect beer_ratings_beer_id_fkey and “not present in table beers”; no rating is saved.
Create one separately named test brewery, beer, and rating. Each provided subquery finds the parent just inserted by its distinctive name. Before deleting, the supplied COUNT report must show one row at each level; COUNT(*) is a preview that returns the number of matching records. Delete only Cascade Test Brewery, then repeat the same report and expect 0 / 0 / 0. DELETE 1 counts directly deleted brewery rows, not all cascading children. These test rows have no festival/tap references, so unrelated NO ACTION branches cannot block cleanup.
Find a real beer referenced by festival_appearances, then attempt to delete that same beer. The provided subquery uses a sorted beer ID for repeatability. Expect a foreign-key error from either festival_appearances or tap_assignments if both reference it. Those links use default NO ACTION, not explicit RESTRICT: both block this ordinary immediate DELETE, but differ when constraint checks can be deferred.