📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

How would you enforce a business rule that spans two tables in SQL Server, such as uniqueness across a join?

Intermediate Theory Intermediate

🎯 Key points

  • Indexed view with unique clustered index over an inner join
  • SCHEMABINDING, deterministic expressions and required SET options
  • Write cost and contention on hot view keys
  • Set-based AFTER trigger needs UPDLOCK, HOLDLOCK to be race-safe
  • Application checks need locking and only cover writers that use them
  • Measure insert latency and blocking after adding enforcement

⏱ 30-second answer

Options, in order of preference:

  • Indexed view with SCHEMABINDING and a unique clustered index over an inner join of the two tables. SQL Server enforces it on every write in the same transaction. Limits: inner joins only, deterministic expressions, required SET options, and extra write cost and lock contention on hot keys.
  • Set-based AFTER trigger that checks inserted against the other table and rolls back. Flexible, but hidden logic, extra latency on every statement, and it must use locking hints (UPDLOCK, HOLDLOCK) to be race-safe.
  • Validation in the write path inside a transaction with UPDLOCK, HOLDLOCK (or serializable). Works only if every writer goes through it and reduces concurrency on the checked range.

Prefer the indexed view for pure uniqueness; measure insert latency before and after.

🔍 What the interviewer is checking

Checks knowledge of indexed views, triggers and their trade-offs.

↩ Usually asked after

More in Database Design

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me