create table users
(
    id          INTEGER      not null
        primary key,
    name        VARCHAR(50)  not null
        unique,
    pwd         VARCHAR(255) not null,
    description VARCHAR(100)
);

create table categories
(
    id        INTEGER     not null
        primary key,
    name      VARCHAR(50) not null,
    parent_id INTEGER
        references categories,
    user_id   INTEGER     not null
        references users
);

create table const_expenses
(
    id          INTEGER        not null
        primary key,
    description VARCHAR(50),
    expense     NUMERIC(10, 2) not null,
    months      SMALLINT       not null,
    start       DATE           not null,
    end         DATE           not null,
    prev_id     INTEGER
        references const_expenses,
    user_id     INTEGER        not null
        references users,
    category_id INTEGER        not null
        references categories
);

create index idx_start_end
    on const_expenses (user_id, start, end);

create index ix_constexpense_end
    on const_expenses (end);

create index ix_constexpense_start
    on const_expenses (start);

create table single_expenses
(
    id          INTEGER        not null
        primary key,
    description VARCHAR(50),
    expense     NUMERIC(10, 2) not null,
    year        INTEGER        not null,
    month       SMALLINT       not null,
    day         SMALLINT       not null,
    user_id     INTEGER        not null
        references users,
    category_id INTEGER        not null
        references categories
);

create index idx_single_date
    on single_expenses (user_id, year, month);
