如何跟踪数据库表中的数据变化[英] How to track data changes in a database table

本文是小编为大家收集整理的关于如何跟踪数据库表中的数据变化的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

跟踪数据库表更改的最佳方法是什么?

假设您有一个应用程序,其中用户(在应用程序的上下文中不是 DB 用户)能够更改存储在某个数据库表中的数据.跟踪所有更改历史记录的最佳方式是什么,以便您可以显示哪个用户在什么时间更改了哪些数据?

推荐答案

一般来说,如果您的应用程序是分层结构的,则让数据访问层调用数据库服务器上的存储过程来写入数据库更改的日志.

在支持这种事情的语言中,面向方面的编程可能是一个很好的选择用于此类应用的技术.审计数据库表更改是您通常希望为所有操作记录的那种操作,因此 AOP 可以很好地工作.

请记住,记录数据库更改会产生大量数据并降低系统速度.根据应用程序的大小,使用消息队列解决方案和单独的数据库来执行审计日志可能是明智的.

使用存储过程来处理这个问题也是完全可行的,尽管可能需要一些工作涉及将用户凭据传递到数据库本身.

本文地址:https://www.itbaoku.cn/post/597579.html

问题描述

What is the best way to track changes in a database table?

Imagine you got an application in which users (in the context of the application not DB users ) are able to change data which are store in some database table. What's the best way to track a history of all changes, so that you can show which user at what time change which data how?

推荐答案

In general, if your application is structured into layers, have the data access tier call a stored procedure on your database server to write a log of the database changes.

In languages that support such a thing aspect-oriented programming can be a good technique to use for this kind of application. Auditing database table changes is the kind of operation that you'll typically want to log for all operations, so AOP can work very nicely.

Bear in mind that logging database changes will create lots of data and will slow the system down. It may be sensible to use a message-queue solution and a separate database to perform the audit log, depending on the size of the application.

It's also perfectly feasible to use stored procedures to handle this, although there may be a bit of work involved passing user credentials through to the database itself.