<s>
In	O
relational	B-Application
databases	I-Application
,	O
the	O
log	B-Application
trigger	I-Application
or	O
history	B-Application
trigger	I-Application
is	O
a	O
mechanism	O
for	O
automatic	O
recording	O
of	O
information	O
about	O
changes	O
inserting	O
or/and	O
updating	O
or/and	O
deleting	O
rows	B-Application
in	O
a	O
database	B-Application
table	I-Application
.	O
</s>
<s>
It	O
is	O
a	O
particular	O
technique	O
for	O
change	B-General_Concept
data	I-General_Concept
capturing	I-General_Concept
,	O
and	O
in	O
data	O
warehousing	O
for	O
dealing	O
with	O
slowly	B-Application
changing	I-Application
dimensions	I-Application
.	O
</s>
<s>
Suppose	O
there	O
is	O
a	O
table	B-Application
which	O
we	O
want	O
to	O
audit	O
.	O
</s>
<s>
This	O
table	B-Application
contains	O
the	O
following	O
columns	B-Application
:	O
</s>
<s>
The	O
column	B-Application
Column1	O
is	O
assumed	O
to	O
be	O
the	O
primary	B-Application
key	I-Application
.	O
</s>
<s>
These	O
columns	B-Application
are	O
defined	O
to	O
have	O
the	O
following	O
types	O
:	O
</s>
<s>
The	O
Log	B-Application
Trigger	I-Application
works	O
writing	O
the	O
changes	O
(	O
INSERT	B-Language
,	O
UPDATE	B-Language
and	O
DELETE	B-Language
operations	O
)	O
on	O
the	O
table	B-Application
in	O
another	O
,	O
history	O
table	B-Application
,	O
defined	O
as	O
following	O
:	O
</s>
<s>
As	O
shown	O
above	O
,	O
this	O
new	O
table	B-Application
contains	O
the	O
same	O
columns	B-Application
as	O
the	O
original	O
table	B-Application
,	O
and	O
additionally	O
two	O
new	O
columns	B-Application
of	O
type	O
DATETIME	O
:	O
StartDate	O
and	O
EndDate	O
.	O
</s>
<s>
This	O
is	O
known	O
as	O
tuple	B-Application
versioning	I-Application
.	O
</s>
<s>
These	O
two	O
additional	O
columns	B-Application
define	O
a	O
period	O
of	O
time	O
of	O
"	O
validity	O
"	O
of	O
the	O
data	O
associated	O
with	O
a	O
specified	O
entity	O
(	O
the	O
entity	O
of	O
the	O
primary	B-Application
key	I-Application
)	O
,	O
or	O
in	O
other	O
words	O
,	O
it	O
stores	O
how	O
the	O
data	O
were	O
in	O
the	O
period	O
of	O
time	O
between	O
the	O
StartDate	O
(	O
included	O
)	O
and	O
EndDate	O
(	O
not	O
included	O
)	O
.	O
</s>
<s>
For	O
each	O
entity	O
(	O
distinct	O
primary	B-Application
key	I-Application
)	O
on	O
the	O
original	O
table	B-Application
,	O
the	O
following	O
structure	O
is	O
created	O
in	O
the	O
history	O
table	B-Application
.	O
</s>
<s>
Notice	O
that	O
if	O
they	O
are	O
shown	O
chronologically	O
the	O
EndDate	O
column	B-Application
of	O
any	O
row	B-Application
is	O
exactly	O
the	O
StartDate	O
of	O
its	O
successor	O
(	O
if	O
any	O
)	O
.	O
</s>
<s>
It	O
does	O
not	O
mean	O
that	O
both	O
rows	B-Application
are	O
common	O
to	O
that	O
point	O
in	O
time	O
,	O
since	O
-by	O
definition	O
-	O
the	O
value	O
of	O
EndDate	O
is	O
not	O
included	O
.	O
</s>
<s>
There	O
are	O
two	O
variants	O
of	O
the	O
Log	B-Application
trigger	I-Application
,	O
depending	O
how	O
the	O
old	O
values	O
(	O
DELETE	B-Language
,	O
UPDATE	B-Language
)	O
and	O
new	O
values	O
(	O
INSERT	B-Language
,	O
UPDATE	B-Language
)	O
are	O
exposed	O
to	O
the	O
trigger	O
(	O
it	O
is	O
RDBMS	B-Application
dependent	O
)	O
:	O
</s>
<s>
The	O
function	O
GetDate( )	O
is	O
used	O
to	O
get	O
the	O
system	O
date	O
and	O
time	O
,	O
a	O
specific	O
RDBMS	B-Application
could	O
either	O
use	O
another	O
function	O
name	O
,	O
or	O
get	O
this	O
information	O
by	O
another	O
way	O
.	O
</s>
<s>
Several	O
RDBMS	B-Application
(	O
Db2	B-Application
,	O
MySQL	B-Application
)	O
do	O
not	O
support	O
that	O
the	O
same	O
trigger	O
can	O
be	O
attached	O
to	O
more	O
than	O
one	O
operation	O
(	O
INSERT	B-Language
,	O
DELETE	B-Language
,	O
UPDATE	B-Language
)	O
.	O
</s>
<s>
In	O
such	O
a	O
case	O
a	O
trigger	O
must	O
be	O
created	O
for	O
each	O
operation	O
;	O
For	O
an	O
INSERT	B-Language
operation	O
only	O
the	O
inserting	O
section	O
must	O
be	O
specified	O
,	O
for	O
a	O
DELETE	B-Language
operation	O
only	O
the	O
deleting	O
section	O
must	O
be	O
specified	O
,	O
and	O
for	O
an	O
UPDATE	B-Language
operation	O
both	O
sections	O
must	O
be	O
present	O
,	O
just	O
as	O
it	O
is	O
shown	O
above	O
(	O
the	O
deleting	O
section	O
first	O
,	O
then	O
the	O
inserting	O
section	O
)	O
,	O
because	O
an	O
UPDATE	B-Language
operation	O
is	O
logically	O
represented	O
as	O
a	O
DELETE	B-Language
operation	O
followed	O
by	O
an	O
INSERT	B-Language
operation	O
.	O
</s>
<s>
On	O
a	O
specific	O
RDBMS	B-Application
they	O
could	O
have	O
different	O
names	O
.	O
</s>
<s>
In	O
the	O
code	O
shown	O
,	O
the	O
virtual	O
tables	B-Application
are	O
called	O
DELETED	O
and	O
INSERTED	O
.	O
</s>
<s>
On	O
a	O
specific	O
RDBMS	B-Application
they	O
could	O
have	O
different	O
names	O
.	O
</s>
<s>
Another	O
RDBMS	B-Application
(	O
Db2	B-Application
)	O
even	O
let	O
the	O
name	O
of	O
these	O
logical	O
tables	B-Application
be	O
specified	O
.	O
</s>
<s>
In	O
the	O
code	O
shown	O
,	O
comments	O
are	O
in	O
C/C	O
++	O
style	O
,	O
they	O
could	O
not	O
be	O
supported	O
by	O
a	O
specific	O
RDBMS	B-Application
,	O
or	O
a	O
different	O
syntax	O
should	O
be	O
used	O
.	O
</s>
<s>
Several	O
RDBMS	B-Application
require	O
that	O
the	O
body	O
of	O
the	O
trigger	O
is	O
enclosed	O
between	O
BEGIN	O
and	O
END	O
keywords	O
.	O
</s>
<s>
According	O
with	O
the	O
slowly	B-Application
changing	I-Application
dimension	I-Application
management	O
methodologies	O
,	O
The	O
log	B-Application
trigger	I-Application
falls	O
into	O
the	O
following	O
:	O
</s>
<s>
A	O
trigger	O
cannot	O
be	O
attached	O
to	O
more	O
than	O
one	O
operation	O
(	O
INSERT	B-Language
,	O
DELETE	B-Language
,	O
UPDATE	B-Language
)	O
,	O
so	O
a	O
trigger	O
must	O
be	O
created	O
for	O
each	O
operation	O
.	O
</s>
<s>
The	O
same	O
trigger	O
can	O
be	O
attached	O
to	O
all	O
the	O
INSERT	B-Language
,	O
DELETE	B-Language
,	O
and	O
UPDATE	B-Language
operations	O
.	O
</s>
<s>
Old	O
and	O
new	O
values	O
as	O
rows	B-Application
of	O
virtual	O
tables	B-Application
named	O
DELETED	O
and	O
INSERTED	O
.	O
</s>
<s>
A	O
trigger	O
cannot	O
be	O
attached	O
to	O
more	O
than	O
one	O
operation	O
(	O
INSERT	B-Language
,	O
DELETE	B-Language
,	O
UPDATE	B-Language
)	O
,	O
so	O
a	O
trigger	O
must	O
be	O
created	O
for	O
each	O
operation	O
.	O
</s>
<s>
The	O
same	O
trigger	O
can	O
be	O
attached	O
to	O
all	O
the	O
INSERT	B-Language
,	O
DELETE	B-Language
,	O
and	O
UPDATE	B-Language
operations	O
.	O
</s>
<s>
It	O
is	O
necessary	O
to	O
test	O
the	O
nullity	O
of	O
the	O
fields	O
of	O
the	O
:NEW	O
record	O
that	O
define	O
the	O
primary	B-Application
key	I-Application
(	O
when	O
a	O
DELETE	B-Language
operation	O
is	O
performed	O
)	O
,	O
in	O
order	O
to	O
avoid	O
the	O
insertion	O
of	O
a	O
new	O
row	B-Application
with	O
null	O
values	O
in	O
all	O
columns	B-Application
.	O
</s>
<s>
Using	O
the	O
log	B-Application
trigger	I-Application
the	O
information	O
we	O
can	O
know	O
is	O
not	O
discrete	O
but	O
continuous	O
,	O
we	O
can	O
know	O
the	O
exact	O
state	O
of	O
the	O
information	O
in	O
any	O
point	O
of	O
time	O
,	O
only	O
limited	O
to	O
the	O
granularity	O
of	O
time	O
provided	O
with	O
the	O
DATETIME	O
data	O
type	O
of	O
the	O
RDBMS	B-Application
used	O
.	O
</s>
<s>
It	O
is	O
not	O
a	O
commercial	O
product	O
,	O
it	O
works	O
with	O
available	O
features	O
in	O
common	O
RDBMS	B-Application
.	O
</s>
<s>
It	O
is	O
not	O
required	O
to	O
have	O
good	O
knowledge	O
about	O
the	O
tables	B-Application
of	O
the	O
database	O
,	O
or	O
the	O
data	O
model	O
.	O
</s>
<s>
Changes	O
in	O
the	O
current	O
tables	B-Application
are	O
not	O
required	O
,	O
because	O
log	O
data	O
of	O
any	O
table	B-Application
is	O
stored	O
in	O
a	O
different	O
one	O
.	O
</s>
<s>
Only	O
changes	O
(	O
INSERT	B-Language
,	O
UPDATE	B-Language
and	O
DELETE	B-Language
operations	O
)	O
are	O
registered	O
,	O
so	O
the	O
growing	O
rate	O
of	O
the	O
history	O
tables	B-Application
are	O
proportional	O
to	O
the	O
changes	O
.	O
</s>
<s>
It	O
is	O
not	O
necessary	O
to	O
apply	O
the	O
trigger	O
to	O
all	O
the	O
tables	B-Application
on	O
database	O
,	O
it	O
can	O
be	O
applied	O
to	O
certain	O
tables	B-Application
,	O
or	O
certain	O
columns	B-Application
of	O
a	O
table	B-Application
.	O
</s>
<s>
It	O
should	O
return	O
the	O
same	O
resultset	O
of	O
the	O
whole	O
original	O
table	B-Application
.	O
</s>
<s>
Suppose	O
the	O
@DATE	O
variable	O
contains	O
the	O
point	O
or	O
time	O
of	O
interest	O
,	O
and	O
the	O
@KEY	O
variable	O
contains	O
the	O
primary	B-Application
key	I-Application
of	O
the	O
entity	O
of	O
interest	O
.	O
</s>
<s>
Suppose	O
the	O
@KEY	O
variable	O
contains	O
the	O
primary	B-Application
key	I-Application
of	O
the	O
entity	O
of	O
interest	O
.	O
</s>
<s>
Suppose	O
the	O
@KEY	O
variable	O
contains	O
the	O
primary	B-Application
key	I-Application
of	O
the	O
entity	O
of	O
interest	O
.	O
</s>
<s>
Since	O
the	O
trigger	O
requires	O
that	O
primary	B-Application
key	I-Application
being	O
the	O
same	O
throughout	O
time	O
,	O
it	O
is	O
desirable	O
to	O
either	O
ensure	O
or	O
maximize	O
its	O
immutability	O
,	O
if	O
a	O
primary	B-Application
key	I-Application
changed	O
its	O
value	O
,	O
the	O
entity	O
it	O
represents	O
would	O
break	O
its	O
own	O
history	O
.	O
</s>
<s>
There	O
are	O
several	O
options	O
to	O
achieve	O
or	O
maximize	O
the	O
primary	B-Application
key	I-Application
immutability	O
:	O
</s>
<s>
Use	O
of	O
a	O
surrogate	B-Application
key	I-Application
as	O
a	O
primary	B-Application
key	I-Application
.	O
</s>
<s>
Use	O
of	O
an	O
immutable	O
natural	B-Application
key	I-Application
as	O
a	O
primary	B-Application
key	I-Application
.	O
</s>
<s>
In	O
a	O
good	O
database	O
design	O
,	O
a	O
natural	B-Application
key	I-Application
which	O
can	O
change	O
should	O
not	O
be	O
considered	O
as	O
a	O
"	O
real	O
"	O
primary	B-Application
key	I-Application
.	O
</s>
<s>
Use	O
of	O
a	O
mutable	O
natural	B-Application
key	I-Application
as	O
a	O
primary	B-Application
key	I-Application
(	O
it	O
is	O
widely	O
discouraged	O
)	O
where	O
changes	O
are	O
propagated	O
in	O
every	O
place	O
where	O
it	O
is	O
a	O
foreign	B-Application
key	I-Application
.	O
</s>
<s>
In	O
such	O
a	O
case	O
,	O
the	O
history	O
table	B-Application
should	O
be	O
also	O
affected	O
.	O
</s>
<s>
Sometimes	O
the	O
Slowly	B-Application
changing	I-Application
dimension	I-Application
is	O
used	O
as	O
a	O
method	O
,	O
this	O
diagram	O
is	O
an	O
example	O
:	O
</s>
