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 concert (
    id INTEGER NOT NULL PRIMARY KEY,
    conc_date DATE,
    conc_time VARCHAR(5),
    conc_place VARCHAR(255)
);

CREATE TABLE is_able_to_play(
    work_id INTEGER NOT NULL,
    mov_id INTEGER NOT NULL,
    pianist_id INTEGER NOT NULL,
    days_to_practice INTEGER DEFAULT 7,
    PRIMARY KEY(work_id, mov_id, pianist_id),
    FOREIGN KEY(work_id, mov_id) REFERENCES movement(work_id, mov_number),
    FOREIGN KEY(pianist_id) REFERENCES pianist(id)
);

CREATE TABLE plays_in(
    concert_id INTEGER NOT NULL,
    pianist_id INTEGER NOT NULL,
    work_id INTEGER NOT NULL,
    mov_id INTEGER NOT NULL,
    PRIMARY KEY(concert_id, pianist_id, work_id, mov_id),
    FOREIGN KEY(concert_id) REFERENCES concert(id),
    FOREIGN KEY(work_id, mov_id) REFERENCES movement(work_id, mov_number),
    FOREIGN KEY(pianist_id) REFERENCES pianist(id)
);
