An SQL Trigger is a set of instructions that automatically executes in response to specific events on a database table or view. These events can be INSERT, UPDATE, or DELETE operations.
Key Features:
1. Automatic Execution: Triggers are invoked automatically when the specified event occurs.
2. Event-Driven: They respond to changes in the database, such as data modification.
3. Types:
– BEFORE Trigger: Executes before the event.
– AFTER Trigger: Executes after the event.
Example:
sql
CREATE TRIGGER trigger_name
AFTER INSERT ON table_name
FOR EACH ROW
BEGIN
— Trigger logic here
END;
Use Cases:
– Enforcing business rules.
– Maintaining audit trails.
– Synchronizing tables.
Triggers enhance data integrity and automate processes, reducing manual intervention. However, they should be used carefully to avoid performance issues.