Categories
Sql Server

What are Triggers in Sql server and types of Triggers

Triggers:-

Trigger is a special kind of stored procedure they executes themselves when an INSERT, UPDATE, or DELETE statement modifies the data in table. A trigger can query other tables as well and we can create complex Transact-SQL statements.

We often create triggers to enforce referential integrity or consistency among logically related data in tables.You can use triggers to enforce complex business logic that is difficult to enforce by using other data integrity mechanisms.

  1. The trigger when fires are treated as a single transaction, which can be rolled back from within the trigger. If any error is detected the entire transaction automatically rolls back.
  2. Triggers can cascade changes through related tables in the database.
  3. Triggers guard our data against malicious or incorrect insert, update, and delete operations and enforce other restrictions that are more complex than those defined by using CHECK constraints.
  4. Triggers also reference columns in other tables, unlike CHECK constraints.
  5. Triggers allow custom error messages when constraint violations occur.

Types of Trigger:-

  1. Insert Trigger
  2. Delete Trigger
  3. Update Trigger
  4. Instead Of Trigger
  5. Nested Trigger
  6. Recursive Trigger