简体   繁体   English

MySQL数据库具有产品,产品类别和不同属性

[英]MySQL database with products, products categories and different attributes

I'm having some difficult designing a database with a lot of different attributes per product. 我在设计一个每个产品有很多不同属性的数据库时遇到了一些困难。

Here's the situation: each product should belong to a category and each category has a different set of attributes . 情况如下:每个产品都应属于一个类别 ,每个类别都有一组不同的属性

Eg: 例如:

  • Product iGal 7SCategory SmartphonesAttributes attr_phone 产品 iGal 7S类别 智能手机属性 attr_phone
  • Product UberLaptopCategory Notebooks & LaptopsAttributes attr_laptop 产品 UberLaptop类别 笔记本和笔记本电脑属性 attr_laptop

From what I understood , it would be best to avoid EAV and proceed with class table inheritance. 根据我的理解 ,最好避免使用EAV并继续进行类表继承。

On this observation, I would have as starting point: 根据这一观察,我将以起点为出发点:

开始设计


If I had a single attributes set, I'd use an attribute_id foreign key in the products table and call it a day, but I'll potentially have 50+ attributes set (and therefore 50+ tables). 如果我有一个属性集,我会在products表中使用一个attribute_id外键并调用它一天,但我可能会设置50多个属性(因此有50多个表)。

  1. Is CTI still valid as approach? CTI仍然有效吗?
  2. How could I assign the correct attributes set to a product? 如何为产品分配正确的属性集?

Sorry for the stupid question, but in particular I fail to understand how I can assign a different attributes set when a different category is selected. 抱歉这个愚蠢的问题,但特别是我不明白如何在选择其他类别时分配不同的属性集。 Thanks for reading. 谢谢阅读。

Is CTI still valid as approach? CTI仍然有效吗?

I think that if the number of the categories are in the order of tenth, and not of hundredth, then yes. 我认为,如果类别的数量是十分之一,而不是百分之一,那么是的。

How could I assign the correct attributes set to a product? 如何为产品分配正确的属性集?

You could add to each category row the table name of corresponding table of attributes, and for each attribute table, the id of the row will be the id of the corresponding product (so that you can define it as a foreign key for the products table). 您可以向每个类别行添加相应的属性表的表名,并且对于每个属性表,该行的id将是相应产品的id(以便您可以将其定义为products表的外键) )。

In almost all situations it is "wrong" to have 55 tables with identical schema. 在几乎所有情况下,拥有55个具有相同模式的表是“错误的”。 Making it 1 table is better. 使它成为一张桌子更好。 But then it gets you into the nightmare called "Entity-Attribute-Value". 但随后它会让你进入一个名为“实体 - 属性 - 价值”的梦魇。

Pick a few "attributes" that you usually need to search on. 选择一些您通常需要搜索的“属性”。 Put the rest into a JSON string in a single column. 将其余部分放入单个列中的JSON字符串中。 More details 更多细节

Your schema is not quite EAV. 您的架构不是EAV。 It is an interesting variant; 这是一个有趣的变种; what does it stand for? 它代表什么? I don't think I have seen this as an alternative to EAV. 我认为我不认为这是EAV的替代品。 I don't yet have an opinion on whether this replaces the set of problems that EAV has with a different set of problems. 我还不知道这是否取代了EAV对一系列不同问题的问题。 Or maybe it is better. 或者它可能更好。

What client language will you be using? 您将使用哪种客户端语言? You will need to turn category.name into attr_laptop in order to SELECT ... from attr_laptop ... This implies dynamically creating the queries. 您需要将category.name转换为attr_laptop以便SELECT ... from attr_laptop ...这意味着动态创建查询。 (This is quite easy in most client languages. It is also possible, though a little clumsy, in Stored Routines.) (在大多数客户端语言中这很容易。虽然有点笨拙,但在存储例程中也是可能的。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM