What are the types of Triggers in the SQL server?
What are the types of Triggers in the SQL server?
24022-Mar-2023
Updated on 22-Mar-2023
Home / DeveloperSection / Forums / What are the types of Triggers in the SQL server?
What are the types of Triggers in the SQL server?
Krishnapriya Rajeev
27-Mar-2023TRIGGER statements in SQL servers can be classified into 3 main types:
These triggers are fired in response to events caused by DDL statements like CREATE, ALTER, and DROP. These can be created at either the database level or server level, and can also be triggered by system-defined stored procedures that perform operations similar to DDL statements.
These triggers are useful:
Example:
2. Data Manipulation Language (DML) Trigger
These triggers are fired in response to events caused by DML statements like INSERT, UPDATE, and DELETE. They can also be triggered by system-defined stored procedures that perform operations similar to DML statements.
There are two types of DML triggers:
It is fired when the SQL server action that triggered it is completed. It is usually executed when a table completes an INSERT, UPDATE, or DELETE operation.
Example:
b. INSTEAD OF Trigger
It is fired before the SQL server begins executing the action that triggers it. Hence, the trigger will fire even if the constraint check fails.
Example:
3. LOGON Trigger
These triggers are fired in response to LOGON events, which occur during the creation of a user session with an SQL server instance following the authentication process of logging in but before the user session is actually established. This results in the SQL server error log displaying all the messages created by the trigger, including error messages and PRINT statement messages.
These triggers do not fire if the authentication fails.
Eden Wheeler
22-Mar-2023Hello there!
It's great to see your interest in SQL Server and triggers. AS an expert of this field, I can help you understand the types of triggers in SQL Server.
In SQL Server, there are two types of triggers: DML (Data Manipulation Language) triggers and DDL (Data Definition Language) triggers. DML triggers fire in response to DML events, such as INSERT, UPDATE, or DELETE statements, while DDL triggers fire in response to DDL events, such as CREATE, ALTER, or DROP statements.
Let me share a personal story about triggers. A few years ago, I was working on a project where we needed to track changes made to a database table. We decided to use a DML trigger to capture the changes. We created an AFTER INSERT, UPDATE, and DELETE trigger on the table, and every time a row was inserted, updated, or deleted, the trigger fired and wrote the details of the change to an audit table. This allowed us to easily track changes made to the table and investigate any issues that arose.
In conclusion, triggers are a powerful feature of SQL Server that can help you automate tasks and track changes to your database. Understanding the different types of triggers and how they work can be very useful in developing efficient and effective database solutions.
I hope this will help you.