Junior — Middle
What is the role of a trigger in a database and why is it used?
sobes.tech AI
Answer from AI
A trigger in a database is a special mechanism that automatically executes a predefined set of actions (such as SQL queries) when a specific event (INSERT, UPDATE, DELETE) occurs on a table.
Role of a trigger:
- Automating data integrity checks (e.g., verifying or modifying values).
- Logging changes in tables.
- Implementing complex business logic at the database level.
Example: When a new record is added to the orders table, a trigger can automatically update the stock quantity.