简体   繁体   English

构建数据库以跟踪数据的某些更改

[英]Structuring a database to track certain changes to data

I have a SQL Server 2008 R2 database in which I need to track certain changes made to the data. 我有一个SQL Server 2008 R2数据库,需要在其中跟踪对数据所做的某些更改。 So far all of my research has brought up ways to do quite complex tracking of every change made, but my case is somewhat more specific and, I think, simpler as I only need to track a change until it is approved (by an administrator). 到目前为止,我的所有研究都提出了对每个所做的更改进行相当复杂的跟踪的方法,但是我的案例更为具体,而且我认为更简单,因为我只需要跟踪更改,直到批准(由管理员)即可。 。

So far the only way I can think of to do this is, for each affected table, to create a pending_changes table which tracks the type of change (ie INSERT , UPDATE or DELETE ), the primary key, the column name, and, where applicable, the value or values to be changed. 到目前为止,我唯一想到的方法是为每个受影响的表创建一个pending_changes表,该表跟踪更改的类型(即INSERTUPDATEDELETE ),主键,列名以及其中的位置适用的值或要更改的值。

Then when a change is approved, a SQL statement can be generated from this to write the change to the main table. 然后,当更改被批准时,可以由此生成一条SQL语句,以将更改写入主表。 This feels like a very clumsy way of doing it though, and it will be difficult to query this (eg to produce a list of old vs new values). 不过,这听起来像是一种非常笨拙的方式,并且很难查询(例如,生成旧值对新值的列表)。

This is a very small database (< 1000000 rows) with only about 5 users, so performance is not an issue. 这是一个非常小的数据库(<1000000行),只有大约5个用户,因此性能不是问题。

EDIT: The purpose of tracking changes is that they need to be approved before they can be written to the database. 编辑:跟踪更改的目的是在将更改写入数据库之前需要对其进行批准。 The data is mostly scientific with legal implications, so changes proposed by a user (in this case a client) need to be checked by an expert before they can be approved. 数据大部分是具有法律含义的科学数据,因此用户(在这种情况下是客户)提出的更改需要由专家进行检查,然后才能被批准。 Once they have been approved, we don't need to know the old values any more. 一旦获得批准,我们就不再需要了解旧的价值观。 All changes, including DELETE statements need this approval. 包括DELETE语句在内的所有更改都需要得到此批准。 INSERT and UPDATE could be dealt with by creating a similar table which holds the changed data, but I'm not sure how to deal with deletions. INSERTUPDATE可以通过创建一个包含已更改数据的类似表来处理,但我不确定如何处理删除。 I also want to avoid too many extra tables because there are several base tables I need to track, and I am worried about the added complexity. 我还想避免过多的表,因为我需要跟踪几个基本表,并且我担心会增加复杂性。

You can use an "Instead of" trigger. 您可以使用“代替”触发器。

(not tested) (未测试)

CREATE TRIGGER IO_Trig_INS_Employee ON Employee
INSTEAD OF INSERT
AS
BEGIN
  insert pending_employee
    select * from inserted
end

http://msdn.microsoft.com/en-us/library/ms175521(v=SQL.105).aspx http://msdn.microsoft.com/zh-CN/library/ms175521(v=SQL.105).aspx

The MERGE keyword can help with applying the changes. MERGE关键字可以帮助您应用更改。 You may have to drop the trigger before making the change though. 但是,您可能必须先删除触发器,然后才能进行更改。

http://msdn.microsoft.com/en-us/library/bb510625.aspx http://msdn.microsoft.com/en-us/library/bb510625.aspx

Have you considered handling this in the application? 您是否考虑过在应用程序中处理此问题? Something like an approval workflow in Sharepoint might be better suited. Sharepoint中的批准工作流之类的方法可能更适合。

Are you tracking the changes with the intent of preventing undesirable ones before they are made, or to be able to review/revert the values back after they are made? 您是否在跟踪更改的目的是防止不良更改发生,还是能够在更改后重新查看/还原值?

If you are okay with allowing the user to make the insert/update/delete and then have an admin approve it, consider using OUTPUT INTO clause. 如果您可以允许用户进行插入/更新/删除然后由管理员批准,则可以考虑使用OUTPUT INTO子句。 There is a great example in the section labeled "C. Using OUTPUT INTO with an UPDATE statement" at this page: http://msdn.microsoft.com/en-us/library/ms177564.aspx 在此页上,标有“ C.使用OUTPUT INTO和UPDATE语句”一节中有一个很好的示例: http : //msdn.microsoft.com/zh-cn/library/ms177564.aspx

USE AdventureWorks2012;
GO
DECLARE @MyTableVar table(
    EmpID int NOT NULL,
    OldVacationHours int,
    NewVacationHours int,
    ModifiedDate datetime);
UPDATE TOP (10) HumanResources.Employee
SET VacationHours = VacationHours * 1.25,
    ModifiedDate = GETDATE() 
OUTPUT inserted.BusinessEntityID,
       deleted.VacationHours,
       inserted.VacationHours,
       inserted.ModifiedDate
INTO @MyTableVar;
--Display the result set of the table variable.
SELECT EmpID, OldVacationHours, NewVacationHours, ModifiedDate
FROM @MyTableVar;
GO
--Display the result set of the table.
SELECT TOP (10) BusinessEntityID, VacationHours, ModifiedDate
FROM HumanResources.Employee;
GO

As you can see, this will allow you to record both the "before" and "after" values of each column. 如您所见,这将允许您记录每列的“之前”和“之后”值。

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

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