assume we have some nulls in pk, what we can do about it in practice?
You have two choices:
- UPDATE the rows and set non-NULL values in that column before you declare it as a primary key.
- DELETE the rows that have NULL in that column before you declare it as a primary key.
You cannot declare a primary key constraint on a column if it has NULLs. This causes an error.
Likewise, you can't insert a NULL in the primary key column afterwards. This causes an error too, because if it's a primary key, the column must have the NOT NULL option.
Let's generalise the question a bit: forget about pk, imagine we have a unique column with some nulls, what do you do about nulls?
If you're talking about a more general case of a column with a unique key, it is okay to declare a unique key on a nullable column.
Depending on the purpose of the column, it could be legitimate to use NULL where the row has no value. But it must be an attribute column, not the primary key.
(we can treat this column as pk even if it is not declared as pk)
No, sorry, you cannot even treat this column as a de facto primary key if it allows NULLs. The purpose of a primary key is not only to be unique, but to provide a way for a query to select each row individually by value. Since a nullable unique key can have any number of rows with NULL (notwithstanding the nonstandard behavior of Microsoft SQL Server), a NULL is not able to unambiguously select each row individually.
It's hard to understand why you are asking this question. Can you describe the scenario more concretely? What is the problem you're trying to solve? I am suspecting that this is an XY Problem.