Difficulty: Easy
Correct Answer: AFTER triggers apply only to tables; views allow INSTEAD OF triggers
Explanation:
Introduction / Context:
SQL Server provides two main trigger types: AFTER and INSTEAD OF. These are used for enforcing business rules, auditing, or transforming data writes. Knowing which objects support which trigger types prevents compilation errors and inappropriate designs.
Given Data / Assumptions:
Concept / Approach:
AFTER triggers fire after the data modification statement (INSERT, UPDATE, DELETE) completes on a table. Views cannot host AFTER triggers. Instead, SQL Server allows INSTEAD OF triggers on both tables and views, with the common use case being to make inherently non-updatable views behave updatable by redirecting modifications to underlying tables.
Step-by-Step Solution:
Create a view: CREATE VIEW v AS SELECT ... FROM dbo.T;Attempt CREATE TRIGGER trg ON v AFTER INSERT ... → this fails.Create INSTEAD OF trigger: CREATE TRIGGER trg ON v INSTEAD OF INSERT AS INSERT INTO dbo.T ...;On a table, AFTER triggers are valid: CREATE TRIGGER trg ON dbo.T AFTER UPDATE AS ...;
Verification / Alternative check:
Refer to sys.triggers and sys.views; you will find INSTEAD OF triggers associated with views, while AFTER triggers associate with tables.
Why Other Options Are Wrong:
Claiming AFTER triggers work on views contradicts engine rules. BEFORE triggers do not exist in SQL Server. AFTER triggers are not limited to DELETE; they support INSERT and UPDATE as well.
Common Pitfalls:
Trying to enforce view logic using AFTER triggers; assuming cross-platform parity with systems that support BEFORE triggers.
Final Answer:
AFTER triggers apply only to tables; views allow INSTEAD OF triggers
Discussion & Comments