如何为存储订单详细信息的关系 table 建模

How to model a relational table that store order details

我正在研究如何创建电子商务数据库。经过一些搜索,这是我发现的:

这里的问题是 OrderDetails 是对产品 table 的引用。如果我编辑了价格,sku等产品,它会影响之前添加的订单。

我可以在第一次创建订单时将产品数据(序列化或 json 化)插入到 OrderDetails 列的新列中。但是缺点是很难查询,如果我想从产品数据中查询具体数据。

我的问题是如何设计OrderDetails产品在创建订单时坚持数据,这样以后如果我编辑产品,它就不会丢失这些数据。

The problem here is the OrderDetails is reference back to the Products table. If I edited the Product like price, sku etc, it will affected the previously added order.

建议您在做出这样明显错误的陈述之前先阅读架构。

OrderDetails 引用 Products 不是“问题”,这是我所期望的:客户不得订购不存在的产品。

并回答我关于第一个版本的 etc 中的内容的问题:OrderDetail 包括 price。这是一个常见的结构:OrderDetailprice 取自客户下订单时的 Product。这是给客户的报价;因此我们在法律上有义务向客户收取该价格。如果价格随后在 Product 发生变化,则不会影响此订单。

类似地:IDSKUSizeColour 出现在 OrderDetails 上,所以如果您“编辑了产品”,它 不会影响“之前添加的顺序”。

这里没有什么“难以查询”的。如果您需要产品数据(可能下新订单),请查询 Product。如果需要历史 actual-Order 数据,请查询 OrderDetails。如果您想查找差异,OrderDetails INNER JOIN Product ON OrderDetails.ProductID = Product.ProductID。现在你必须小心 dot-prefix Size, Colour 以获得你想要的 table。但请注意 UnitPrice 不同于 Price.

My question is how to design the OrderDetails product stick to data when the order is created, so in the future if I edited the product, it will not lost that data.

您现在应该可以回答您自己的问题了。