Chapter 25. Using Triggers

In this chapter, you’ll learn what triggers are, why they are used, and how. You’ll also look at the syntax for creating and using them.

Understanding Triggers

Note

Requires MySQL 5Support for triggers was added to MySQL 5. As such, this chapter is applicable to MySQL 5 or later only.

MySQL statements are executed when needed, as are stored procedures. But what if you wanted 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, ...

Get MySQL® Crash Course now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.