在数据库中维护历史记录

jas*_*nco 7 database history triggers database-design linq-to-sql

我正在设计这个数据库,该数据库必须保留员工薪资和组织内部动向的历史记录.基本上,我的设计有3个表(我的意思是,有更多的表,但对于这个问题,我会提到3,所以请耐心等待).员工表(包含最新的薪水,职位数据等),SalaryHistory表(薪水,日期,原因等)和MovementHistory(Title,Dept.,comments).我将使用Linq到Sql,所以我想的是每次更新员工数据时,旧值将被复制到各自的历史表中.这是一个好方法吗?我应该使用Linq to SQL或触发器吗?感谢您提供任何帮助,建议或想法.

san*_*247 8

请查看http://www.simple-talk.com/sql/database-administration/database-design-a-point-in-time-architecture.

基本上,本文建议您在跟踪历史记录的表格中包含以下列:

* DateCreated – the actual date on which the given row was inserted.
* DateEffective – the date on which the given row became effective.
* DateEnd – the date on which the given row ceased to be effective.
* DateReplaced – the date on which the given row was replaced by another row.
* OperatorCode – the unique identifier of the person (or system) that created the row. 
Run Code Online (Sandbox Code Playgroud)

DateEffective和DateEnd一起告诉您行有效的时间(或者员工在某个部门的时间,或者他获得特定薪水的时间).


Ant*_*ony 6

将该逻辑保持在数据库内部是一个好主意:这基本上就是触发器存在的原因.然而,我谨慎地说,因为有很多理由让它保持外部.通常情况下 - 特别是使用像LINQ-to-SQL这样简单的技术 - 从外部编写代码更容易.根据我的经验,更多人可以在C#/ LINQ中编写该逻辑,而不是使用触发器正确执行.

触发器很快 - 它们被编译!但是,它们很容易被误用,并使您的逻辑过于复杂,以至于性能可能会迅速降低.考虑到你的用例有多简单,我会选择使用触发器,但那是我个人的.