CREATE TABLE composer (
    id INTEGER NOT NULL PRIMARY KEY,
    first_name VARCHAR(255) NOT NULL,
    name VARCHAR(255) NOT NULL,
    date_of_birth DATE NOT NULL,
    date_of_death DATE
);

CREATE TABLE work (
    id INTEGER NOT NULL PRIMARY KEY,
    comp_id INTEGER NOT NULL,
    year INTEGER,
    opus VARCHAR(15),
    collection VARCHAR(127),
    main_key VARCHAR(15),
    title VARCHAR(127),
    mov_title VARCHAR(127),
    alias VARCHAR(127),
    work_directory VARCHAR(31),
    wd_number VARCHAR(15),
    FOREIGN KEY(comp_id) REFERENCES composer(id)
);

CREATE TABLE movement (
    work_id INTEGER NOT NULL,
    mov_number INTEGER NOT NULL,
    numb VARCHAR(15),
    designation VARCHAR(63),
    mus_key VARCHAR(15),
    recording BLOB,
    PRIMARY KEY(work_id, mov_number),
    FOREIGN KEY(work_id) REFERENCES work(id)
);

CREATE TABLE pianist (
    id INTEGER NOT NULL PRIMARY KEY,
    first_name VARCHAR(255) NOT NULL,
    sec_name VARCHAR(255) NOT NULL
);

CREATE TABLE Konzert (
    id INTEGER NOT NULL PRIMARY KEY,
    Datum DATE,
    Uhrzeit VARCHAR(5),
    Saal VARCHAR(255)
);

CREATE TABLE beherrscht(
    work_id INTEGER NOT NULL,
    Satz_id INTEGER NOT NULL,
    Spieler_in_id INTEGER NOT NULL,
    Vorlauf INTEGER DEFAULT 7,
    PRIMARY KEY(work_id, Satz_id, Spieler_in_id),
    FOREIGN KEY(work_id, Satz_id) REFERENCES movement(work_id, mov_number),
    FOREIGN KEY(Spieler_in_id) REFERENCES pianist(id)
);

CREATE TABLE spielt_in(
    Konzert_id INTEGER NOT NULL,
    Spieler_in_id INTEGER NOT NULL,
    work_id INTEGER NOT NULL,
    Satz_id INTEGER NOT NULL,
    PRIMARY KEY(Konzert_id, Spieler_in_id, work_id, Satz_id),
    FOREIGN KEY(Konzert_id) REFERENCES Konzert(id),
    FOREIGN KEY(work_id, Satz_id) REFERENCES movement(work_id, mov_number),
    FOREIGN KEY(Spieler_in_id) REFERENCES pianist(id)
);
