对产品变体进行建模
您正在尝试对产品变体进行建模,并对使用 EAV(实体属性值)有一些疑虑。您可以考虑以下替代设计:
标准化设计
以下设计标准化产品变体的数据结构:
+---------------+ +-----------------+ | PRODUCTS |-----< PRODUCT_VARIANTS | +---------------+ +-----------------+ | #product_id | | #product_id | | product_name | | #variant_id | +---------------+ | sku_id | | +-----------------+ | | +--------^--------+ +--------^--------+ | PRODUCT_OPTIONS |-----< VARIANT_VALUES | +-----------------+ +-----------------+ | #product_id | | #product_id | | #option_id | | #variant_id | +--------v--------+ | #option_id | | | value_id | +-----------------+ +--------v--------+ | OPTIONS | | +-----------------+ | | #option_id | | | option_name | | +-----------------+ | | | +-------^-------+ | | OPTION_VALUES |---------------+ +---------------+ | #option_id | | #value_id | | value_name | +---------------+
主要的、独特的和外国的键:
PRODUCTS - PK: product_id - UK: product_name OPTIONS - PK: option_id - UK: option_name OPTION_VALUES - PK: option_id, value_id - UK: option_id, value_name - FK: option_id REFERENCES OPTIONS (option_id) PRODUCT_OPTIONS - PK: product_id, option_id - FK: product_id REFERENCES PRODUCTS (product_id) - FK: option_id REFERENCES OPTIONS (option_id) PRODUCT_VARIANTS - PK: product_id, variant_id - UK: sku_id - FK: product_id REFERENCES PRODUCTS (product_id) VARIANT_VALUES - PK: product_id, variant_id, option_id - FK: product_id, variant_id REFERENCES PRODUCT_VARIANTS (product_id, variant_id) - FK: product_id, option_id REFERENCES PRODUCT_OPTIONS (product_id, option_id) - FK: option_id, value_id REFERENCES OPTION_VALUES (option_id, value_Id)
工作原理
此设计允许您独立定义选项及其值,从而可以灵活地将来添加新选项或值。
示例数据
以下示例说明了如何根据问题中的电子表格在这些表格中输入数据:
PRODUCTS ======== product_id product_name ---------- ------------ 1 Widget 1 2 Widget 2 3 Widget 3 OPTIONS ======= option_id option_name --------- ----------- 1 Size SL 2 Color 3 Size SM 4 Class 5 Size ML OPTION_VALUES ============= option_id value_id value_name --------- -------- ------------ 1 1 Small (Size SL) 1 2 Large (Size SL) 2 1 White (Color) 2 2 Black (Color) 3 1 Small (Size SM) 3 2 Medium (Size SM) 4 1 Amateur (Class) 4 2 Professional (Class) 5 1 Medium (Size ML) 5 2 Large (Size ML) PRODUCT_OPTIONS =============== product_id option_id ---------- --------- 1 1 (Widget 1; Size SL) 1 2 (Widget 1; Color) 2 3 (Widget 2; Size SM) 3 4 (Widget 3; Class) 3 5 (Widget 4; Size ML) PRODUCT_VARIANTS ================ product_id variant_id sku_id ---------- ---------- ------ 1 1 W1SSCW (Widget 1) 1 2 W1SSCB (Widget 1) 1 3 W1SLCW (Widget 1) 1 4 W1SLCB (Widget 1) 2 1 W2SS (Widget 2) 2 2 W2SM (Widget 2) 3 1 W3CASM (Widget 3) 3 2 W3CASL (Widget 3) 3 3 W3CPSM (Widget 3) 3 4 W3CPSL (Widget 3) VARIANT_VALUES ============== product_id variant_id option_id value_id ---------- ---------- --------- -------- 1 1 1 1 (W1SSCW; Size SL; Small) 1 1 2 1 (W1SSCW; Color; White) 1 2 1 1 (W1SSCB; Size SL; Small) 1 2 2 2 (W1SSCB; Color; Black) 1 3 1 2 (W1SLCW; Size SL; Large) 1 3 2 1 (W1SLCW; Color; White) 1 4 1 2 (W1SLCB; Size SL; Large) 1 4 2 2 (W1SLCB; Color; Black) 2 1 3 1 (W2SS; Size SM; Small) 2 2 3 2 (W2SM; Size SM; Medium) 3 1 4 1 (W3CASM; Class; Amateur) 3 1 5 1 (W3CASM; Size ML; Medium) 3 2 4 1 (W3CASL; Class; Amateur) 3 2 5 2 (W3CASL; Size ML; Large) 3 3 4 2 (W3CPSM; Class; Professional) 3 3 5 1 (W3CPSM; Size ML; Medium) 3 4 4 2 (W3CPSL; Class; Professional) 3 4 5 2 (W3CPSL; Size ML; Large)
优点
缺点
结论
这种标准化设计是 EAV 对产品变体建模的可行替代方案。它提供灵活性、可扩展性和数据完整性,同时也相对易于查询。但是,EAV 和标准化之间的具体选择应根据您的应用程序的具体要求和权衡进行。
以上是规范化数据库设计如何有效地对产品变体进行建模,作为实体-属性-值 (EAV) 模型的替代方案?的详细内容。更多信息请关注PHP中文网其他相关文章!