Thursday, December 3, 2026 | London 10°C · Drizzle
DailyGlimpse

AI

SQL UNIQUE KEY and NULL: The Surprising Truth Developers Miss

My Project HD 1 min read

In SQL and relational databases, a common interview question stumps even senior developers: Can a UNIQUE KEY column contain NULL values? And if so, can it hold multiple NULLs?

Contrary to what many assume, the answer is not straightforward. In standard SQL, a UNIQUE constraint allows NULL values, and importantly, it permits multiple NULLs. The reason lies in how SQL handles NULL: NULL is not equal to anything, even another NULL. So two NULLs are not considered duplicates.

However, different database systems vary in their implementation:

  • MySQL and PostgreSQL follow the standard and allow multiple NULLs in a UNIQUE column.
  • SQL Server also allows multiple NULLs, consistent with the standard.
  • Some older or strict systems (e.g., older versions of Oracle) treat NULLs as equal for UNIQUE constraints, allowing only one NULL.

This behavior highlights a key difference between UNIQUE KEY and PRIMARY KEY: a PRIMARY KEY implicitly disallows NULLs, while a UNIQUE KEY generally allows them (with system-dependent variation).

For backend developers, data analysts, and data engineers, understanding this nuance is critical for designing databases correctly and acing technical interviews.

React