What is a trigger, and what is its purpose?
What is a stored procedure and why is it particularly useful?
Provide detailed examples
A trigger is a special type of stored procedure that is automatically executed when a specified event occurs in a database. For example, a trigger can be used to automatically update a table when a row is inserted or deleted. Triggers are often used to enforce referential integrity or to maintain audit trails.
The purpose of a trigger is to automate a task that would otherwise have to be performed manually. For example, a trigger can be used to automatically send an email notification when a new order is placed. Triggers can also be used to prevent errors or to enforce security policies.
A stored procedure is a collection of SQL statements that are stored in the database. Stored procedures are executed as a single unit, which can improve performance and security. Stored procedures are also useful for encapsulating complex logic, such as business rules or calculations.
Stored procedures are particularly useful for the following reasons:
Here are some examples of triggers and stored procedures: