我在SQL Server数据库中有一个重要的列(Balance
属性Account
表)。我在我的程序中跟踪并记录此属性,但我也希望在数据库级别中跟踪并保存此列的更改。我不想跟踪表的所有属性,因为它对我来说并不重要。请帮我找一个好的解决方案。
答案 0 :(得分:2)
您可以使用触发器将更改,插入或删除插入到一种审计表中。
https://msdn.microsoft.com/en-AU/library/ms189799(v=sql.110).aspx
CREATE TRIGGER yourInsertTrigger ON Account
FOR INSERT
AS
INSERT INTO yourAuditTable
(balance, user_id, user_name)
SELECT
inserted.balance, user_id, user_name
FROM inserted
go
但请注意,如果触发器太多,触发操作很昂贵,或者表格经常更新,性能会受到影响。
答案 1 :(得分:2)
您可以创建历史记录表:
<强>记录
hrs = raw_input("Enter Hours:")
h = float(hrs)
rate = raw_input("Enter Rate:")
r = float(rate)
pay = h*r
ifh = ("raw_input<=40")
print pay
ifh = ("raw_input>40")
pay = (40*r)+(h-40)*(1.5*r)
print pay
在该表格中,您可以使用表格帐户插入每项更改。 为此你可以使用触发器:
account_ID
column_name
old_value
new_value
where子句很重要,因为在插入新行时需要在CREATE TRIGGER account_UID on Account
FOR INSERT,UPDATE,DELETE
AS
BEGIN
-- for update
INSERT INTO history (account_ID, column_name, old_value, new_value)
SELECT I.account_ID, 'balance', D.balance, I.balance
FROM inserted as I left join deleted as D on I.account_ID = D.account_ID
where D.account_ID is not null and I.balance <> D.balance
-- for insert
INSERT INTO history (account_ID, column_name, old_value, new_value)
SELECT I.account_ID, 'balance', null, I.balance
FROM inserted as I left join deleted as D on I.account_ID = D.account_ID
where D.account_ID is null
-- for delete
INSERT INTO history (account_ID, column_name, old_value, new_value)
SELECT D.account_ID, 'balance', D.balance, null
FROM deleted as D left join inserted as I on D.account_ID = I.account_ID
where D.account_ID is not null
END
中插入null
,在删除行时需要old_value
ind null
抱歉我的英文。