Quantcast
Channel: NULL permitted in Primary Key - why and in which DBMS? - Stack Overflow
Viewing all articles
Browse latest Browse all 7

NULL permitted in Primary Key - why and in which DBMS?

$
0
0

Further to my question "Why to use ´not null primary key´ in TSQL?"...

As I understood from other discussions, some RDBMS (for example SQLite, MySQL) permit "unique" NULL in the primary key.

Why is this allowed and how might it be useful?

Background: I believe it is beneficial for communication with colleagues and database professionals to know the differences in fundamental concepts, approaches and their implementations in different DBMS.

Notes

  • MySQL is rehabilitated and returned to the "NOT NULL PK" list.
  • SQLite has been added (thanks to Paul Hadfield) to "NULL PK" list:

For the purposes of determining the uniqueness of primary key values, NULL values are considered distinct from all other values, including other NULLs.

If an INSERT or UPDATE statement attempts to modify the table content so that two or more rows feature identical primary key values, it is a constraint violation. According to the SQL standard, PRIMARY KEY should always imply NOT NULL. Unfortunately, due to a long-standing coding oversight, this is not the case in SQLite.

Unless the column is an INTEGER PRIMARY KEY SQLite allows NULL values in a PRIMARY KEY column. We could change SQLite to conform to the standard (and we might do so in the future), but by the time the oversight was discovered, SQLite was in such wide use that we feared breaking legacy code if we fixed the problem.

So for now we have chosen to continue allowing NULLs in PRIMARY KEY columns. Developers should be aware, however, that we may change SQLite to conform to the SQL standard in future and should design new programs accordingly.

SQL As Understood By SQLite: CREATE TABLE


Viewing all articles
Browse latest Browse all 7

Latest Images

Trending Articles





Latest Images