In relational databases, the log trigger or history trigger is a mechanism for automatic recording of information about changes inserting or/and updating or/and deleting rows in a database table.
It is a particular technique for change data capturing, and in data warehousing for dealing with slowly changing dimensions.
Operational databases are typically designed to capture the current state of an organization, acting as a snapshot of "now" rather than a historical archive. In this environment, updates are often destructive; when a specific data point changes, the system prioritizes efficiency by replacing the existing value with the new one. For instance, in an employee or customer directory, if an individual moves to a new location, an update operation is performed on the database that writes the new address directly over the old one. Consequently, the previous address is permanently overwritten and lost to the system, leaving the database with only the most up-to-date information and no record of the entity's history or previous status.
The Log trigger is a mechanism to automatically detect changes and to store the previous status of information.
Suppose there is a table which we want to audit. This table contains the following columns:
<code>Column1, Column2, ..., Columnn</code>
The column <code>Column1</code> is assumed to be the primary key.
These columns are defined to have the following types:
<code>Type1, Type2, ..., Typen</code>
The Log Trigger works writing the changes (INSERT, UPDATE and DELETE operations) on the table in another, history table, defined as following:
As shown above, this new table contains the same columns as the original table, and additionally two new columns of type <code>DATETIME</code>: <code>StartDate</code> and <code>EndDate</code>. This is known as tuple versioning. These two additional columns define a period of time of "validity" of the data associated with a specified entity (the entity of the primary key), or in other words, it stores how the data were in the period of time between the <code>StartDate</code> (included) and <code>EndDate</code> (not included).
For each entity (distinct primary key) on the original table, the following structure is created in the history table. Data is shown as example.
Notice that if they are shown chronologically the <code>EndDate</code> column of any row is exactly the <code>StartDate</code> of its successor (if any). It does not mean that both rows are common to that point in time, since -by definition- the value of <code>EndDate</code> is not included.
There are two variants of the Log trigger, depending how the old values (DELETE, UPDATE) and new values (INSERT, UPDATE) are exposed to the trigger (it is RDBMS dependent):
Old and new values as fields of a record data structure
Old and new values as rows of virtual tables
The code above is shown as a code idiom. Trigger syntax vary enormously among RDBMS, for example:
Source:
Source:
Typically, database backups are used to store and retrieve historic information. A database backup is a security mechanism, more than an effective way to retrieve ready-to-use historic information.
A (full) database backup is only a snapshot of the data in specific points of time, so we could know the information of each snapshot, but we can know nothing between them. Information in database backups is discrete in time.
Using the log trigger the information we can know is not discrete but continuous, we can know the exact state of the information in any point of time, only limited to the granularity of time provided with the <code>DATETIME</code> data type of the RDBMS used.
It should return the same resultset of the whole original table.
Suppose the <code>@DATE</code> variable contains the point or time of interest.
Suppose the <code>@DATE</code> variable contains the point or time of interest, and the <code>@KEY</code> variable contains the primary key of the entity of interest.
Suppose the <code>@KEY</code> variable contains the primary key of the entity of interest.
Suppose the <code>@KEY</code> variable contains the primary key of the entity of interest.
Since the trigger requires that primary key being the same throughout time, it is desirable to either ensure or maximize its immutability, if a primary key changed its value, the entity it represents would break its own history.
There are several options to achieve or maximize the primary key immutability:
According with the slowly changing dimension management methodologies, The log trigger falls into the following:
The Log trigger was designed by Laurence R. Ugalde to automatically generate history of transactional databases.