BEGIN TRANSACTION;
DROP TABLE IF EXISTS "Genre";
CREATE TABLE "Genre" (
	"ID"	INTEGER,
	"Genre"	TEXT NOT NULL,
	PRIMARY KEY("ID" AUTOINCREMENT)
);
DROP TABLE IF EXISTS "Herausgeber";
CREATE TABLE "Herausgeber" (
	"ID"	INTEGER,
	"Name"	TEXT NOT NULL,
	PRIMARY KEY("ID" AUTOINCREMENT)
);
DROP TABLE IF EXISTS "Pseudonym";
CREATE TABLE "Pseudonym" (
	"ID"	INTEGER,
	"Pseudonym"	TEXT NOT NULL,
	PRIMARY KEY("ID" AUTOINCREMENT)
);
DROP TABLE IF EXISTS "Reihe";
CREATE TABLE "Reihe" (
	"ID"	INTEGER,
	"Titel"	TEXT NOT NULL,
	"Verlag"	TEXT,
	PRIMARY KEY("ID" AUTOINCREMENT),
	FOREIGN KEY("Verlag") REFERENCES "Verlag"("ID")
);
DROP TABLE IF EXISTS "Sprache";
CREATE TABLE "Sprache" (
	"ID"	INTEGER,
	"Sprache"	TEXT NOT NULL,
	PRIMARY KEY("ID" AUTOINCREMENT)
);
DROP TABLE IF EXISTS "Text";
CREATE TABLE "Text" (
	"ID"	INTEGER,
	"Titel"	TEXT NOT NULL,
	"Untertitel"	TEXT,
	"Reihe"	INTEGER,
	"Textform"	INTEGER,
	"Sprache"	INTEGER,
	PRIMARY KEY("ID" AUTOINCREMENT),
	FOREIGN KEY("Reihe") REFERENCES "Reihe"("ID"),
	FOREIGN KEY("Sprache") REFERENCES "Sprache"("ID"),
	FOREIGN KEY("Textform") REFERENCES "Textform"("ID")
);
DROP TABLE IF EXISTS "Text_Genre";
CREATE TABLE "Text_Genre" (
	"Text"	INTEGER,
	"Genre"	INTEGER,
	PRIMARY KEY("Text","Genre"),
	FOREIGN KEY("Genre") REFERENCES "Genre"("ID"),
	FOREIGN KEY("Text") REFERENCES "Text"("ID")
);
DROP TABLE IF EXISTS "Textform";
CREATE TABLE "Textform" (
	"ID"	INTEGER,
	"Textform"	TEXT NOT NULL,
	PRIMARY KEY("ID" AUTOINCREMENT)
);
DROP TABLE IF EXISTS "Titelbild";
CREATE TABLE "Titelbild" (
	"ID"	INTEGER,
	"Mimetype"	TEXT NOT NULL,
	"Dateiname"	TEXT NOT NULL,
	"Dateigroesse"	INTEGER NOT NULL,
	"Breite"	INTEGER NOT NULL,
	"Hoehe"	INTEGER NOT NULL,
	"Bild"	BLOB NOT NULL,
	"Thumbnail"	BLOB NOT NULL,
	"sha256"	TEXT NOT NULL UNIQUE,
	PRIMARY KEY("ID" AUTOINCREMENT)
);
DROP TABLE IF EXISTS "Verlag";
CREATE TABLE "Verlag" (
	"ID"	INTEGER,
	"Verlag"	TEXT NOT NULL,
	PRIMARY KEY("ID" AUTOINCREMENT)
);
DROP TABLE IF EXISTS "Veroeffentlichung";
CREATE TABLE "Veroeffentlichung" (
	"ID"	INTEGER,
	"Text"	INTEGER NOT NULL,
	"Werk"	INTEGER NOT NULL,
	"AltTitel"	TEXT,
	"AltUntertitel"	TEXT,
	"Pseudonym"	INTEGER NOT NULL,
	PRIMARY KEY("ID" AUTOINCREMENT),
	FOREIGN KEY("Pseudonym") REFERENCES "Pseudonym"("ID"),
	FOREIGN KEY("Text") REFERENCES "Text"("ID"),
	FOREIGN KEY("Werk") REFERENCES "Werk"("ID")
);
DROP TABLE IF EXISTS "Werk";
CREATE TABLE "Werk" (
	"ID"	INTEGER,
	"Titel"	TEXT NOT NULL,
	"Untertitel"	TEXT,
	"Werksform"	INTEGER,
	"Verlag"	INTEGER,
	"Reihe"	INTEGER,
	"Reihennummer"	TEXT,
	"Erscheinungsdatum"	TEXT,
	"ISBN_13"	TEXT,
	"ISBN_10"	TEXT,
	"ISSN"	TEXT,
	"Preis"	TEXT,
	"Titelbild"	INTEGER,
	"Klappentext"	TEXT,
	"Anmerkungen"	TEXT,
	PRIMARY KEY("ID" AUTOINCREMENT),
	FOREIGN KEY("Reihe") REFERENCES "Reihe"("ID"),
	FOREIGN KEY("Titelbild") REFERENCES "Titelbild"("ID"),
	FOREIGN KEY("Verlag") REFERENCES "Verlag"("ID"),
	FOREIGN KEY("Werksform") REFERENCES "Werksform"("ID")
);
DROP TABLE IF EXISTS "Werk_Genre";
CREATE TABLE "Werk_Genre" (
	"Werk"	INTEGER,
	"Genre"	INTEGER,
	PRIMARY KEY("Werk","Genre"),
	FOREIGN KEY("Genre") REFERENCES "Genre"("ID"),
	FOREIGN KEY("Werk") REFERENCES "Werk"("ID")
);
DROP TABLE IF EXISTS "Werk_Herausgeber";
CREATE TABLE "Werk_Herausgeber" (
	"Herausgeber"	INTEGER,
	"Werk"	INTEGER,
	PRIMARY KEY("Herausgeber","Werk"),
	FOREIGN KEY("Herausgeber") REFERENCES "Herausgeber"("ID"),
	FOREIGN KEY("Werk") REFERENCES "Werk"("ID")
);
DROP TABLE IF EXISTS "Werksform";
CREATE TABLE "Werksform" (
	"ID"	INTEGER,
	"Werksform"	TEXT NOT NULL,
	PRIMARY KEY("ID" AUTOINCREMENT)
);
COMMIT;
