简体   繁体   English

在OLTP系统中实现行版本控制的DataModel

[英]DataModel for Implementing row versioning in OLTP system

I have following table 我有下表

  • Person(PID, Name, Dob, AddressId) PID being surrogate key(auto incremented) 人员(PID,名称,Dob,AddressId) PID是代理密钥(自动递增)

  • Address(AddressId, Line1, Line2, City, State) AddressId being surrogate key(auto incremented) 地址(地址标识,线1,线2,城市,州)代理键的地址标识 (自动递增)

PersonID (PID) is used in my lot of transaction tables. 我的许多交易表中都使用了PersonID(PID)。 Since address of a person may change over the period of time, I need to store record version in database so that I can refer correct version according to time slice. 由于一个人的地址可能会随时间变化,因此我需要将记录版本存储在数据库中,以便可以根据时间段引用正确的版本。 How should I handle it. 我应该如何处理。

I am thinking of having solution something like this 我正在考虑解决这样的问题

  • Using same table to store history eg 使用同一张表存储历史记录,例如
  • Person(PID, Name, Dob, AddressId) 人(PID,姓名,Dob,AddressId)
  • Address(AddressId, Line1, Line2, City, State, StartDate, EndDate, LastUpdatedBy, LastUpdatedDate) 地址(AddressId,Line1,Line2,城市,州,StartDate,EndDate,LastUpdatedBy,LastUpdatedDate)

Let's assume initial record of person as below 如下假设人的初始记录

Person(100, X, Y, 110)
Address(110, A, B, C, D, 2011-01-01, 9999-12-31, Y, 2011-01-01)

Now address was changed in 2012 for PID 100. I am thinking to do 现在,地址在2012年更改为PID100。我正在考虑这样做

Address(320, A, B, C, D, 2011-01-01, 2012-06-01, Y, 2011-01-01) - INSERT
Address(110, A, B, Q, D, 2012-06-01, 9999-12-31, Z, 2012-06-01) - UPDATE

if address changes again in 2014 for PID 100. It would look like 如果2014年 PID 100的地址再次更改。

Address(740, A, B, Q, D, 2012-06-01, 2014-06-01, Y, 2012-01-01) - INSERT
Address(320, A, B, C, D, 2011-01-01, 2012-06-01, Y, 2011-01-01) - NO CHANGE
Address(110, A, B, N, D, 2014-06-01, 9999-12-31, R, 2012-06-01) - UPDATE

Now I can query historical data as well as current data from same table without changing surrogate key. 现在,我可以查询历史数据以及同一表中的当前数据,而无需更改代理键。 I won't have to refer historical tables hence no bombarding of logic and tables. 我将不必引用历史表,因此不会轰炸逻辑和表。

This approach won't let me enforce unique key constraint on tables assuming I have Address Code being unique. 假设我的地址代码是唯一的,这种方法不会让我对表实施唯一的键约束。 How shall I handle this? 我该如何处理?

Please suggest different approach and feedback on this. 请为此提出不同的方法和反馈。

What I would do is to make a relation table that contains dates: 我要做的是制作一个包含日期的关系表:

Person_At_Address(PID,AddressId,StartDate,EndDate,LastUpdatedBy,LastUpdatedDate)

And remove the corresponding columns from Person and Address yielding: 并从Person和Address产生中删除相应的列:

Person(PID, Name, Dob)
Address(AddressId, Line1, Line2, City, State)

Now whenever a person changes the address you just insert a new row into the Person_At_Address table. 现在,每当有人更改地址时,您只需在Person_At_Address表中插入新行。 When querying, you have got all the info you need about historic and current data. 查询时,您已经获得了有关历史数据和当前数据的所有信息。

As for your case with NO-CHANGE row, you have number of options to pull it out: 至于NO-CHANGE行的情况,您有很多选择可以将其拉出:

  • You can have a scheduled job that inserts rows at given checkpoints in time if there is no row present for that time slice. 如果该时间片没有行,则可以有一个计划的作业,该作业在给定的检查点及时插入行。 Then select query gives you exactly the above output. 然后,选择查询将为您提供上面的输出。
  • You can also create a display like that after you run the query by filling in the missing rows in your application. 您也可以在运行查询后通过填写应用程序中缺少的行来创建类似的显示。 That is fairly simple also. 那也很简单。

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

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