数据库中每个项目的价格历史


Price History for each item in database

我有这个表

Transactions
- id
- user_id // this will point to which user did this transaction
- item_id // this will point to which item in this transaction
- date


Products
- id
- name


Items
- id
- product_id // this will point to which product this item is
- name
- price
问题是,随着时间的推移,商品的价格会发生变化。
防止更改旧交易历史的价格
我需要一个保持旧商品价格的方法。

我如何做正确的数据库设计来解决这个问题?

您需要在事务中添加此信息:

Transaction
 - id
 - user_id
 - item_id
 - item_price
 - any_other_transaction_specific_info

这也允许您对每笔交易的实际价格应用折扣或任何其他修改。

最好在包含price列的事务表中保存price当前产品价格。

<>之前交易- - - - - - id- user_id//这将指向哪个用户执行了此事务- item_id//这将指向该事务中的哪个项目——日期——价格

我认为您可以为每个事务记录定义一个date。另一方面,您可以有另一个表来记录产品价格priceLogs.(like: product_id, price and date)。只要产品的价格发生变化,就会向表中添加一条记录。这也将帮助您在更多的业务问题上跟踪产品的价格。

您需要根据交易日期查询每个产品的价格。这可能需要您在查询中编写一些连接。

在我看来,您应该创建另一个表Item_Pricing,这将帮助您最终确定定价历史。表Item_Pricing历史应该是这样的:Item_Pricing(id[PK], itm_id[FK], price, price_still_in_use, date_price_created,…)这个想法是,列price_still_in_use应该被视为一个值为1(是)或0(否)的标志。然后在表事务中,这是应该考虑的价格。然后,每笔交易或销售都应与仍在使用的定价相关,如下所示:

事务(id (PK),……,item_pricing_id[FK]//这将指向Item_Pricing(item_pricing_id), quantity, date)

因此,如果应用程序中的项目价格发生变化,则如果不再使用该项目,该项目的价格将不再可用。触发器还可以帮助您获得最佳结果,以便同一商品不能有多个列(属性)price_still_in_use

的值为yes的价格。