[英]How can the data on the transaction still be used when the product has been deleted
enter image description here 在此处输入图片说明
I have a problem regarding transaction data in the reports of products that have been sold. 我对已售产品报告中的交易数据有疑问。 Constraints that occur if the product has entered the transaction / order but the product in question has been deleted. 如果产品已输入交易/订单但有问题的产品已被删除,则会发生约束。
How do I make the transaction report can still be used with the product that was deleted earlier? 如何使交易报告仍可与之前删除的产品一起使用?
Thanks. 谢谢。
Perhaps the best way to handle this possibility would be to add constraints to the tables involved which would make it not possible to delete a product row if an extant order exists which uses that product. 处理这种可能性的最佳方法可能是向所涉及的表中添加约束,如果存在使用该产品的现有订单,则无法删除该产品行。 Here is what the table definitions for Products
and Order_Items
might look like: 这是Products
和Order_Items
的表定义的样子:
CREATE TABLE Order_Items (
order_item_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
order_id INT NOT NULL,
product_id INT NOT NULL,
...
FOREIGN KEY fk_prod (product_id) REFERENCES Products (product_id)
)
CREATE TABLE Products (
product_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
product_name VARCHAR(255) NOT NULL,
...
)
With this design in place, should someone attempt to delete a product record from the Products
table while there exists on or more order item records referring to that product, the delete attempt would fail. 采用这种设计后,如果有人尝试从“ Products
表中删除产品记录,而同时存在一个或多个引用该产品的订单项记录,则删除尝试将失败。
If a row in the Products
table is used for two things, namely for purchasing the product and for reporting on a historical transaction, then you can never actually delete the row. 如果“ Products
表中的行用于两件事,即用于购买产品和报告历史交易,则您永远无法实际删除该行。
You might flag it in various ways: "available_for_purchase", "inventory = 0", "no_longer_produced", etc. 您可以通过多种方式标记它:“ available_for_purchase”,“库存= 0”,“ no_longer_produced”等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.