CREATE TABLE IF NOT EXISTS "sequences"
(
    "key"         VARCHAR(64) NOT NULL,
    "type"        VARCHAR(64) NOT NULL,
    "seq"         INT8        NOT NULL,
    "update_time" TIMESTAMP,
    PRIMARY KEY ("key", "type")
);
COMMENT ON TABLE "sequences" IS '当前序号表';
COMMENT ON COLUMN "sequences"."key" IS '序号英文名称';
COMMENT ON COLUMN "sequences"."type" IS '序号类型';
COMMENT ON COLUMN "sequences"."seq" IS '已使用到的序号';
COMMENT ON COLUMN "sequences"."update_time" IS '最后使用时间';


/*
触发器函数，用户更新update_time字段
*/
CREATE OR REPLACE FUNCTION SEQUENCES_UPDATE_TIME() RETURNS TRIGGER AS $$
BEGIN
    NEW."update_time" := current_timestamp;
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

/*
@触发器设置，功能：自动记录更新时间
@触发器名为 SEQUENCES_UPDATE_TIME
@触发器执行范围为每行生效
*/
DROP TRIGGER IF EXISTS SEQUENCES_UPDATE_TIME ON "sequences" CASCADE;
CREATE TRIGGER SEQUENCES_UPDATE_TIME BEFORE UPDATE ON "sequences" FOR EACH ROW EXECUTE FUNCTION SEQUENCES_UPDATE_TIME();