Database Reference
In-Depth Information
25
In this chapter, you learn what triggers are, why they are used, and how. You also
look at the syntax for creating and using them.
MariaDB statements are executed when needed, as are stored procedures. But
what if you want a statement (or statements) to be executed automatically
when events occur? For example:
■
Every time a customer is added to a database table, check that the
phone number is formatted correctly and that the state abbreviation is
in uppercase.
■
Every time a product is ordered, subtract the ordered quantity from
the number in stock.
■
Whenever a row is deleted, save a copy in an archive table.
All these examples need to be processed automatically whenever a table change
occurs. And that is exactly what triggers are. A
trigger
is a MariaDB statement
(or a group of statements enclosed within
BEGIN
and
END
statements) that are
automatically executed by MariaDB in response to any of these statements:
■
DELETE
■
INSERT
■
UPDATE
No other MariaDB SQL statements support triggers.
Note
Only Tables Triggers are supported only on tables, not on views (and not on temporary
tables).