我真的需要为我的三类用户提供个人表吗?

问题描述 投票:0回答:2

如果我有三种类型的用户。让我们说卖家,消费者和销售人员。我应该为名称,电子邮件密码和所有其他凭证等详细信息制作单独的表格,其中包含role_type表格或每个表格的单独表格。考虑到DBMS的所有工程原理,如规范化等,这是大型项目的最佳方法。

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

postgresql database-design
2个回答
2
投票

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

但问题是,一个人可以有多个角色吗?如果情况绝非如此,那么将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)
);

1
投票

听起来这是尝试在关系数据库中建模继承的情况。复杂的话题,讨论了herehere

听起来你的“卖家,消费者,销售人员”需要很多不同的属性和关系。卖方通常属于某个部门,具有目标,与销售相关联。消费者有购买历史记录,可能是信用额度等。

如果是这种情况,我建议“类表继承”可能是正确的解决方案。

这可能看起来像这样。

create table user_account
(id int not null, 
username varchar not null, 
password varchar not null
....);

create table buyer
(id int not null, 
user_account_id int not null(fk), 
credit_limit float not null, 
....);

create table seller
(id int not null, 
user_account_id int not null(fk),
sales_target float,
....);

要回答您的其他问题 - 关系数据库已针对连接表进行了优化。数十年的研究和开发已经进入这个领域,一个设计良好的数据库(在你加入的列上有索引)将显示由于连接而没有明显的性能影响。根据实际经验,具有数亿条记录和十条或更多条连接的查询在现代硬件上运行得非常快。

© www.soinside.com 2019 - 2024. All rights reserved.