SQL Server Mastery

Triggers: Auditing changes and the dangers of hidden logic

1 Views Updated 5/4/2026

SQL Server Triggers

A Trigger is a special type of stored procedure that automatically runs when an event occurs in the database server. They are most commonly used for auditing changes or enforcing complex business rules that simple constraints can't handle.

1. INSERTED and DELETED Tables

Triggers have access to two virtual tables: Inserted and Deleted. When you update a row, the old value goes to Deleted and the new value goes to Inserted. This is how you can track exactly who changed what and when.

2. The Hidden Logic Danger

Triggers run inside the same transaction as your main query. If a trigger is slow, your INSERT becomes slow. If a trigger fails, your main query fails. Because triggers are "Hidden," developers often spend hours debugging a query only to find that a trigger was causing the problem. Architect Rule: Use triggers sparingly!

CREATE TRIGGER trg_AuditUserChange
ON Users AFTER UPDATE AS
BEGIN
    INSERT INTO AuditLog (OldEmail, NewEmail, ChangedBy)
    SELECT D.Email, I.Email, SUSER_NAME()
    FROM Deleted D JOIN Inserted I ON D.Id = I.Id
END

4. Interview Mastery

Q: "What is the difference between an AFTER trigger and an INSTEAD OF trigger?"

Architect Answer: "An **AFTER** trigger runs after the primary action (Insert/Update/Delete) has succeeded but before the transaction is committed. An **INSTEAD OF** trigger intercepts the call and runs *its* code instead of the original SQL. `INSTEAD OF` triggers are extremely useful for making **Views** updatable—when you try to update a complex View, the trigger catches the call and manually updates the underlying base tables for you."

SQL Server Mastery
1. SQL Server Architecture & Basics
SQL Server Internals: How the Storage Engine works Relational Database Design & Normalization (1NF to 3NF) Data Types Mastery: Choosing the right type for performance
2. Advanced T-SQL Querying
Joins Deep Dive: Inner, Outer, Cross, and Self Joins Subqueries vs CTEs: Writing readable, high-performance code Window Functions: ROW_NUMBER, RANK, and LEAD/LAG Aggregations & Grouping Sets: Building complex reports Set Operators: UNION vs UNION ALL, INTERSECT, and EXCEPT
3. Indexing & Performance Tuning
Clustered vs Non-Clustered Indexes: The physical storage reality Covering Indexes & Included Columns: Reducing I/O costs Index Fragmentation: Why it happens and how to fix it Execution Plans: Reading the Query Optimizer's mind Statistics: Why 'Out of Date' stats kill performance SARGability: Writing queries that actually use indexes
4. Database Programmability
Stored Procedures: Security, Performance, and Best Practices User Defined Functions (UDF): Scalar vs Table-Valued Triggers: Auditing changes and the dangers of hidden logic Views & Indexed Views: Abstraction with performance Error Handling: TRY/CATCH and XACT_STATE()
5. Transactions & Concurrency
Transaction Isolation Levels: Read Uncommitted to Snapshot Locking & Blocking: Analyzing Deadlocks like a Pro Optimistic vs Pessimistic Concurrency
6. Administration & Security
SQL Server Security: Logins, Users, and Roles SQL Injection Prevention: Beyond simple parameterization Backup & Recovery Models: Full vs Simple vs Bulk-Logged Automating Maintenance: SQL Agent Jobs & Rebuilding Indexes
7. Modern SQL & Cloud
SQL Server & JSON: Storing and Querying semi-structured data Temporal Tables: Keeping track of data history automatically Introduction to Azure SQL: Database as a Service (PaaS) SQL Server Developer Interview: Junior to Senior Architect Level