<s>
A	O
database	B-Language
trigger	I-Language
is	O
procedural	B-Application
code	I-Application
that	O
is	O
automatically	O
executed	O
in	O
response	O
to	O
certain	O
events	O
on	O
a	O
particular	O
table	B-Application
or	O
view	B-Language
in	O
a	O
database	O
.	O
</s>
<s>
For	O
example	O
,	O
when	O
a	O
new	O
record	O
(	O
representing	O
a	O
new	O
worker	O
)	O
is	O
added	O
to	O
the	O
employees	O
table	B-Application
,	O
new	O
records	O
should	O
also	O
be	O
created	O
in	O
the	O
tables	O
of	O
the	O
taxes	O
,	O
vacations	O
and	O
salaries	O
.	O
</s>
<s>
Below	O
follows	O
a	O
series	O
of	O
descriptions	O
of	O
how	O
some	O
popular	O
DBMS	B-General_Concept
support	O
triggers	O
.	O
</s>
<s>
In	O
addition	O
to	O
triggers	O
that	O
fire	O
(	O
and	O
execute	O
PL/SQL	B-Language
code	O
)	O
when	O
data	O
is	O
modified	O
,	O
Oracle	B-General_Concept
10g	I-General_Concept
supports	O
triggers	O
that	O
fire	O
when	O
schema-level	O
objects	O
(	O
that	O
is	O
,	O
tables	O
)	O
are	O
modified	O
and	O
when	O
user	O
logon	O
or	O
logoff	O
events	O
occur	O
.	O
</s>
<s>
For	O
each	O
statement	O
type	O
:	O
This	O
trigger	O
gets	O
executed	O
only	O
once	O
for	O
the	O
entire	O
result	B-Application
set	I-Application
,	O
but	O
also	O
fires	O
each	O
time	O
the	O
statement	O
is	O
executed	O
.	O
</s>
<s>
From	O
Oracle	B-General_Concept
8i	I-General_Concept
,	O
database	O
events	O
-	O
logons	O
,	O
logoffs	O
,	O
startups	O
-	O
can	O
fire	O
Oracle	B-Application
triggers	O
.	O
</s>
<s>
The	O
following	O
functionality	O
in	O
SQL:2003	B-Language
was	O
previously	O
not	O
implemented	O
in	O
PostgreSQL	O
:	O
</s>
<s>
The	O
standard	O
allows	O
the	O
execution	O
of	O
a	O
number	O
of	O
SQL	O
statements	O
other	O
than	O
SELECT	B-Language
,	O
INSERT	B-Language
,	O
UPDATE	B-Language
,	O
such	O
as	O
CREATE	O
TABLE	B-Application
as	O
the	O
triggered	O
action	O
.	O
</s>
<s>
This	O
can	O
be	O
done	O
through	O
creating	O
a	O
stored	O
procedure	O
or	O
function	O
to	O
call	O
CREATE	O
TABLE	B-Application
.	O
</s>
<s>
Firebird	B-Application
supports	O
multiple	O
row-level	O
,	O
BEFORE	O
or	O
AFTER	O
,	O
INSERT	B-Language
,	O
UPDATE	B-Language
,	O
DELETE	B-Language
(	O
or	O
any	O
combination	O
of	O
thereof	O
)	O
triggers	O
per	O
table	B-Application
,	O
where	O
they	O
are	O
always	O
"	O
in	O
addition	O
to	O
"	O
the	O
default	O
table	B-Application
changes	O
,	O
and	O
the	O
order	O
of	O
the	O
triggers	O
relative	O
to	O
each	O
other	O
can	O
be	O
specified	O
where	O
it	O
would	O
otherwise	O
be	O
ambiguous	O
(	O
POSITION	O
clause	O
.	O
)	O
</s>
<s>
Triggers	O
may	O
also	O
exist	O
on	O
views	O
,	O
where	O
they	O
are	O
always	O
"	O
instead	O
of	O
"	O
triggers	O
,	O
replacing	O
the	O
default	O
updatable	O
view	B-Language
logic	O
.	O
</s>
<s>
Firebird	B-Application
does	O
not	O
raise	O
mutating	O
table	B-Application
exceptions	O
(	O
like	O
Oracle	B-Application
)	O
,	O
and	O
triggers	O
will	O
by	O
default	O
both	O
nest	O
and	O
recurse	O
as	O
required	O
(	O
SQL	O
Server	O
allows	O
nesting	O
but	O
not	O
recursion	O
,	O
by	O
default	O
.	O
)	O
</s>
<s>
Firebird	B-Application
's	O
triggers	O
use	O
NEW	O
and	O
OLD	O
context	O
variables	O
(	O
not	O
Inserted	O
and	O
Deleted	O
tables	O
,	O
)	O
and	O
provide	O
UPDATING	O
,	O
INSERTING	O
,	O
and	O
DELETING	O
flags	O
to	O
indicate	O
the	O
current	O
usage	O
of	O
the	O
trigger	O
.	O
</s>
<s>
As	O
of	O
version	O
2.1	O
,	O
Firebird	B-Application
additionally	O
supports	O
the	O
following	O
database-level	O
triggers	O
:	O
</s>
<s>
Database-level	O
triggers	O
can	O
help	O
enforce	O
multi-table	O
constraints	O
,	O
or	O
emulate	O
materialized	B-Application
views	I-Application
.	O
</s>
<s>
Syntax	O
for	O
database	B-Language
triggers	I-Language
:	O
</s>
<s>
Limited	O
support	O
for	O
triggers	O
in	O
the	O
MySQL/MariaDB	O
DBMS	B-General_Concept
was	O
added	O
in	O
the	O
5.0	O
version	O
of	O
MySQL	O
,	O
launched	O
in	O
2005	O
.	O
</s>
<s>
If	O
there	O
are	O
more	O
triggers	O
for	O
same	O
operation	O
on	O
table	B-Application
then	O
firing	O
order	O
is	O
determined	O
by	O
trigger	O
creation	O
data	O
.	O
</s>
<s>
After	O
triggers	O
can	O
write	O
data	O
into	O
tables	O
and	O
unlike	O
some	O
other	O
databases	O
you	O
can	O
write	O
into	O
any	O
table	B-Application
including	O
table	B-Application
on	O
which	O
trigger	O
operates	O
.	O
</s>
<s>
Triggers	O
are	O
usually	O
programmed	O
in	O
SQL	B-Application
PL	I-Application
language	O
.	O
</s>
<s>
SQLite	B-Language
only	O
supports	O
row-level	O
triggers	O
,	O
not	O
statement-level	O
triggers	O
.	O
</s>
<s>
Updateable	O
views	O
,	O
which	O
are	O
not	O
supported	O
in	O
SQLite	B-Language
,	O
can	O
be	O
emulated	O
with	O
INSTEAD	O
OF	O
triggers	O
.	O
</s>
<s>
An	O
example	O
of	O
implementation	O
of	O
triggers	O
in	O
non-relational	O
database	O
can	O
be	O
Sedna	B-Application
,	O
that	O
provides	O
support	O
for	O
triggers	O
based	O
on	O
XQuery	B-Language
.	O
</s>
<s>
Triggers	O
in	O
Sedna	B-Application
were	O
designed	O
to	O
be	O
analogous	O
to	O
SQL:2003	B-Language
triggers	O
,	O
but	O
natively	O
base	O
on	O
XML	O
query	O
and	O
update	B-Language
languages	O
(	O
XPath	B-Language
,	O
XQuery	B-Language
and	O
XML	O
update	B-Language
language	O
)	O
.	O
</s>
<s>
A	O
trigger	O
in	O
Sedna	B-Application
is	O
set	O
on	O
any	O
nodes	O
of	O
an	O
XML	O
document	O
stored	O
in	O
database	O
.	O
</s>
<s>
When	O
these	O
nodes	O
are	O
updated	O
,	O
the	O
trigger	O
automatically	O
executes	O
XQuery	B-Language
queries	O
and	O
updates	O
specified	O
in	O
its	O
body	O
.	O
</s>
<s>
Suppose	O
you	O
have	O
a	O
trigger	O
that	O
is	O
made	O
to	O
be	O
called	O
on	O
an	O
UPDATE	B-Language
to	O
a	O
certain	O
table	B-Application
.	O
</s>
<s>
Row	O
level	O
triggers	O
would	O
execute	O
once	O
for	O
each	O
row	O
that	O
is	O
affected	O
by	O
the	O
UPDATE	B-Language
.	O
</s>
<s>
It	O
is	O
important	O
to	O
keep	O
in	O
mind	O
if	O
no	O
rows	O
are	O
affected	O
by	O
the	O
UPDATE	B-Language
command	O
,	O
the	O
trigger	O
will	O
not	O
execute	O
any	O
code	O
within	O
the	O
trigger	O
.	O
</s>
<s>
Statement	O
level	O
triggers	O
will	O
be	O
called	O
once	O
regardless	O
of	O
how	O
many	O
rows	O
are	O
affected	O
by	O
the	O
UPDATE	B-Language
.	O
</s>
<s>
Here	O
it	O
is	O
important	O
to	O
note	O
that	O
even	O
if	O
the	O
UPDATE	B-Language
command	O
did	O
n't	O
affect	O
any	O
rows	O
,	O
the	O
code	O
within	O
the	O
trigger	O
will	O
still	O
be	O
executed	O
once	O
.	O
</s>
<s>
Suppose	O
you	O
have	O
a	O
trigger	O
that	O
is	O
called	O
on	O
an	O
INSERT	B-Language
to	O
a	O
certain	O
table	B-Application
.	O
</s>
<s>
If	O
your	O
trigger	O
is	O
using	O
the	O
BEFORE	O
option	O
,	O
the	O
code	O
within	O
the	O
trigger	O
will	O
be	O
executed	O
before	O
the	O
INSERT	B-Language
into	I-Language
the	O
table	B-Application
occurs	O
.	O
</s>
<s>
A	O
common	O
use	O
of	O
the	O
BEFORE	O
trigger	O
is	O
to	O
verify	O
the	O
input	O
values	O
of	O
the	O
INSERT	B-Language
,	O
or	O
modify	O
the	O
values	O
accordingly	O
.	O
</s>
<s>
The	O
code	O
within	O
the	O
trigger	O
is	O
executed	O
after	O
the	O
INSERT	B-Language
happens	O
to	O
the	O
table	B-Application
.	O
</s>
<s>
An	O
example	O
use	O
of	O
this	O
trigger	O
is	O
creating	O
an	O
audit	O
history	O
of	O
who	O
has	O
made	O
inserts	B-Language
into	O
the	O
database	O
,	O
keeping	O
track	O
of	O
the	O
changes	O
made	O
.	O
</s>
<s>
Using	O
AFTER	O
triggers	O
allows	O
you	O
to	O
modify	O
tables	O
such	O
as	O
inserting	O
into	O
an	O
audit	O
history	O
table	B-Application
.	O
</s>
<s>
Be	O
cautious	O
of	O
using	O
additional	O
INSERT/UPDATE/DELETE	O
commands	O
within	O
your	O
trigger	O
,	O
because	O
trigger	O
recursion	O
is	O
possible	O
,	O
causing	O
unwanted	O
behavior	O
.	O
</s>
<s>
In	O
the	O
examples	O
below	O
each	O
trigger	O
is	O
modifying	O
a	O
different	O
table	B-Application
,	O
by	O
looking	O
at	O
what	O
is	O
being	O
modified	O
you	O
can	O
see	O
some	O
common	O
applications	O
of	O
when	O
different	O
trigger	O
types	O
are	O
used	O
.	O
</s>
<s>
The	O
following	O
is	O
an	O
Oracle	B-Application
syntax	O
example	O
of	O
a	O
row	O
level	O
trigger	O
that	O
is	O
called	O
AFTER	O
an	O
update	B-Language
FOR	O
EACH	O
ROW	O
affected	O
.	O
</s>
<s>
This	O
trigger	O
is	O
called	O
on	O
an	O
update	B-Language
to	O
a	O
phone	O
book	O
database	O
.	O
</s>
<s>
When	O
the	O
trigger	O
is	O
called	O
it	O
adds	O
an	O
entry	O
into	O
a	O
separate	O
table	B-Application
named	O
phone_book_audit	O
.	O
</s>
<s>
Also	O
take	O
note	O
of	O
triggers	O
being	O
able	O
to	O
take	O
advantage	O
of	O
schema	O
objects	O
like	O
sequences	O
,	O
in	O
this	O
example	O
audit_id_sequence.nexVal	O
is	O
used	O
to	O
generate	O
unique	O
primary	B-Application
keys	I-Application
in	O
the	O
phone_book_audit	O
table	B-Application
.	O
</s>
<s>
Now	O
calling	O
an	O
UPDATE	B-Language
on	O
the	O
phone_book	O
table	B-Application
for	O
people	O
with	O
the	O
last	O
name	O
'	O
Jones	O
 '	O
