Learn to automate actions with triggers that respond to data modifications.
A Trigger is a special type of stored procedure in a database that is automatically executed, or 'fired', when a specific event occurs in a table. Triggers are used to maintain the integrity of the information on the database. The events that can fire a trigger are DML (Data Manipulation Language) statements, specifically `INSERT`, `UPDATE`, or `DELETE`. You can define a trigger to run either `BEFORE` or `AFTER` the event. For example, you can create a trigger that runs automatically `BEFORE` any `INSERT` on an 'employees' table to validate the input data, perhaps ensuring that a new employee's salary is within a valid range for their job title. Another common use case is for auditing. You could create an `AFTER DELETE` trigger on a table that inserts a copy of the deleted row into a separate audit table, along with the timestamp and the user who performed the deletion. This creates a historical record of all changes. While triggers are powerful for enforcing complex business rules and automating tasks at the database level, they should be used with care. They operate implicitly, which can make debugging difficult, and poorly written triggers can cause significant performance overhead. They are best used for tasks that must always be enforced, regardless of which application is accessing the database.