SQL Server Triggers Interview Preparation Guide
Download PDF

SQL Server Triggers frequently Asked Questions by expert members with experience in MS SQL Server triggers. These interview questions and answers on SQL Server Triggers will help you strengthen your technical skills, prepare for the interviews and quickly revise the concepts. So get preparation for the SQL Server Triggers job interview

10 MS SQL Server Triggers Questions and Answers:

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

2 :: How many triggers you can have on a table?

A table can have up to 12 triggers defined on it.

3 :: 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.

4 :: 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.

5 :: 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

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 ]

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.

8 :: 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.

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 }

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.