Table 网上商店的结构

Table structure for web shop

请考虑这种情况:

A web shop sells IT products. If we look at computers and printers, than there are some filters that both products need: ID, name, price, e.d. Some filters are specific to only one product: OS, cartridge type.

存储这些产品的最佳方式是什么?

  1. 我在想这个。

Create a product table with all possible filters. If a product doesn't have a certain filter, just keep it empty. This method is simple to implement but can cause a lot of waisted space if there are many different specific filters.

  1. 这是 WordPress 的做法。

Create a product table with only the general filters. Create a separate table which holds the meta data (specific filters) with these columns: filter_id, product_id, filter_name, filter_value. Every specific filter gets it's own row. This method doesn't waste any space, but if every product has around 10 different specific filters, than the table would become very large and might cause more performance issues than the first method.

谁能告诉我哪种方法是首选方法,或者提供替代方法。

谢谢

您正在寻找的是我在此处描述的 m:n 关系(即一个过滤器可能适用于多个产品,并且一个产品可能有多个过滤器):

你对可能的值感兴趣。我会推荐 4 个表:

  • Table 1 有 Products (ProductId, ProductName, Price etc.) 列出所有 可能的产品
  • Table 2 个有过滤器(FilterId、FilterName 等) 列出所有可能的过滤器(但不是它们可能的值)
  • Table 3 具有关系 (ProductId, FilterId),表明该过滤器是 适用于该产品。 FK 到产品和过滤器
  • Table 4 具有过滤器值:(FilterId, FilterValue) FK to Filter listing 给定过滤器的每个可能值。