문제

My work

  • A primary key is unique and only one contains for a table.

  • There are many unique keys that can contain in a table.

  • In SQL Server, the primary key is not Nullable. But, a unique key may contain only one null value.

My question:

Practically in SQL Server, the primary key is not nullable. But when it comes in theoretical, why don't we can have exactly only one Null value in the primary key when it allows a Null value in the unique key?

Thanks for answering my question

Sorry for asking a general question, after reading the answer I got a clear idea about null values in DB.

Also, I confused with programming languages like in C++, Jave, where the null value can be comparable. After doing some work, I understand that Null is not comparable in SQL.

In programming languages like in C++, Jave...

Null==Null 

returns TRUE

But, in SQL

Null==Null 

returns FALSE, because Null value is unknown and it could be any value.

도움이 되었습니까?

해결책

No attribute that is part of a key can contain nulls; if a column is nullable then it isn't part of any key. It is also the case that the set of columns defined by a UNIQUE or PRIMARY KEY constraint is NOT a key unless it is a minimal superkey and also non-nullable.

Why can't nulls permit keys? There are lots of potential reasons:

  1. Keys are a relational concept whereas nulls (as we know them today) are essentially a SQL invention which came along years later. SQL DBMSs are different to Relational DBMSs.
  2. Nulls are not values so they are not key values. Nulls are markers for unspecified values.
  3. Keys are supposed to be irreducible (minimal) - in other words all the components of the key are required to make a superkey. Since null means that some components are missing it contradicts this most basic definition of a key.
  4. In standard SQL a uniqueness constraint does not enforce uniqueness if any column permits nulls.
  5. SQL DBMSs also permit nulls in other constraints (except PRIMARY KEY): FOREIGN KEY, CHECK, ASSERTION. Nulls never violate the constraint so the null acts as a kind of "get out clause" for data integrity. Hypothetically, if nulls were permitted in keys then that would seem to imply a stricter form of referential integrity that didn't permit nulls in foreign keys unless there was a corresponding null key in the parent table. In that case every nullable foreign key would have to reference a nullable key and could not reference a non-nullable one.
  6. There are no nulls in reality, in everyday mathematics or logic; they are just a programming feature in SQL and are not particularly "mathematical" or even consistent in the way they work. Don't look for more reasons: nulls work the way they do just because the designers of SQL say so.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 dba.stackexchange
scroll top