postgresql - 我真的需要为我的三种用户提供单独的表吗?
问题描述
如果我有三种类型的用户。假设卖方、消费者和销售人员。我是否应该为其中的详细信息(如姓名、电子邮件密码和所有其他凭据等)制作单独的表格,并为每个表格使用 role_type 表格或单独的表格。考虑到 DBMS 的所有工程原则(如规范化等),这是大型项目的最佳方法。
还告诉我,如果我在表中有很多连接来执行某些操作,它会影响应用程序的性能吗?
解决方案
如果区分这些人的唯一因素是角色但所有细节都相同,那么我肯定会选择一张桌子。
然而,问题是,一个人可以担任多个角色吗?如果情况并非如此,则role_type
在 person 表中添加一列。根据这些角色的固定程度,可能会使用查找表和外键,例如:
create table role_type
(
id integer primary key,
name varchar(20) not null unique
);
create table person
(
id integer primary key,
.... other attributes ...,
role_id integer not null references role_type
);
但是,根据我的经验,每个人只能扮演一个角色的限制通常并不成立,因此您需要多对多关系
create table role_type
(
id integer primary key,
name varchar(20) not null unique
);
create table person
(
id integer primary key,
.... other attributes ...,
);
create table person_role
(
person_id integer not null references person,
role_id integer not null references role_type,
primary key (person_id, role_id)
);
推荐阅读
- swift - 为什么具有 autosaveExpandedItems true 的基于视图的 NSOutlineView 在 reloadData 时忽略扩展?
- c - 我想在 MBED 中使用 RX_interrupt 获取字符串
- mongodb - 如何在更新时间 MongoDb 中将字段的总值添加为预聚合字段?
- angular - 面临角度站点路线导航的问题
- c# - 未在 Azure Blob 存储 v12 中计算 ContentHash
- javascript - 使用本机反应创建页面构建器
- javascript - 单击信息窗口内的按钮时更改谷歌地图标记图标
- java - 在 Unique Constrains JPA/Hibernate 期间添加自定义错误消息的更好方法
- java - 如何在 JavaFX 可编辑 TreeTableView 中提供货币编辑(LongProperty)?
- flutter - 断言失败:第 556 行 pos 15:'scrollOffsetCorrection != 0.0':不正确