WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

SQL Server · Intermediate · question 23 of 100

What is a SQL Server trigger, and what are the different types of triggers?

📕 Buy this interview preparation book: 100 SQL Server questions & answers — PDF + EPUB for $5

In SQL Server, a trigger is a special type of stored procedure that is automatically executed in response to certain events or actions performed on a database table or view. A trigger typically consists of a set of SQL statements that are executed when a specific event occurs.

There are two main types of triggers in SQL Server:

1. DML Triggers: These triggers are fired in response to DML (data manipulation language) events such as INSERT, UPDATE, and DELETE statements executed on a table or view. DML triggers can be further classified into two subtypes:

* ‘After triggers‘: These triggers are executed after the DML event occurs, and they can be used to validate, modify, or log the changes made to the table.

For example, suppose we have a table called ‘Orders‘ with columns ‘OrderID‘, ‘CustomerID‘, ‘OrderDate‘, and ‘Amount‘. We can create an After trigger that automatically updates the ‘Amount‘ column whenever an ‘INSERT‘ or ‘UPDATE‘ statement is executed on the ‘Orders‘ table, as shown below:

    CREATE TRIGGER UpdateAmount
    ON Orders
    AFTER INSERT, UPDATE
    AS
    BEGIN
       UPDATE Orders
       SET Amount = Quantity * UnitPrice
       FROM inserted
       WHERE Orders.OrderID = inserted.OrderID
    END

* ‘Instead of triggers‘: These triggers are executed instead of the actual DML event. They are commonly used to implement complex business logic, such as enforcing complex security policies, view restrictions, or other restrictions on certain tables.

For example, suppose we have a table called ‘Employees‘ with columns ‘EmployeeID‘, ‘FirstName‘, ‘LastName‘, and ‘Salary‘. We can create an Instead of trigger that prevents any ‘DELETE‘ statements from being executed on the ‘Employees‘ table for employees whose ‘Salary‘ is greater than a certain threshold, as shown below:

    CREATE TRIGGER PreventDelete
    ON Employees
    INSTEAD OF DELETE
    AS
    BEGIN
       DELETE Employees
       FROM Employees INNER JOIN deleted
       ON Employees.EmployeeID = deleted.EmployeeID
       WHERE Salary > 50000
    END

2. DDL Triggers: These triggers are fired in response to DDL (data definition language) events such as ‘CREATE‘, ‘ALTER‘, and ‘DROP‘ statements executed on a database or server. DDL triggers are mainly used to enforce certain policies or to audit changes made to the database schema.

For example, suppose we have a database called ‘SalesDB‘ with several tables and views. We can create a DDL trigger that logs any changes made to the database schema, as shown below:

    CREATE TRIGGER LogSchemaChanges
    ON DATABASE
    FOR DDL_DATABASE_LEVEL_EVENTS
    AS
    BEGIN
        DECLARE @EventData XML
        SET @EventData = EVENTDATA()

        INSERT INTO SchemaChangesLog (EventDate, UserName, ObjectType, ObjectName, TSQLCommand)
        VALUES (GETDATE(), ORIGINAL_LOGIN(), @EventData.value('(/EVENT_INSTANCE/ObjectType)[1]', 'nvarchar(255)'),
        @EventData.value('(/EVENT_INSTANCE/ObjectName)[1]', 'nvarchar(255)'), @EventData.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'nvarchar(max)'))
    END

In summary, SQL Server triggers are a powerful feature that can be used to implement complex business logic, perform auditing, or enforce various policies on database tables and views. The different types of triggers provide a rich set of functionality to handle different database events and to address different use cases.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic SQL Server interview — then scores it.
📞 Practice SQL Server — free 15 min
📕 Buy this interview preparation book: 100 SQL Server questions & answers — PDF + EPUB for $5

All 100 SQL Server questions · All topics