
Code
Join the Delivery Log
Writing 'Amber Isle' on every delivery is risky. One typo
('Amber isle') and the totals split. Renaming the island means
changing hundreds of rows.
So databases split data into tables that point at each other:
islands(id, name): each island once, with an id number.deliveries(id, island_id, courier, crates): each delivery stores the island's id, not its name.
A column declared id INTEGER PRIMARY KEY gives every row its own
unique number. If you leave it out of an INSERT, SQLite picks the
next number for you.
JOIN glues matching rows together
SELECT i.name, d.crates
FROM deliveries AS d
JOIN islands AS i ON i.id = d.island_id
For every delivery, JOIN ... ON finds the island row whose id
matches d.island_id and joins them into one wide row. AS d and
AS i are short nicknames, so d.crates means "the crates column
of deliveries".
SELECT DISTINCT removes duplicate result rows.
Assignment
delivery_log(conn)returns a list of(island_name, courier, crates)for every delivery, in deliveryidorder.courier_islands(conn, courier)returns a list of the different island names (strings) that courier delivered to, A to Z. Use a?placeholder andDISTINCT.
The program should print:
[('Amber Isle', 'Wren', 5), ('Gull Rock', 'Pip', 9), ('Amber Isle', 'Pip', 2)]
['Amber Isle', 'Gull Rock']delivery_log(conn)returns a list of(island_name, courier, crates)for every delivery, in deliveryidorder.courier_islands(conn, courier)returns a list of the different island names (strings) that courier delivered to, A to Z. Use a?placeholder andDISTINCT.
The program should print:
[('Amber Isle', 'Wren', 5), ('Gull Rock', 'Pip', 9), ('Amber Isle', 'Pip', 2)]
['Amber Isle', 'Gull Rock']main.py Python sleeping
Press Run to try your code, then Check to test it.