CREATE SCHEMA IF NOT EXISTS tests;

CREATE TABLE tests.authors (
    author_id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL
);

CREATE TABLE tests.books (
    id SERIAL PRIMARY KEY,
    title VARCHAR(100) NOT NULL,
    author_id INT NOT NULL,
    FOREIGN KEY (author_id) REFERENCES tests.authors(author_id) ON DELETE CASCADE
);

CREATE TABLE tests.genres (
    genre_id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL
);

CREATE TABLE tests.book_genres (
    book_id INT NOT NULL,
    genre_id INT NOT NULL,
    PRIMARY KEY (book_id, genre_id),
    FOREIGN KEY (book_id) REFERENCES tests.books(id) ON DELETE CASCADE,
    FOREIGN KEY (genre_id) REFERENCES tests.genres(genre_id) ON DELETE CASCADE
);
