[英]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.
我不会展示所有的设计,但我会在这里展示其中的一些,以及我们即将实施的最后一个设计。
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 会发生冗余,而且它不是外键。
How we want to design the tables that accept these cases:我们希望如何设计接受这些情况的表:
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.
对了解您的真实世界实体及其在数据世界中的表示方式保持谨慎。
product
is definitely an entity.product
绝对是一个实体。variant
is also an entity.variant
也是一个实体。product
has one or more variant
s.product
有一个或多个variant
。 That's a one-to-many relationship.variant
is generic. variant
是通用的。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
这是用四个表完成的,
product
, variant
, variant_attribute
, attribute
(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.