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 "Documents" (
"Document_ID" INT,
"Template_ID" INT,
"Document_Name" VARCHAR(255),
PRIMARY KEY ("Document_ID"),
FOREIGN KEY ("Template_ID") REFERENCES "Templates"("Template_ID")
);

CREATE TABLE "Ref_Template_Types" (
"Template_Type_Code" CHAR(15) UNIQUE,
"Template_Type_Description" VARCHAR(255),
PRIMARY KEY ("Template_Type_Code")
);

CREATE TABLE "Templates" (
"Template_ID" INT UNIQUE,
"Version_Number" INT,
"Template_Type_Code" CHAR(15),
PRIMARY KEY ("Template_ID"),
FOREIGN KEY ("Template_Type_Code") REFERENCES "Ref_Template_Types"("Template_Type_Code")
);





INSERT INTO "Ref_Template_Types" VALUES ("TYPE_A", "Type A Description");
INSERT INTO "Ref_Template_Types" VALUES ("TYPE_B", "Type B Description");
INSERT INTO "Ref_Template_Types" VALUES ("TYPE_C", "Type C Description");
INSERT INTO "Ref_Template_Types" VALUES ("TYPE_D", "Type D Description");
INSERT INTO "Ref_Template_Types" VALUES ("TYPE_E", "Type E Description");
INSERT INTO "Templates" VALUES ("1", "1", "TYPE_A");
INSERT INTO "Templates" VALUES ("2", "1", "TYPE_B");
INSERT INTO "Templates" VALUES ("3", "2", "TYPE_B");
INSERT INTO "Templates" VALUES ("4", "1", "TYPE_C");
INSERT INTO "Templates" VALUES ("5", "3", "TYPE_D");
INSERT INTO "Templates" VALUES ("6", "1", "TYPE_E");
INSERT INTO "Documents" VALUES ("101", "1", "Document 1 using TYPE_A");
INSERT INTO "Documents" VALUES ("102", "3", "Document 2 using TYPE_B version 2");
INSERT INTO "Documents" VALUES ("103", "5", "Document 3 using TYPE_D");
INSERT INTO "Documents" VALUES ("104", "5", "Document 4 using TYPE_D again");
INSERT INTO "Documents" VALUES ("105", "3", "Document 5 using TYPE_B version 2 again");