数据库设计策略:电子商务系统上的产品表(EAV、类表继承或具体表继承)

发布于 2024-09-29 10:35:17 字数 1431 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

属性 2024-10-06 10:35:17

数据库中至少有三种表示类继承

  • 单表继承的方法:整个继承树存储在一个表中,并且有一个专用列描述行的类型。这意味着将会有许多未使用的列,因此只有当子类共享大部分属性时,这种方法才有意义。
  • 类表继承:树中的每个类都保存在单独的数据库表中,该数据库表仅存储特定的属性到班级。这意味着为了获取对象,您必须跨代表继承树中祖先的表进行连接。
  • 具体表继承:每个具体类都有一个单独的数据库表,但一个具体类所需的所有属性存储给定的类,包括继承的类。这意味着您不必加入,但另一方面您不能升级(例如,当您查询产品时,您不会看到任何鞋子)。

这些方法各有利弊,您必须做出权衡。

另一方面,使用继承来表示产品类型意味着每次引入新的产品类型时都必须修改代码和数据库架构。如果类型将发生很大变化,并且与每种类型相关的逻辑不多,那么最好使用单个产品属性表(productId、key、value)。这不是一个漂亮的数据库设计,但在这种情况下它会更加实用。

There are at least three approaches to representing class inheritance in a database

  • Single Table Inheritance: All attributes in the whole inheritance tree are stored in a single table and there is a dedicated column which describes the type of the row. This means that there will be many unused columns, so this approach makes sense only when subclasses share most of the attributes.
  • Class Table Inheritance: Every class in the tree is persisted in a separate database table which stores only attributes specific to the class. This means that in order to fetch an object your have to join across tables representing ancestors in the inheritance tree.
  • Concrete Table Inheritance: There is a separate database table for each concrete class, but all attributes needed by a given class are stored, including the inherited ones. This means you don't have to join, but on the other hand you cannot upcast (eg. when you query for products you won't see any shoes).

Each of these approaches has pros and cons, it's a trade-off that you have to make.

On the other hand using inheritance to represent the product type means that you'll have to modify both the code and the database schema every time you introduce a new product type. If the types are going to change a lot and there is not much logic associated with each type, then it might be better to use a single (producId, key, value) table of product properties. It's not a beautiful database design, but it's going to be much more practical in such case.

樱花落人离去 2024-10-06 10:35:17

你的对象设计很好。

然而,我建议使用单表继承(正如 Adam 提到的)并避开 EAV。如果您的 RDBMS 支持 XML 类型并且您担心所有可为 NULL 的字段的存储,请将所有可选数据放入单个 XML 列中。然后,架构验证将确保您不会尝试用一双鞋等来存储处理器类型。

然后,为每个产品类型添加额外的字段就像更新该特定产品的架构一样简单。

Your object design is good.

However, I suggest using single table inheritance (as mentioned by Adam) and steer clear away from EAV. If your RDBMS supports XML types and you're worried about storage of all the NULLable fields, put all the optional data in a single XML column. Schema validation will then ensure that you don't try to store a processor type with a pair of shoes, etc.

Adding extra fields per product type is then as simple as updating the schema for that particular product.

倾`听者〃 2024-10-06 10:35:17

您的设计是当今的正确标准。这称为标准化,将对象分解为更小的对象表。这就是关系数据库的全部目的。我会留在你所走的路上

Your design is the correct standard for today. It's called Normalization, breaking up objects into smaller object tables. That's the whole purpose of a relational database. I would stay on the path you are on

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文