我的数据库/表的每个端点/作用域都有多个表,
例如user:user,user_information,user_role ...
我想知道是否应该像下面那样分开表?
这是我第一次构建一个不像以前那样制作产品的小型网站。这可能会在将来的版本中添加更多功能。
我不确定这种过度设计吗?这样单独的表在将来有什么好处吗?
我知道的利弊是,使用更多的表联接,对我来说,维护查询更困难。
任何建议,分享经验将不胜感激。
现在,我只能想象我可能想知道每个列的上次修改时间吗?
因此,如果我想知道每一列的上次修改时间,PostgreSQL中是否有任何原始的构建方法?或者我必须为每个列添加email_last_modified_date
,username_last_modified_date
...
端点/范围用户
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
);
去步步高?
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")
);
为每个实体(其中一个实体对企业来说很重要,需要唯一标识)和每个多对多关系建立一个表格。因此,用户,角色,用户角色各获得一张表。区分用户信息和用户信息没有任何价值,您只需引入另一个联接即可。
如果您想使用一种最佳方法来处理从用户到角色的联接而不加载用户信息:如果在表上放置索引,则许多查询可能只能使用索引,而不必读取表,除非存在需要一些特定的数据。无需制作单独的用户信息表。
您对合并后的示例所做的工作对我来说还不清楚,这似乎是非规范化的极端情况。
如果要跟踪更改,则可以查找事件源,这将提供一种保留更改的完整历史记录的方法。有关简介,请参见http://martinfowler.com/eaaDev/EventSourcing.html。缺少这些,您可以保留关注跟踪其更改的特定实体的历史记录。。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句