gpt4 book ai didi

postgresql - 数据库中的版本控制

转载 作者:行者123 更新时间:2023-11-29 14:09:13 25 4
gpt4 key购买 nike

每次对数量敏感表进行更新时,我都想存储该行的完整版本。

到目前为止,我已决定使用以下方法。

  1. 不允许更新。
  2. 每次更新时创建一个新的表中的条目。

但是,我不确定什么是适合这种变化的最佳数据库结构设计。

当前结构

主键:id

id(int) | amount(decimal) | other_columns 

第一种方法

复合主键:id、version

id(int) | version(int) | amount(decimal) | change_reason 
1 | 1 | 100 |
1 | 2 | 20 | correction

第二种方法

主键:id

[origin_id, version] 的唯一性索引

id(int) | origin_id(int) | version(int) | amount(decimal) | change_reason 
1 | NULL | 1 | 100 | NULL
2 | 1 | 2 | 20 | correction

最佳答案

我会建议一个新表来存储项目的 unique id。这用作所有可用项目的查找表。

项目表:

id(int)
1000

对于存储item所有变化的表,我们称它为item_changes表。 item_iditem 表的 idFOREIGN KEYitem表与item_changes表之间的关系,是一对多关系。

item_changes表:

id(int) | item_id(int)   | version(int) | amount(decimal) | change_reason 
1 | 1000 | 1 | 100 | NULL
2 | 1000 | 2 | 20 | correction

有了这个,item_id 永远不会是 NULL,因为它是 item 表的有效 FOREIGN KEY

关于postgresql - 数据库中的版本控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50118372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com