购物车数据库设计:按尺寸添加数量功能

Shopping cart database design: adding functionality for quantities by size

我目前有一个简单的基于 php 的购物车,但我需要添加一个新功能,以便每个尺寸都有唯一的数量。例如,库存中可能有 1 件大件,但有 3 件小件。

每件产品都有唯一的产品代码,但有多种尺寸和颜色。当前 table 中有一个数量字段。我还需要能够在订购产品后减少数量。

为了灵活性,我目前将选项作为数据库条目中的单个字段引用,该条目是一个 json 对象,看起来像这样:

{"colors":"Red,Blue","sizes":"S,M,L"}

这让我可以灵活地使用多个不同名称的选项,而无需在数据库中创建选项 table。

我可以简单地将 json 对象扩展为如下所示:

{"colors":"Red,Blue","sizes":{"S":"1","M":"3","L":"0"}}

我担心这会给我带来不可预见的问题。我还担心在销售后进行递减会变得更加复杂。

目前,我只需要更新产品代码匹配的地方并减少数量。通过上述更改,我必须检索产品信息、解码选项、查找大小和递减量,然后更新数据库。这感觉太复杂了。

是否有更好的方法来按尺寸处理产品数量?

我永远不会在嵌入 sizing/quantity 的单个产品行中保存字符串或 json 块。

我会选择产品 table。

然后 Junction table 使用带有尺寸列和数量列的 productId。

架构想法:

create table Products
(   prodId int auto_increment primary key,
    prodName varchar(100) not null
);

create table ProductQuantities
(   -- table to store quantities for Product/Sizing combos
    id int auto_increment primary key,
    prodId int not null,
    size varchar(10) not null,
    quantity int not null,
    constraint foreign key (prodId) references products(prodId),
    key(prodId),
    unique key (prodId,size) -- maintains sanity for no duplicates at the combo level, plus a general index
);

当然,第二个 table 不是路口 table,但你明白了