【问题标题】:Shopping Cart Database Structure购物车数据库结构
【发布时间】:2023-03-16 11:43:01
【问题描述】:

我一直在研究购物车的数据库结构,并注意到在存储订单详细信息时,产品信息会重复并再次存储在表中。我想知道这背后的原因是什么?这是我的意思的一个小例子:

产品表

product_id     name               desc         price
1            product 1    This is product 1    27.00

订单表

order_id   customer id     order_total
1             3               34.99

订单明细表

order_details_id    product_id       product name      price    qty
       1                1              product 1        27.00     1

如您所见,产品名称和价格再次存储在订单详细信息表中。为什么是这样?我能想到的唯一原因是因为下订单后产品详细信息可能会发生变化,这可能会导致混淆。这是正确的吗?

谢谢

保罗

【问题讨论】:

    标签: mysql shopping-cart


    【解决方案1】:

    是的,这是唯一的原因

    您的产品价格经常变化

    这样您就可以再创建一张表并存储产品的详细信息,如下所示

    产品更新表

    id product_id     name               desc         price
    1     1            product 1    This is product 1    27.00
    

    订单表将是

    order_details_id    product_Update_id  qty
           1                1               1
    

    【讨论】:

    【解决方案2】:

    这可能是出于性能原因,而不必执行 JOIN,只需执行直接 SELECT 即可获取订单详细信息。

    【讨论】:

      【解决方案3】:

      你可以根据自己的结构修改

      product_id     name               desc                 price
      1              product 1          This is product 1    27.00
      
      order_id  product_id  customer id   order_total
      1         1           3             34.99
      
      order_details_id   order_id  qty
      1                  1         1
      

      在 orderdetail 中不需要取 productname 及其价格,只需取 order id 并在订单表中添加一个字段 product_id

      【讨论】:

      • 您是否建议只在订单表中添加一个 product_id 列?这肯定不正确吗?如果订单中有多个产品怎么办?
      猜你喜欢
      • 2023-03-21
      • 2012-05-11
      • 2012-07-16
      • 2015-01-26
      • 1970-01-01
      • 1970-01-01
      • 2013-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多