数据库设计。并获取 PostgreSQL 中每一列的最后修改时间

Database design. And get last modified time with each column in PostgreSQL

我的database/table每个endpoint/scope有多个table,
例如用户:用户,user_information,user_role ...
我想知道我是否应该像下面那样分开 table ?

这是我第一次做产品,不像以前做一些一次性的小网站。这可能会在未来的版本中添加更多功能。

我不确定这是不是过度设计?这样分开table对以后有什么好处吗??
我知道的缺点是使用更多 table 连接,而且我更难维护构建查询。

任何建议,分享经验将不胜感激。



现在我只能想象我可能想知道每一列的最后修改时间?

所以如果我想知道每个列的最后修改时间,PostgreSQL中有没有原始的构建方法?或者我必须为每一列添加 email_last_modified_dateusername_last_modified_date ...



endpoint/scope 用户

CREATE TABLE IF NOT EXISTS "user"(
    "id" SERIAL NOT NULL,
    "create_date" timestamp without time zone NOT NULL,
    "last_modified_date" timestamp without time zone,
    "last_modified_by_user_id" integer,
    "status" integer NOT NULL,
    PRIMARY KEY ("id")
  );

CREATE TABLE IF NOT EXISTS "user_information"(
    "id" SERIAL NOT NULL,
    "create_date" timestamp without time zone NOT NULL,
    "last_modified_date" timestamp without time zone,
    "last_modified_by_user_id" integer,
    "user_id" integer NOT NULL,
    "email" varchar(100) NOT NULL,
    "username" varchar(50),
    "password" varchar NOT NULL,
    "first_name" varchar(50),
    "last_name" varchar(50),
    "website" varchar,
    "description" varchar,
    "birth_date" timestamp without time zone,
    "country" varchar(50),
    "gender" integer,
    "file_type" integer,
    "file_name" varchar(50),
    "file_extension" varchar(50),
    "file_portrait" boolean,
    PRIMARY KEY ("id"),
    FOREIGN KEY ("user_id") REFERENCES "user" ("id") ON DELETE CASCADE ON UPDATE CASCADE
  );
CREATE TABLE IF NOT EXISTS "user_role"(
    "id" SERIAL NOT NULL,
    "create_date" timestamp without time zone NOT NULL,
    "last_modified_date" timestamp without time zone,
    "last_modified_by_user_id" integer,
    "user_id" integer NOT NULL,
    "role" integer NOT NULL,
    PRIMARY KEY ("id"),
    FOREIGN KEY ("user_id") REFERENCES "user" ("id") ON DELETE CASCADE ON UPDATE CASCADE
  );

合并 table ?

CREATE TABLE IF NOT EXISTS "user"(
    "id" SERIAL NOT NULL,
    "create_date" timestamp without time zone NOT NULL,
    "last_modified_date" timestamp without time zone,
    "last_modified_by_user_id" integer,
    "status" integer NOT NULL,

    "information_last_modified_date" timestamp without time zone,
    "information_last_modified_by_user_id" integer,
     .... user_information 

    "role_last_modified_date" timestamp without time zone,
    "role_last_modified_by_user_id" integer,
    ... user_role
    PRIMARY KEY ("id")
  );

为每个实体(其中一个实体对业务很重要,需要唯一标识)和每个多对多关系创建一个 table。所以用户、角色、用户角色各得到一个table。用户和用户信息之间的分离没有任何价值,你只是引入另一个连接。

如果您认为您想要一种最佳方式来处理从用户到角色的联接而不加载用户信息:如果您在 table 上放置索引,许多查询可能只能使用索引除非需要某些特定数据,否则不必阅读 table。无需单独制作用户信息table.

我不清楚你在用合并的例子做什么,这似乎是反规范化的极端情况。

如果您想跟踪更改,您可以查找事件溯源,它提供了一种保留完整更改历史记录的方法。有关介绍,请参阅 http://martinfowler.com/eaaDev/EventSourcing.html。除此之外,您可以保留您关心的特定实体的历史记录,以跟踪其更改。 .