是否使用一对一 MySQL 关系?

Use one-to-one MySQL Relation or not?

我有 user table,我需要用 authorpartner数据。我应该将其他列放在相同的 table 中还是再创建 2 个 tables(user_authoruser_partner) 与 一对一 关系 ?

表和列已简化,例如。

用户

id
name
email
password
avatar
is_active

user_author

user_id => user.id (PK, UNQ)
description
amount_per_text
max_text
is_active

user_partner

user_id => user.id (PK, UNQ)
name
description
image_logo
is_active

用户不能既是作者又是合作伙伴。然后我可以为普通用户提供 class,例如 User.phpUserPartner.phpUserAuthor.php 扩展 User.php class.

这有意义吗,还是我应该将这些列放在 user table 中?

两种方法都有效且经常使用。

出于数据一致性的原因,关系模式的规范化理论不允许将所有数据放在一个 table 中。这将有利于多 table 方法。

在单一 table 方法中,您最终会在 table 的每条记录中得到大量空(null)字段。

但是,当使用多个 table 时,您可能必须执行一个或两个联接才能查明记录是合作伙伴还是作者。因此,从性能的角度来看,单table解决方案是更可取的。

在这两种情况下,您都可以添加一个额外的字段,例如"recordType",方便用户快速区分不同类型的记录。这将提供最佳的运行时性能。