82. What Is A Trigger? What Are The Types Of Trigger?
Triggers are database object. Basically these are special type of stored procedure that is automatically fired / executed when a DDL or DML command statement related with the trigger is executed. They can be executed automatically on the insert, delete and update operation.
Trigger allows us to execute a batch of SQL code when an insert, update or delete command is executed against a specific table.
Triggers are generally used to implement business rules, auditing. Triggers can also be used to extend the referential integrity checks, but wherever possible, use constraints for this purpose, instead of triggers, as constraints are much faster.
In SQL Server we can create four types of triggers
- Data Definition Language (DDL) triggers.
- Data Manipulation Language (DML) triggers.
- After Trigger (using FOR/AFTER CLAUSE)
- Instead of Trigger (using INSTEAD OF CLAUSE)
- Common Language Runtime (CLR) triggers.
- Logon triggers.
83. What Are Data Definition Language (DDL) Triggers?
DDL trigger can be used to observe and control actions performed on the server, and to audit these operations. DDL triggers can be used to manage administrator tasks such as auditing and regulating database operations.
In SQL Server one can create triggers on DDL statements (like CREATE, ALTER, and DROP) and certain system defined stored procedures that perform DDL like operations.
For Example, If you are going to execute the CREATE LOGIN statement or the sp_addlogin stored procedure to create login user, then both these can execute/fire a DDL trigger that you can create on CREATE_LOGIN event of Sql Server.
84. What Are Data Manipulation Language (DML) Triggers?
In SQL Server one can create triggers on DML statements (like INSERT, UPDATE, and DELETE) and stored procedures that perform DML-like operations.
DML Triggers are of two types:
After Trigger (using FOR/AFTER CLAUSE): This type of trigger fires after SQL Server finishes the execution of the action successfully that fired it.
For Example, If you insert record/row in a table then the trigger related/associated with the insert event on this table will fire only after the row passes all the constraints, like as primary key constraint, and some rules. If the record/row insertion fails, SQL Server will not fire the After Trigger.
Instead of Trigger (using INSTEAD OF CLAUSE): This type of trigger fires before SQL Server starts the execution of the action that fired it. This is different from the AFTER trigger, which fires after the action that caused it to fire.
For Example, If you insert record/row in a table then the trigger related/associated with the insert event on this table will fire before the row passes all the constraints, such as primary key constraint and some rules. If the record/row insertion fails, SQL Server will fire the Instead of Trigger.
85. What Are Common Language Runtime (CLR) Triggers?
CLR triggers are special type of triggers that based on the CLR (Common Language Runtime) in .net framework. CLR integration of triggers has been introduced with SQL Server 2008 and allows for triggers to be coded in one of .NET languages like C#, Visual Basic and F#.
86. What Are Logon Triggers?
Logon triggers are special type of trigger that fire when LOGON event of Sql Server is raised. This event is raised when a user session is being established with Sql Server that is made after the authentication phase finishes, but before the user session is actually established. Hence, all messages that we define in the trigger such as error messages, will be redirected to the SQL Server error log. Logon triggers do not fire if authentication fails.
Logon Triggers can be used to audit and control server sessions, such as to track login activity or limit the number of sessions for a specific login.
Comments
Post a Comment
Please share your opinions and suggestions or your experience in the comments section. This way we can all help each other...
Experienced guys can share their resumes at admin@interview-made-easy.com