[英]Shopping cart database design: adding functionality for quantities by size
我目前有一个简单的基于php的购物车,但我需要添加一个新功能,以便每个尺寸都有唯一的数量。 例如,可能有1个大库存,但3个小库存。
每个产品都有唯一的产品代码,但是具有多种尺寸和颜色。 当前,表中有一个数量字段。 订购产品后,我还需要能够减少数量。
为了提高灵活性,我目前将选项作为json对象的数据库条目中的单个字段引用,如下所示:
{"colors":"Red,Blue","sizes":"S,M,L"}
这使我可以灵活使用多个不同名称的选项,而不必在数据库中创建选项表。
我可以简单地将json对象扩展为如下所示:
{"colors":"Red,Blue","sizes":{"S":"1","M":"3","L":"0"}}
我担心这会导致我无法预料的问题。 我还担心在销售后进行这种递减会变得更加复杂。
目前,我只需要更新产品代码匹配的位置并减少数量即可。 通过上述更改,我必须检索产品信息,解码选项,找到大小和减量,然后更新数据库。 这感觉太复杂了。
有没有更好的方法来按尺寸处理产品数量?
我永远不会在嵌入了大小/数量的单个产品行中保存字符串或json块。
我会去一个产品表。
然后是一个junction表,该表使用带有大小列和数量列的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
);
当然,第二个表不是联结表,但是您知道了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.