SQL Server Triggers Interview Questions And Answers
Download MS SQL Server Triggers Interview Questions and Answers PDF
Prepare comprehensively for your MS SQL Server Triggers interview with our extensive list of 10 questions. These questions will test your expertise and readiness for any MS SQL Server Triggers interview scenario. Ideal for candidates of all levels, this collection is a must-have for your study plan. Download the free PDF now to get all 10 questions and ensure you're well-prepared for your MS SQL Server Triggers interview. This resource is perfect for in-depth preparation and boosting your confidence.
10 MS SQL Server Triggers Questions and Answers:
MS SQL Server Triggers Job Interview Questions Table of Contents:
1 :: What is MS SQL Server Triggers?
A trigger is a special type of event driven stored procedure. It gets initiated when Insert, Delete or Update event occurs. It can be used to maintain referential integrity. A trigger can call stored procedure.
Executed automatically when an INSERT, UPDATE or DELETE operation takes place on a table.
You can specify which trigger fires first or fires last using sp_settriggerorder.
Triggers can't be invoked on demand.
They get triggered only when an associated action (INSERT, UPDATE, DELETE) happens.
Triggers are generally used to implement business rules, auditing.
Triggers can also be used to extend the referential integrity checks
Read MoreExecuted automatically when an INSERT, UPDATE or DELETE operation takes place on a table.
You can specify which trigger fires first or fires last using sp_settriggerorder.
Triggers can't be invoked on demand.
They get triggered only when an associated action (INSERT, UPDATE, DELETE) happens.
Triggers are generally used to implement business rules, auditing.
Triggers can also be used to extend the referential integrity checks
2 :: How many triggers you can have on a table?
A table can have up to 12 triggers defined on it.
Read More3 :: How to invoke a trigger on demand?
Triggers can't be invoked on demand. They get triggered when the associated INSERT, DELETE or UPDATE is performed.
Read More4 :: Explain triggers?
Triggers are constructs in PL/SQL that need to be just created and associated with a table. Once they are created, when the table associated with it gets updated due to an UPDATE, INSERT or a DELETE, the triggers get implicitly fired depending upon the instructions passed to them.
Read More5 :: Describe triggers features and limitations?
Trigger features:-
1. Can execute a batch of SQL code for an insert, update or delete command is executed
2. Business rules can be enforced on modification of data
Trigger Limitations:-
1. Does not accept arguments or parameters
2. Cannot perform commit or rollback
3. Can cause table errors if poorly written
Read More1. Can execute a batch of SQL code for an insert, update or delete command is executed
2. Business rules can be enforced on modification of data
Trigger Limitations:-
1. Does not accept arguments or parameters
2. Cannot perform commit or rollback
3. Can cause table errors if poorly written
6 :: Explain Syntax for viewing, dropping and disabling triggers?
View trigger:
A trigger can be viewed by using sp_helptrigger syntax. This returns all the triggers defined in a table.
Sp_helptrigger table_name
Drop a trigger
Syntax: DROP TRIGGER Trigger_name
Disable a trigger:-
Syntax: DISABLE TRIGGER [schema name] trigger name
ON [object, database or ALL server ]
Read MoreA trigger can be viewed by using sp_helptrigger syntax. This returns all the triggers defined in a table.
Sp_helptrigger table_name
Drop a trigger
Syntax: DROP TRIGGER Trigger_name
Disable a trigger:-
Syntax: DISABLE TRIGGER [schema name] trigger name
ON [object, database or ALL server ]
7 :: Determine how to use the inserted and deleted pseudo tables?
Inserted and deleted pseudo tables contain the New and Old values of the data that initiating the Trigger. These tables can be used for database maintenance and dynamic modification to data. These tables can be examined by the trigger itself. The tables themselves cannot be altered.
Read More8 :: How to apply cascading referential integrity in place of triggers?
Cascading referential integrity constraints are automatic updates and deletes on dependant objects. They define a set of actions that SQL server may need to take. The References clause of the CREATE TABLE and ALTER TABLE statements support ON DELETE and ON UPDATE clauses:
[ON DELETE {NO ACTION} ]: If an attempt to delete a row is made, with a key referenced by foreign keys in existing rows in other tables, an error is raised and DELETE is rolled back.
[ON UPDATE {NO ACTION } ]: If an attempt to update a row is made, with a key referenced by foreign keys in existing rows in other tables, an error is raised and UPDATE is rolled back.
[ ON DELETE { CASCADE } ]: If an attempt to delete a row is made, with a key referenced by foreign keys in existing rows in other tables, all rows containing those foreign keys are also deleted
[ ON UPDATE { CASCADE } ]: If an attempt to update a row is made, with a key referenced by foreign keys in existing rows in other tables, all rows containing those foreign keys are also Updated.
NO ACTION is default.
Read More[ON DELETE {NO ACTION} ]: If an attempt to delete a row is made, with a key referenced by foreign keys in existing rows in other tables, an error is raised and DELETE is rolled back.
[ON UPDATE {NO ACTION } ]: If an attempt to update a row is made, with a key referenced by foreign keys in existing rows in other tables, an error is raised and UPDATE is rolled back.
[ ON DELETE { CASCADE } ]: If an attempt to delete a row is made, with a key referenced by foreign keys in existing rows in other tables, all rows containing those foreign keys are also deleted
[ ON UPDATE { CASCADE } ]: If an attempt to update a row is made, with a key referenced by foreign keys in existing rows in other tables, all rows containing those foreign keys are also Updated.
NO ACTION is default.
9 :: Explain trigger classes i.e. instead of and after trigger?
INSTEAD OF: Cause the trigger to fire instead of executing the triggering event or action. It prevents unnecessary changes to be made.
Example: Causes the trigger to fire instead of the update (action)
CREATE TRIGGER Employee_update ON Employee INSTEAD OF UPDATE AS { TRIGGER Definition }
AFTER: execute following the triggering action, such as an insert, update, or delete. These triggers are fired a little late in the process.
Example: Causes the trigger to fire instead of the update (action)
CREATE TRIGGER Employee_update ON Employee AFTER UPDATE AS { TRIGGER Definition }
Read MoreExample: Causes the trigger to fire instead of the update (action)
CREATE TRIGGER Employee_update ON Employee INSTEAD OF UPDATE AS { TRIGGER Definition }
AFTER: execute following the triggering action, such as an insert, update, or delete. These triggers are fired a little late in the process.
Example: Causes the trigger to fire instead of the update (action)
CREATE TRIGGER Employee_update ON Employee AFTER UPDATE AS { TRIGGER Definition }
10 :: What are the instances when triggers are appropriate?
When security is the top most priority. i.e. to allow unauthorized access
When backups are essential
When Maintenance is desired. Triggers can be fired when any error message is logged
Keeping the database consistent.
Read MoreWhen backups are essential
When Maintenance is desired. Triggers can be fired when any error message is logged
Keeping the database consistent.