Goal: write a small set of rows safely and use RETURNING to see what changed.
Connect as the dataset owner beer_db. This lab adds practice styles and three beers in this lesson’s disposable overlay.
In the connection command, -U beer_db selects the SQL role and -d beer_db selects the database. Expect the psql welcome message and a beer_db=> prompt; run the following SQL there.
Goal: insert one style and inspect the actual inserted identity.
Add New England IPA with the supplied ABV and IBU ranges. The [) notation includes the lower bound and excludes the upper bound. RETURNING id, name displays the generated UUID and name; your UUID will differ from the example.
numrange constructs a numeric interval; the ranges describe ABV and bitterness bounds, not two separate values. The omitted id uses the table’s default. Expect one returned row and INSERT 0 1; the final number counts inserted rows, while the zero is a legacy command-tag field.
Goal: handle an existing unique name without inserting a duplicate.
Try to insert "New England IPA" again. Without ON CONFLICT it would fail with a unique violation. Add ON CONFLICT (name) DO NOTHING — the duplicate is silently skipped.
(name) selects the unique conflict key. RETURNING has nothing to report for a skipped row: expect (0 rows) and INSERT 0 0, with the original style still present. No unique-violation error is expected in this command.
Goal: preview the target and update only that style.
Preview the New England IPA row by name, then update its description with the same WHERE clause. RETURNING confirms one row changed. Filter by the unique style name.
SET supplies the replacement description; WHERE limits which row receives it. Expect the previewed name, then that name with its new description and UPDATE 1.
Goal: delete the previewed row and verify the removal action.
Preview the same named row, then delete it with RETURNING name. The output names the removed style and DELETE 1 confirms one deletion.
RETURNING reports values from the deleted row; it does not mean the row remains stored. The unique-name predicate targets the practice style, and DELETE 1 counts affected rows.
Goal: insert multiple rows together and remove exactly those practice rows.
Add Brut IPA and Cold IPA with one INSERT VALUES statement. RETURNING shows both names and INSERT 0 2 confirms two inserts. Remove those two practice rows afterwards.
Each parenthesized tuple in VALUES supplies one row. The cleanup uses IN to name both targets; expect both names again and DELETE 2.
Goal: insert rows produced by a SELECT rather than individual INSERT commands.
Insert 3 new beers into the beers table using INSERT … SELECT. Copy the brewery_id of "Golden Brewery" using a subquery — no need to look up the UUID manually. The new beers should be: "Hazy Pils" (style "German Pilsner", abv 4.9), "Dark Wave" (style "American Stout", abv 5.8), and "Citrus Burst" (style "American IPA", abv 6.2). Use RETURNING name, style, abv to confirm all three were inserted. The VALUES list becomes a three-row query source; the brewery subquery returns one ID because brewery names are unique.
AS v(name, style, abv) labels the VALUES source and columns. The destination column list and selected expressions correspond by position. Expect three returned beer rows and INSERT 0 3. These rows remain for the rest of this exercise.