[英]Abstract databases design.
So I'm creating a web app with different user types that can come from different countries. 因此,我正在创建一个具有来自不同国家/地区的不同用户类型的Web应用程序。 Examples of the user types would be
company
, staff
etc. Where a company would have a company_name
field and staff would not. 用户类型的示例是
company
, staff
等。公司将具有company_name
字段,而staff则没有。
In the users
database I'm wondering if it's a good idea to implement a one table per column approach ie for each user attribute there would be a table with a foreign key which would be the user_id
and a value
for the attribute value. 在
users
数据库中,我不知道它是否实现,即每个用户的每列一个表的做法属性会有一个外键这将是一个表是一个好主意user_id
和value
的属性值。
eg. 例如。
users.company_name = id(PK), | user_id(FK) | 'company_name' 1 | 1 | company 1
users.email = id(PK), | user_id(FK) | 'email' 1 | 1 | user@email.com
The same could be applied to an address
database where different countries' addresses have different values. 对于不同国家的地址具有不同值的
address
数据库,也可以使用相同的方法。
Opinions? 意见?
The term you're looking for is " The Party Model " 您要查找的术语是“ 派对模型 ”
You want to use Table Inheritance†, also known as subtype/supertype relationships to model stuff like this. 您想要使用表继承†(也称为子类型/超类型关系)来对此类事物进行建模。
An Individual is a concretion of an abstract Legal Party. 个人是抽象法人的一种构想。 An Organization (eg a Company) is also a concretion of an abstract Legal Party.
组织(例如公司)也是抽象法人的具体构想。
"Staff" is not a subtype of Legal Party. “工作人员”不是合法政党的子类型。 It's a relationship between a Company and an Individual.
这是公司与个人之间的关系。 A company hasMany staffRelationships with individuals.
一个公司有很多员工与个人的关系。
I recommend Single Table Inheritance , as it's fast and simple. 我建议使用Single Table Inheritance ,因为它既快速又简单。 If you really don't like nulls, then go for Class Table Inheritance .
如果您真的不喜欢null,请使用Class Table Inheritance 。
create table parties (
party_id int primary key,
type smallint not null references party_types(party_type_id), --elided,
individual_name text null,
company_name text null,
/* use check constraints for type vs individual/company values */
);
I'd go with PostgreSQL over MySQL (or MariaDB) if you're going to use Single Table Inheritance, as the latter do not support check constraints. 如果您要使用单表继承,那么我将通过MySQL(或MariaDB)使用PostgreSQL,因为后者不支持检查约束。
You can make user
belongTo a party
, or make party haveOne user
. 您可以使
user
属于party
,也可以使聚会具有一个user
。
† Which is different than PostgreSQL's Inheritance feature. †与PostgreSQL的继承功能不同。
I'd create a single users table with company_name
and email
columns. 我将使用
company_name
和email
列创建一个单个用户表。
For addresses table, I'd start with something simple like this: id, address_line_1, address_line_2, city, state, country, zip. 对于地址表,我将从类似以下的简单内容开始:id,address_line_1,address_line_2,城市,州,国家/地区,邮政编码。
With this strategy you'll have to do a lot of joining tables to get a meaningful query result. 使用这种策略,您将必须执行很多联接表才能获得有意义的查询结果。 As a result your performance will suffer and you have very ineffective use of storage.
结果,您的性能将受到影响,并且存储的使用效率非常低。
You should at least combine columns that will typically be combined for a logical entity in your application. 您至少应组合通常在应用程序中为逻辑实体组合的列。 So if a 'company' differs from 'staff' in that it has extra columns, you would create a table 'users.company_properties'.
因此,如果“公司”与“工作人员”的不同之处在于它具有额外的列,则可以创建表“ users.company_properties”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.