简体   繁体   English

通过不断编辑维护订单数据的完整性

[英]Maintaining order data integrity with constant edits

hypothetically, if i were with a system that keeps track of products and order information across multiple tables (orders, order_items, product) 假设,如果我使用的系统可以跟踪多个表(订单,订单项,产品)中的产品和订单信息

orders
    id              INT(11)
    shipping_name   VARCHAR(255)
    shipping_street VARCHAR(255)
    shipping_city   VARCHAR(255)
    [etc] 

order_details
    id              INT(11)
    order_id        INT(11)
    product_id      INT(11)

products
    id              INT(11)
    name            VARCHAR(255)
    description     VARCHAR(255)
    price           DECIMAL(8,2)

structure is very simple order has multiple order_items , order_items has one product . 结构非常简单, order有多个order_itemsorder_items有一个product

the problem is that when someone edits a product, those edits modify the data of previous orders. 问题是当有人编辑产品时,这些编辑会修改以前订单的数据。 if an employee were to go back and look at that information later on, they may not have the same information that the customer received at the time the order was placed. 如果员工稍后返回并查看该信息,则可能与订单下达时客户收到的信息不同。

What would be best practice? 什么是最佳做法? should i add a 'display_item' field to the products table, and on edit/delete set display to 0 and add edited product as new row? 我应该在products表中添加'display_item'字段,并在编辑/删除设置显示为0并将编辑后的产品添加为新行? should i duplicate the name, description, and price in order_details? 我应该在order_details中复制名称,描述和价格吗?

I think this is one of those cases where database normalization "breaks". 我认为这是数据库规范化“中断”的情况之一。

Some possible solutions: 一些可能的解决方

  1. Keep a copy of the product attributes for each order. 保留每个订单的产品属性的副本。 This is expensive in terms of storage, but it makes it easier to track down the product data stored in the order. 这在存储方面很昂贵,但它可以更容易地跟踪订单中存储的产品数据。
  2. Create a log of attributes that can be changed in time. 创建可以及时更改的属性日志。 Product attributes can change over time, so a log which stores the modification date can help you filter out the product attributes to the moment the order was made. 产品属性可能会随时间而变化,因此存储修改日期的日志可帮助您过滤产品属性,直至订单生成。

Proposal for option 1 备选方案1的提案

Create a copy of the products table and create a relation (one-to-one) to the order_details table for each order and order detail. 创建products表的副本,并为order_details表创建每个订单订单明细的关系(一对一)。

Proposal for option 2 备选方案2的提案

Split the products table in two: product_general_info and product_attributes . products表拆分为两个: product_general_infoproduct_attributes Product general info is meant to be stable through time (a product's general info will not change), as any modification to the data in this table will propagate to the whole orders set. 产品常规信息在时间上是稳定的(产品的一般信息不会改变),因为对此表中数据的任何修改都将传播到整个订单集。 Product attributes must have a date or timestamp value to define when the attributes changed. 产品属性必须具有datetimestamp值,以定义属性更改的时间。 Then you can query the database and return the last record that is before or on the order date. 然后,您可以查询数据库并返回订单日期之前或之后的最后一条记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM