【发布时间】:2016-11-10 12:59:28
【问题描述】:
我的数据库/表每个端点/范围都有多个表,
例如用户:用户、用户信息、用户角色 ...
我想知道我应该像下面那样分开表格吗?
这是我第一次构建一个不像以前只是做一些一次性工作的小型网站的产品。这可能会在未来的版本中添加更多功能。
我不确定这是不是过度设计?这样的单独表格将来有什么好处吗??
我知道的缺点是使用更多的表连接,并且更难以维护构建查询。
任何建议,分享经验将非常感激。
现在我只能想象我可能想知道每一列的最后修改时间?
那么如果我想知道每一列的最后修改时间,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")
);
【问题讨论】:
标签: database postgresql database-design database-schema