CREATE TABLE IF NOT EXISTS owner
(
    owner_id BIGINT       NOT NULL AUTO_INCREMENT,
    name     VARCHAR(100) NOT NULL,
    domain     VARCHAR(100) NOT NULL,
    is_admin INT(1) NOT NULL DEFAULT (0),
    active   INT(1)       NOT NULL DEFAULT (1),
    PRIMARY KEY (owner_id)
);

CREATE TABLE IF NOT EXISTS token
(
    token_id BIGINT    NOT NULL AUTO_INCREMENT,
    user_id  BIGINT    NOT NULL,
    expires  TIMESTAMP NOT NULL,
    PRIMARY KEY (token_id),
    FOREIGN KEY (user_id) REFERENCES PREFIXsystem.user (user_id)
);

CREATE TABLE IF NOT EXISTS rechte_code
(
    code_id BIGINT       NOT NULL AUTO_INCREMENT,
    name    VARCHAR(100) NOT NULL,
    app     VARCHAR(20)  NOT NULL,
    last_date BIGINT NOT NULL,
    active  INT(1)       NOT NULL DEFAULT (1),
    PRIMARY KEY (code_id)
);
CREATE TABLE IF NOT EXISTS app
(
    app_id BIGINT       NOT NULL AUTO_INCREMENT,
    name   VARCHAR(100) NOT NULL,
    url    VARCHAR(100) NOT NULL,
    active INT(1)       NOT NULL DEFAULT (1),
    PRIMARY KEY (app_id)
);

CREATE TABLE IF NOT EXISTS key_app_owner
(
    key_id   BIGINT NOT NULL AUTO_INCREMENT,
    app_id   BIGINT NOT NULL,
    owner_id BIGINT NOT NULL,
    PRIMARY KEY (key_id),
    FOREIGN KEY (app_id) REFERENCES app (app_id),
    FOREIGN KEY (owner_id) REFERENCES owner (owner_id)
);