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 "circuits" (
"circuitId" INT UNIQUE,
"name" TEXT,
PRIMARY KEY ("circuitId")
);

CREATE TABLE "constructors" (
"constructorId" INT,
"name" TEXT,
PRIMARY KEY ("constructorId")
);

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

CREATE TABLE "qualifying" (
"qualifyId" INT,
"raceId" INT,
"driverId" INT,
"position" INT,
"q3" TEXT,
PRIMARY KEY ("qualifyId"),
FOREIGN KEY ("raceId") REFERENCES "races"("raceId"),
FOREIGN KEY ("driverId") REFERENCES "drivers"("driverId")
);

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

CREATE TABLE "results" (
"resultId" INT,
"position" INT,
PRIMARY KEY ("resultId")
);



INSERT INTO "circuits" VALUES ("1", "Marina Bay Street Circuit");
INSERT INTO "circuits" VALUES ("2", "Silverstone Circuit");
INSERT INTO "circuits" VALUES ("3", "Monza Circuit");
INSERT INTO "constructors" VALUES ("1", "Mercedes");
INSERT INTO "constructors" VALUES ("2", "Ferrari");
INSERT INTO "constructors" VALUES ("3", "Red Bull Racing");
INSERT INTO "drivers" VALUES ("1", "Lewis", "Hamilton");
INSERT INTO "drivers" VALUES ("2", "Kimi", "Raikkonen");
INSERT INTO "drivers" VALUES ("3", "Sebastian", "Vettel");
INSERT INTO "drivers" VALUES ("4", "Fernando", "Alonso");
INSERT INTO "races" VALUES ("1", "2008", "1");
INSERT INTO "races" VALUES ("2", "2008", "2");
INSERT INTO "races" VALUES ("3", "2009", "1");
INSERT INTO "races" VALUES ("4", "2008", "3");
INSERT INTO "qualifying" VALUES ("1", "1", "1", "1", "1:40.500");
INSERT INTO "qualifying" VALUES ("2", "1", "2", "2", "1:41.000");
INSERT INTO "qualifying" VALUES ("3", "1", "3", "3", "1:41.500");
INSERT INTO "qualifying" VALUES ("4", "2", "1", "1", "1:30.000");
INSERT INTO "qualifying" VALUES ("5", "2", "4", "2", "1:30.500");
INSERT INTO "qualifying" VALUES ("6", "3", "3", "1", "1:39.800");
INSERT INTO "qualifying" VALUES ("7", "1", "4", "4", "1:42.000");
INSERT INTO "qualifying" VALUES ("8", "1", "1", "5", "1:42.500");
INSERT INTO "qualifying" VALUES ("9", "1", "3", "1", "1:39.900");
INSERT INTO "qualifying" VALUES ("10", "1", "2", "1", "NULL");
INSERT INTO "qualifying" VALUES ("11", "1", "4", "1", "1:40.000");