【问题标题】:How to successfully separate "ordered items" from "product items" in a shopping software system design?如何在购物软件系统设计中成功地将“订购商品”与“商品商品”区分开来?
【发布时间】:2020-08-08 13:54:32
【问题描述】:

我正在开发一个用于在线订购产品的应用程序。我将首先向您展示部分 ER 图,然后进行解释。

这里的“products”是食品,它们将放在fresh_products 表中。如您所见,fresh_productproduct speciescategorytypesizeproduct grade 组成。

然后当我下订单时,order idorder_status 将保存在order 表中。所有订购的商品都将保存在order_item 表中。在order_item 表中,您可以看到我与fresh_products直接 连接。

在某些时候,管理层将决定更改现有的fresh_products。例如,让我们以鱼制品为例,例如 金枪鱼。此鱼品有一个名为 Loin(用 Loin 捕捞)的类别,在一段时间后管理层决定将其删除或重命名,因为他们不再用 Loin 捕捞。

现在,我的设计会因为上述变化而受到影响。为什么?当您更改任何fresh_product 字段时,它将直接影响保存已订购商品信息的order_item。因此,如果您重命名 Loin ,则所有具有 Loinorder_items 现在都将链接到错误的新名称。如果您决定删除fresh_product,您也不能这样做,因为现有的订单历史记录绑定到该fresh_product

我的建议

作为这个问题的解决方案,我正在考虑从order_item 中删除fresh_products 的关系。然后,我将String字段添加到order_item中,代表fresh_products的所有字段,例如productTypeprodyctCategory等等。

现在我与fresh_products 没有直接联系,但我拥有所需的所有信息。同时,anyfresh_product的物品可以在order_item已经购买的物品进行任何更改,而不影响已经购买的物品。

我的问题是,我的建议是解决此问题的最佳方法吗?如果你有更好的解决方案,我很乐意。

【问题讨论】:

  • 不可能有那么多变化。审计表怎么样,你保存旧的字段值和你的字段名称,这样你就可以在结果集中重新创建它在订单时所在的状态,但这也意味着保存另一个审计也是。
  • 您不应“更改”现有产品。您应该添加一个新的。
  • @nbk 我认为您指的是与我建议的设计相同的设计?
  • @HomeIsWhereThePcIs 是的,我也听说过。但有时你必须改变一些东西,对吗?
  • 我不明白您为什么要查看旧数据,例如对鱼的描述。可能在账单中,但因此您无需购买任何开销,保存所有可能为子孙后代更改的产品数据,您将产生巨大的开销

标签: java mysql database database-design entity-relationship


【解决方案1】:

考虑以下事项;在这个例子中,只有产品名称发生了变化,但您可以轻松地为每个属性添加列(尽管在某些时候这会从崇高变为荒谬)......

另外,我很少使用相关子查询,如果有错误,请见谅...

create table product_history
(product_id int not null
,product_name varchar(12) not null
,date date not null
,primary key (product_id,date)
);

insert into product_history values
(1,'soda','2020-01-01'),
(1,'pop','2020-01-04'),
(1,'cola','2020-01-07');

create table order_detail
(order_id int not null
,product_id int not null
,quantity int not null default 1
,primary key(order_id,product_id)
);

insert into order_detail values
(100,1,4);

create table orders
(order_id serial primary key
,customer_id int not null
,order_date date not null
);

insert into orders values
(100,22,'2020-01-05');

    SELECT o.order_id
         , o.customer_id
         , o.order_date
         , ph.product_id
         , ph.product_name
         , od.quantity
      FROM orders o
      JOIN order_detail od
        ON od.order_id = o.order_id
      JOIN product_history ph
        ON ph.product_id = od.product_id
     WHERE ph.date IN 
                ( SELECT MAX(x.date) 
                    FROM product_history x
                   WHERE x.product_id = ph.product_id
                     AND x.date <= o.order_date
                );

| order_id | customer_id | order_date | product_id | product_name | quantity |
| -------- | ----------- | ---------- | ---------- | ------------ | -------- |
| 100      | 22          | 2020-01-05 | 1          | pop          | 4        |

---

View on DB Fiddle

【讨论】:

    猜你喜欢
    • 2019-05-29
    • 2017-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    相关资源
    最近更新 更多