mysql中如何存储商品的属性?

How to store the properties of the goods in mysql?

现在正在规划数据库产品的架构。存在货物存储选项/参数的问题。 例如,如果是商品的尺寸——创建宽度/高度/深度三个字段就足够了,但是如果有几十个参数呢? 假设参数列表可以更改,我看到了这个结构: 1 - 宽度 2 - 高度 ... 11 - 颜色

但我不能像商品一样捆绑它 - 可以将字段设为 json 类型

{1: "20", 2 "30", ..., 11: "white"} 然后在货品上做样品处理,但是不知道怎么过滤参数

谁能指教一下正确的道路?

基本上有两种方法 - 您可以在 table 中创建一系列 "udf"(用户定义的字段)列,例如 udf1、udf2 等 - 然后在您的其他地方应用你说 "udf1 == color, udf2 == weight".

另一种方法是创建一个 productmeta table,其结构如下:

id BIGINT AUTO_INCREMENT PRIMARY KEY
product_id INT
property VARCHAR(16)
propvalue VARCHAR(64)

那么对于产品 ID 137,您可能有几条记录:

|------------------------------------------|
| ID   | product_id | property | propvalue |
|------------------------------------------|
| 3137 | 137        | size     | large     |
| 3138 | 137        | color    | green     |
| 3139 | 137        | height   | 11.3      |
|------------------------------------------|

缺点是这些 table 可以变得非常大,非常快。

这个东西可以用三种方法实现

  1. products table中的所有separate columns相加。
  2. 通过json-encoding[=在products table末尾添加longtext类型的extras列来存储所有extra/dynamic参数29=] 或 序列化 .
  3. 最后一个是使用单独的元 table 来存储键值对以及 table 中的 product_id。

    CREATE TABLE product_meta()
        id BIGINT AUTO_INCREMENT PRIMARY KEY,  // Primary key of this table
        product_id INT, // For joining purpose and reference
        meta_key VARCHAR(16),
        meta_value VARCHAR(64)
    );
    

那么对于产品 ID 137,您可能有几条记录:

|------------------------------------------|
| id   | product_id | meta_key | meta_value|
|------------------------------------------|
| 3137 | 137        | size     | large     |
| 3138 | 137        | color    | green     |
| 3139 | 137        | height   | 11.3      |
|------------------------------------------|