Four steps for a check constraint on existing data: find the two negative areas, fail to add the rule too early with Msg 547, fix the values and add the rule, then watch an update to minus 5 get rejected

Check Constraint on Existing Data: Clean First, Then Enforce

A check constraint on existing data fails if even one row already breaks the rule. This post finds bad values in a small table of lakes, cleans them, adds the constraint, tightens it with an upper limit, and writes a rule across two columns. It also shows how NULL and WITH NOCHECK behave. Every result comes from SQL Server 2025.

Read More
Statistics shown on an orders table of 10,000 rows: the histogram lists Saffron 5 rows, Spice 995 and Tea 9,000, and the optimizer uses an Index Seek with a Key Lookup for Saffron and a Clustered Index Scan for Tea

SQL Statistics: How SQL Server Estimates Row Counts

SQL statistics are small summaries of your data that tell the optimizer how many rows a query will return. This post builds an orders table with one rare value and one common value, reads the histogram, compares estimated and actual rows in the plans, and shows a stale statistic fixed with UPDATE STATISTICS. Every result comes from SQL Server 2025.

Read More