简体   繁体   English

产品的数据库模式设计取决于变体和价格

[英]Database schema design for products depending on the variants and prices

We are in the process of designing a database for an e-commerce application and website.我们正在为电子商务应用程序和网站设计数据库。 We reached an obstacle that has set us back in searching and experimenting but none of the solutions worked because there will be redundancy in the data.我们遇到了一个障碍,使我们在搜索和试验中倒退了,但没有一个解决方案奏效,因为数据中会有冗余。

We have made some designs that we rejected.我们做了一些我们拒绝的设计。 I will not show all the designs but I will show some of them here and one of the last designs that we were about to implement.我不会展示所有的设计,但我会在这里展示其中的一些,以及我们即将实施的最后一个设计。

  1. This is the first design we rejected because it was over-normalized and it was over complicated.这是我们拒绝的第一个设计,因为它过于规范化并且过于复杂。

ERD

  1. The second design was the same thing.第二个设计是一样的。

ERD

  1. This is the last design we tried but it was over simplified and the redundancy will be a lot.这是我们尝试的最后一个设计,但它过于简化并且冗余会很多。

ERD

After that we tried searching and looking for a design that will work and suit our previous design to the other tables.之后,我们尝试搜索并寻找一种设计,该设计将适用于我们之前的设计并适合其他桌子。 We found one in here we did some modification to the design but also it didn't suit us because the redundancy that will happen with the color_id and size_id and it is not a foreign key.我们在这里找到了一个,我们对设计进行了一些修改,但它也不适合我们,因为 color_id 和 size_id 会发生冗余,而且它不是外键。

ERD

How we want to design the tables that accept these cases:我们希望如何设计接受这些情况的表:

  1. Case number one: I have a products that doesn't have any colors or sizes.案例一:我的产品没有任何 colors 或尺寸。
  2. Case number two: I have a product that have different colors and different sizes also the size doesn't depend on the color or vice-versa and they all have the same price.案例二:我有一个产品有不同的 colors 和不同的尺寸,尺寸也不取决于颜色,反之亦然,它们的价格都一样。
  3. Case number three: I have a product where it has different colors and sizes but the sizes depends on the color for example (Color: Red) has (Sizes: S,M,L) and (Color: Black) has (Sizes: M,L) and they have different prices.案例三:我有一个产品,它有不同的 colors 和尺寸,但尺寸取决于颜色,例如(颜色:红色)有(尺寸:S、M、L)和(颜色:黑色)有(尺寸:M ,L) 并且它们有不同的价格。
  4. Case number four: I have a product Where it has different sizes and the price change depends on the sizes for example product table (Size: S) and the (Price: 50$) but the (Size: L) has the (Price: 100$).案例四:我有一个产品,它有不同的尺寸,价格变化取决于尺寸,例如产品表(尺寸:S)和(价格:50 美元),但(尺寸:L)有(价格: 100 美元)。

Start with entities and relationships.从实体和关系开始。 Each entity will get its own table when you're done.完成后,每个实体都会获得自己的表格。 Be hardnosed about understanding your real-world entities and your data-world representation of them.对了解您的真实世界实体及其在数据世界中的表示方式保持谨慎。

  • Your product is definitely an entity.你的product绝对是一个实体。
  • A product variant is also an entity.产品variant也是一个实体。
  • A product has one or more variant s.一个product有一个或多个variant That's a one-to-many relationship.那是一对多的关系。 (If the product is something without sizes or colors, the variant is generic. (如果产品没有尺寸或 colors,则该variant是通用的。
  • Each variant has stock-keeping attributes like sku_number and number_in_stock.每个variant都有库存属性,如 sku_number 和 number_in_stock。

You can't use the product to keep stock.您不能使用该product来保持库存。 Consider this: your product is a T-shirt.考虑一下:您的产品是一件 T 恤。 But you don't run out of T-shirts , you run out of small red T-shirts and you have to order more of those from your supplier.但是你不会用完T 恤,你会用完红色的小 T 恤,你必须从供应商那里订购更多。

Two entities so far:到目前为止有两个实体:

product :1 to N: variant . product :1 到 N: variant

Let's consider the variant to have a collection of product attributes.让我们考虑变体具有产品属性的集合。 Let's make attribute an entity.让我们将attribute设为实体。 Each attribute has a type: 'size', 'color', 'accent_color' for example.每个属性都有一个类型:例如'size'、'color'、'accent_color'。 So your attribute table contains these columns.所以你的attribute表包含这些列。

attribute_id PK
attribute_type VARCHAR(63)  'size', 'color', ...
attribute_value VARCHAR(63)  '30', 'red', ...

You have a many-to-many join table variant_attribute .您有一个多对多连接表variant_attribute The presence of a row in this table means the variant has the attribute.此表中存在一行意味着该变体具有该属性。

variant_id    PK 
attribute_id  PK

So, we're up to three entities.所以,我们最多三个实体。

product :1 to N: variant :0-N to 0-N: attribute . product :1 到 N: variant :0-N 到 0-N: attribute

That's done with four tables, product , variant , variant_attribute , attribute这是用四个表完成的, productvariantvariant_attributeattribute

(If you want to treat color and size completely separately, you can do that too. But the scheme I propose is field-extensible: if somebody tells you to sell 'cold', 'warm', and 'hot' variants of something next year you won't have to reinvent the wheel.) (如果你想完全分开处理颜色和尺寸,你也可以这样做。但我提出的方案是可扩展的:如果有人告诉你接下来要卖“冷”、“暖”和“热”的变体一年你就不必重新发明轮子了。)

These are the important entities and relationships: they describe the stuff your customers order, and the stuff in your warehouse you have to sell.这些是重要的实体和关系:它们描述了您的客户订购的东西,以及您必须出售的仓库中的东西。 They need to be simple enough to troubleshoot in production at midnight, and expressive enough to represent your real-world entities.它们需要足够简单,以便在午夜在生产中进行故障排除,并且足够表达以代表您的真实世界实体。 Get that part of the schema right.正确获取架构的那部分。

Only then add on the categories and sections.然后才添加类别和部分。 I cannot tell from your last diagram what you want there.我无法从你的最后一张图中看出你想要什么。 You probably should revisit your entities for that part of your app.您可能应该为您的应用程序的这一部分重新访问您的实体。

If you want to everything for sale that's red you can do this:如果你想出售所有红色的东西,你可以这样做:

SELECT p.product_id, 
       p.product_name_ar,
       s.attribute_value size,
       c.attribute_value color,
       v.sku_number,
       v.price
  FROM product p  -- one to many with ...
  JOIN variant v ON p.product_id = v.product_id   -- many to many with ...
  JOIN variant_attribute cva ON v.variant_id = cva.variant_id
  JOIN attribute c   ON cva.attribute_id = c.attribute_id  -- ... color 
                    AND c.attribute_type = 'color'
  JOIN variant_attribute sva ON v.variant_id = sva.variant_id -- ... size
  JOIN attribute s   ON sva.attribute_id = s.attribute_id
                    AND s.attribute_type = 'size'
 WHERE c.attribute_value = 'red';  -- we want the red ones.

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

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