PRAGMA foreign_keys = ON;

PRAGMA writable_schema = 1;
DELETE FROM sqlite_master WHERE type IN ("table", "index", "trigger");
PRAGMA writable_schema = 0;
VACUUM;

CREATE TABLE "lapTimes" (
"raceId" INT,
"driverId" INT,
"lap" INT,
"time" TEXT,
PRIMARY KEY ("raceId", "driverId", "lap")
);

CREATE TABLE "pitStops" (
"raceId" INT,
"driverId" INT,
"stop" INT,
"duration" TEXT,
PRIMARY KEY ("raceId", "driverId", "stop"),
FOREIGN KEY ("raceId") REFERENCES "races"("raceId"),
FOREIGN KEY ("driverId") REFERENCES "drivers"("driverId")
);

CREATE TABLE "circuits" (
"circuitId" INT UNIQUE,
"name" TEXT,
PRIMARY KEY ("circuitId")
);

CREATE TABLE "drivers" (
"driverId" INT UNIQUE,
"forename" TEXT,
"surname" TEXT,
PRIMARY KEY ("driverId")
);

CREATE TABLE "qualifying" (
"qualifyId" INT,
"position" INT,
PRIMARY KEY ("qualifyId")
);

CREATE TABLE "races" (
"raceId" INT UNIQUE,
"year" INT,
"circuitId" INT,
"name" TEXT,
PRIMARY KEY ("raceId"),
FOREIGN KEY ("circuitId") REFERENCES "circuits"("circuitId")
);

CREATE TABLE "results" (
"resultId" INT,
"raceId" INT,
"driverId" INT,
"fastestLapTime" TEXT,
PRIMARY KEY ("resultId"),
FOREIGN KEY ("raceId") REFERENCES "races"("raceId"),
FOREIGN KEY ("driverId") REFERENCES "drivers"("driverId")
);



INSERT INTO "circuits" VALUES ("1", "Circuit de Monaco");
INSERT INTO "circuits" VALUES ("2", "Silverstone Circuit");
INSERT INTO "circuits" VALUES ("3", "Red Bull Ring");
INSERT INTO "circuits" VALUES ("4", "Autodromo Nazionale Monza");
INSERT INTO "circuits" VALUES ("5", "Circuit de Spa-Francorchamps");
INSERT INTO "drivers" VALUES ("1", "Lewis", "Hamilton");
INSERT INTO "drivers" VALUES ("2", "Max", "Verstappen");
INSERT INTO "drivers" VALUES ("3", "Charles", "Leclerc");
INSERT INTO "drivers" VALUES ("4", "Sergio", "Perez");
INSERT INTO "drivers" VALUES ("5", "Valtteri", "Bottas");
INSERT INTO "races" VALUES ("101", "2023", "1", "Monaco Grand Prix");
INSERT INTO "races" VALUES ("102", "2023", "3", "Austrian Grand Prix");
INSERT INTO "races" VALUES ("103", "2023", "2", "British Grand Prix");
INSERT INTO "races" VALUES ("104", "2022", "3", "Austrian Grand Prix");
INSERT INTO "races" VALUES ("105", "2022", "4", "Italian Grand Prix");
INSERT INTO "lapTimes" VALUES ("102", "1", "10", "1:07.000");
INSERT INTO "lapTimes" VALUES ("102", "2", "15", "1:06.500");
INSERT INTO "lapTimes" VALUES ("102", "3", "12", "1:07.200");
INSERT INTO "lapTimes" VALUES ("104", "1", "11", "1:07.100");
INSERT INTO "lapTimes" VALUES ("104", "2", "14", "1:06.800");
INSERT INTO "lapTimes" VALUES ("104", "3", "13", "1:07.500");
INSERT INTO "lapTimes" VALUES ("101", "1", "5", "1:12.000");
INSERT INTO "lapTimes" VALUES ("103", "2", "8", "1:28.000");
INSERT INTO "pitStops" VALUES ("102", "1", "1", "23.5s");
INSERT INTO "pitStops" VALUES ("102", "2", "1", "22.0s");
INSERT INTO "pitStops" VALUES ("102", "3", "1", "24.1s");
INSERT INTO "pitStops" VALUES ("104", "1", "1", "23.0s");
INSERT INTO "pitStops" VALUES ("104", "2", "1", "22.5s");
INSERT INTO "pitStops" VALUES ("104", "3", "1", "24.0s");
INSERT INTO "pitStops" VALUES ("102", "2", "2", "25.0s");
INSERT INTO "pitStops" VALUES ("101", "1", "1", "21.0s");
INSERT INTO "results" VALUES ("1", "102", "1", "1:07.000");
INSERT INTO "results" VALUES ("2", "102", "2", "1:06.500");
INSERT INTO "results" VALUES ("3", "102", "3", "1:07.200");
INSERT INTO "results" VALUES ("4", "104", "1", "1:07.100");
INSERT INTO "results" VALUES ("5", "104", "2", "1:06.800");
INSERT INTO "results" VALUES ("6", "104", "3", "1:07.500");
INSERT INTO "results" VALUES ("7", "101", "1", "1:12.000");
INSERT INTO "results" VALUES ("8", "103", "2", "1:28.000");
INSERT INTO "qualifying" VALUES ("1", "1");
INSERT INTO "qualifying" VALUES ("2", "2");
INSERT INTO "qualifying" VALUES ("3", "3");