[英]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_items
, order_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: 一些可能的解决方
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_info
和product_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. 产品属性必须具有
date
或timestamp
值,以定义属性更改的时间。 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.