Live registration reopens October 1, 2026, in 14d 01h 51mNotify me

Category: Foreign Keys

T-SQL & Development

If You Have Foreign Keys, Don’t Update Fields That Aren’t Changing.

If you update a row without actually changing its contents, does it still hurt?

Paul White wrote in detail about the impact of non-updating updates, proving that SQL Server works hard to avoid doing extra work where it can. That's a great post, and you should read it.

Read more about If You Have Foreign Keys, Don’t Update Fields That Aren’t Changing. 25 comments — Join the discussion
Performance Tuning

Adventures In Foreign Keys 5: How Join Elimination Makes Queries Faster

FINALLY...
This is the last post I'll write about foreign keys for a while. Maybe ever.

Let's face it, most developers probably find them more annoying than useful, and if you didn't implement them when you first started designing your database, you're not likely to go back and start trying to add them in.

Read more about Adventures In Foreign Keys 5: How Join Elimination Makes Queries Faster 14 comments — Join the discussion
Performance Tuning

Adventures In Foreign Keys 4: How to Index Foreign Keys

This week, we're all about foreign keys. So far, we set up the Stack Overflow database to get ready, then tried to set up relationships, and encountered cascading locking issues.
Dawn Of The Data
I don't know how long the recommendation to index your foreign keys has been a thing, but I generally find it useful to abide by, depending a bit on how they're used.

Read more about Adventures In Foreign Keys 4: How to Index Foreign Keys 6 comments — Join the discussion
Performance Tuning

Adventures In Foreign Keys 2: Common Foreign Key Errors

This week, we're all about foreign keys. Yesterday's post covered scripts to set up the Stack Overflow database to get ready, eliminating data that would violate FK relationships.
You Had Two Jobs!
Let's say I wanted to implement Foreign Keys to do two things

If a user deletes their account, all of their badges, comments, and posts will also get deleted
If a user deletes their post, all of the comments and votes will also get deleted

Read more about Adventures In Foreign Keys 2: Common Foreign Key Errors 4 comments — Join the discussion
Performance Tuning

Adventures In Foreign Keys 1: Setting Up Foreign Keys in Stack Overflow

In A Foreign Key, In A Foreign Table Much of what people want from foreign keys, like referential integrity and join elimination, are only as guaranteed as much as SQL Server can trust your constraints (and even then...). The same goes for check constraints, too. Thankfully, things like Primary Keys and Unique Constraints are sort…

Read more about Adventures In Foreign Keys 1: Setting Up Foreign Keys in Stack Overflow 11 comments — Join the discussion

Should You Enforce Foreign Key Relationships in the Database?

It's one of those fierce religious wars that divides families and tears loved ones apart. First, if two tables are related, should we tell SQL Server about it? If SQL Server knows that there's a relationship, then it can make better decisions when building execution plans. The problem is that SQL Server has to be…

Read more about Should You Enforce Foreign Key Relationships in the Database? 21 comments — Join the discussion