CREATE TABLE users
(
    id            SERIAL PRIMARY KEY,
    name          TEXT      NOT NULL UNIQUE,
    password      TEXT      NOT NULL,
    creation_date TIMESTAMP NOT NULL
);

CREATE TABLE groups
(
    id          SERIAL PRIMARY KEY,
    name        TEXT                      NOT NULL UNIQUE,
    description TEXT,

    currency    TEXT                      NOT NULL,
    creator     INT REFERENCES users (id) NOT NULL,

    closed      BOOLEAN                   NOT NULL
);

CREATE TABLE group_members
(
    user_id  INT REFERENCES users (id)  NOT NULL,
    group_id INT REFERENCES groups (id) NOT NULL,
    settled  BOOLEAN                    NOT NULL,
    PRIMARY KEY (user_id, group_id) -- To forbid two equal entries
);

CREATE TABLE expenses
(
    id            SERIAL PRIMARY KEY,
    group_id      INT REFERENCES groups (id) ON DELETE CASCADE NOT NULL,
    creator_id    INT REFERENCES users (id)                    NOT NULL,

    description   TEXT                                         NOT NULL,
    creation_date TIMESTAMP                                    NOT NULL,

    -- Amount of money in the smallest unit, for example 'Cents' or 'Rappen'
    amount        INT                                          NOT NULL
);