TLDR Web Dev · ARTICLES & TUTORIALS

SQL NULLS ARE WEIRD!

jirevwe.github.io9 min read

Summary from the TLDR Web Dev newsletter

SQL's handling of NULL values is counterintuitive: the `=` operator doesn't consider two NULLs equal, returning NULL instead, while the `IS` operator correctly identifies them as identical. This behavior affects unique constraints - multiple NULLs can exist in a uniquely constrained column because each NULL is treated as distinct. These two methods help to enforce uniqueness with NULLs: creating a generated column that replaces NULLs with a default value and using a partial index that only enforces uniqueness when a specific column is NULL.