Databases and big data · A level · OCR H446 1.3.2, AQA 7517 4.10.1, Eduqas A500QS 2.5 · about 75 min
Design, normalise, build and fill a database of robot runs, log live runs in transactions, report with one joined query and export the report as JSON.
[1 mark]Why does the project's Run table not store each run's error?
[1 mark]A report query joins runs, robots, teams and challenges. How many JOIN clauses does it need?
[1 mark]What does this print?
import sqlite3
db = sqlite3.connect(":memory:")
db.execute("CREATE TABLE runs (team TEXT, cm INTEGER, target INTEGER)")
db.executemany("INSERT INTO runs VALUES (?, ?, ?)", [("Hawks", 27, 30), ("Hawks", 31, 30), ("Owls", 16, 15)])
for row in db.execute("SELECT team, COUNT(*), AVG(ABS(cm - target)) FROM runs GROUP BY team ORDER BY team"):
print(*row)[1 mark]Put the stages of building the run database in order.
Number the lines 1 to 5 to put them in the right order.
Query the tables to produce the reportInsert the data and log new runs in transactionsNormalise the design to third normal formCreate the tables, parents before childrenIdentify the entities, keys and relationships from the brief[1 mark]Ada's run is logged inside a transaction (with db:) and the program crashes halfway through the insert. What is in the database?
The starter gives the data as lists of tuples: teams holds (team_id, team_name), robots holds (robot_id, name, team_id), challenges holds (code, name, target_cm) and earlier_runs holds (robot_id, code, cm). Ids and distances are whole numbers; names and codes are text.
1. Create the four tables of the design, with a primary key on each and the three foreign keys declared with REFERENCES, and switch on foreign key checking.
2. Insert the data lists. Let the database number the runs.
3. For each challenge, in the order of challenges, the robot (Ada, robot 1) makes one run at speed 50: S drives forward the target distance, R drives backward the target distance. Measure the straight-line distance really covered, rounded to a whole number, and insert the run inside its own transaction (with db:).
4. With one query that joins runs, robots, teams and challenges, groups by team, and uses COUNT and AVG, print one line per team in name order: <team_name>: <n> runs, mean error <error> cm, where the error is the mean of the absolute differences between each run's cm and its challenge's target_cm, rounded to 1 decimal place. For example Owls: 2 runs, mean error 2.5 cm.
5. Save the report to report.json with json.dump, as a list with one object per team, each with the keys team (text), runs (a whole number) and mean_error (a number). Then print saved report.json.
Three lines in all.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
import sqlite3
import json
teams = [(1, "Hawks"), (2, "Owls")]
robots = [(1, "Ada", 1), (2, "Bolt", 1), (3, "Cog", 2)]
challenges = [("S", "sprint", 30), ("R", "reverse", 15)]
earlier_runs = [(2, "S", 27), (2, "R", 16), (3, "S", 33), (3, "R", 13)]Plan your program here, then type it in and press Run.
SELECT or a second GROUP BY.