首页 > 解决方案 > 我真的需要为我的三种用户提供单独的表吗?

问题描述

如果我有三种类型的用户。假设卖方、消费者和销售人员。我是否应该为其中的详细信息(如姓名、电子邮件密码和所有其他凭据等)制作单独的表格,并为每个表格使用 role_type 表格或单独的表格。考虑到 DBMS 的所有工程原则(如规范化等),这是大型项目的最佳方法。

还告诉我,如果我在表中有很多连接来执行某些操作,它会影响应用程序的性能吗?

标签: postgresqldatabase-design

解决方案


如果区分这些人的唯一因素是角色但所有细节都相同,那么我肯定会选择一张桌子。

然而,问题是,一个人可以担任多个角色吗?如果情况并非如此,则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)
);

推荐阅读