SQL NULLs: Breaking Your Intuition
SQL's treatment of NULL values often defies expectations. This post reveals the surprising behavior of NULLs in unique constraint columns: multiple NULLs can coexist. Through practical examples in SQLite, Postgres, and MySQL, the author demonstrates how NULLs behave differently with the '=' and 'IS' operators, explaining the underlying reasons. Two solutions for ensuring uniqueness are explored: creating a generated column and using a partial index. Using a partial index is recommended as best practice, avoiding table size increases and potential errors.
Read more