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 or procedures) when certain events occur, such as inserting, updating, or deleting data.
The main purposes of using triggers are:
- Automating data integrity checks.
- Logging changes in tables.
- Implementing complex business logic at the database level.
- Synchronizing data between tables.
For example, when a new record is added to the orders table, a trigger can automatically update the available stock quantity.