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 "constructors" (
"constructorId" INT,
"name" TEXT,
PRIMARY KEY ("constructorId")
);

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

CREATE TABLE "races" (
"raceId" INT UNIQUE,
"year" INT,
"name" TEXT,
"date" DATE,
PRIMARY KEY ("raceId")
);

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



INSERT INTO "constructors" VALUES ("1", "Ferrari");
INSERT INTO "constructors" VALUES ("2", "McLaren");
INSERT INTO "constructors" VALUES ("3", "BMW Sauber");
INSERT INTO "drivers" VALUES ("101");
INSERT INTO "drivers" VALUES ("102");
INSERT INTO "drivers" VALUES ("103");
INSERT INTO "drivers" VALUES ("104");
INSERT INTO "drivers" VALUES ("105");
INSERT INTO "drivers" VALUES ("106");
INSERT INTO "races" VALUES ("201", "2008", "Chinese Grand Prix", "2008-10-05");
INSERT INTO "races" VALUES ("202", "2007", "Australian Grand Prix", "2007-03-18");
INSERT INTO "races" VALUES ("203", "2009", "Monaco Grand Prix", "2009-05-24");
INSERT INTO "races" VALUES ("204", "2008", "British Grand Prix", "2008-07-06");
INSERT INTO "results" VALUES ("301", "201", "101", "1", "1:32:45.123");
INSERT INTO "results" VALUES ("302", "201", "102", "2", "1:33:10.456");
INSERT INTO "results" VALUES ("303", "201", "103", "3", "1:33:45.789");
INSERT INTO "results" VALUES ("304", "201", "104", "4", "NULL");
INSERT INTO "results" VALUES ("305", "201", "105", "5", "1:35:00.000");
INSERT INTO "results" VALUES ("306", "201", "106", "6", "1:35:30.500");
INSERT INTO "results" VALUES ("307", "202", "101", "5", "1:30:00.000");
INSERT INTO "results" VALUES ("308", "203", "102", "7", "1:40:00.000");
INSERT INTO "results" VALUES ("309", "204", "105", "2", "1:28:00.000");