.	O
</s>
<s>
Notice	O
that	O
the	O
phone_number_audit	O
table	B-Application
is	O
now	O
populated	O
with	O
two	O
entries	O
.	O
</s>
<s>
Since	O
the	O
update	B-Language
modified	O
two	O
separate	O
row	O
values	O
,	O
the	O
created	O
trigger	O
was	O
called	O
twice	O
;	O
once	O
after	O
each	O
modification	O
.	O
</s>
<s>
An	O
Oracle	B-Application
syntax	O
statement	O
trigger	O
that	O
is	O
called	O
after	O
an	O
UPDATE	B-Language
to	O
the	O
phone_book	O
table	B-Application
.	O
</s>
<s>
Now	O
doing	O
exactly	O
the	O
same	O
update	B-Language
as	O
the	O
above	O
example	O
,	O
however	O
this	O
time	O
with	O
a	O
statement	O
level	O
trigger	O
.	O
</s>
<s>
The	O
result	O
shows	O
that	O
the	O
trigger	O
was	O
only	O
called	O
once	O
,	O
even	O
though	O
the	O
update	B-Language
did	O
change	O
two	O
rows	O
.	O
</s>
<s>
This	O
example	O
demonstrates	O
a	O
BEFORE	O
EACH	O
ROW	O
trigger	O
that	O
modifies	O
the	O
INSERT	B-Language
using	O
a	O
WHEN	O
conditional	O
.	O
</s>
<s>
Now	O
performing	O
an	O
INSERT	B-Language
of	O
someone	O
with	O
a	O
large	O
name	O
.	O
</s>
<s>
The	O
trigger	O
worked	O
as	O
per	O
the	O
result	O
above	O
,	O
modifying	O
the	O
value	O
of	O
the	O
INSERT	B-Language
before	O
it	O
was	O
executed	O
.	O
</s>
<s>
This	O
example	O
demonstrate	O
how	O
to	O
enforce	O
a	O
restriction	O
upon	O
someone	O
named	O
"	O
SOMEUSER	O
"	O
on	O
the	O
table	B-Application
phone_book	O
.	O
</s>
<s>
Now	O
,	O
when	O
"	O
SOMEUSER	O
"	O
is	O
logged	O
in	O
after	O
attempting	O
any	O
INSERT	B-Language
this	O
error	O
message	O
will	O
show	O
:	O
</s>
