Can Foreign Keys be Null?

sonal khatri 81 Reputation points
2023-06-05T12:07:19.8766667+00:00

Hello,

According to me, FKs can't be null as otherwise It will violate the Foreign Key constraint.

FK refers to primary key and Primary key can't be null.

Can you help me understand this better?

SQL Server Other
{count} votes

2 answers

Sort by: Most helpful
  1. Olaf Helper 47,436 Reputation points
    2023-06-05T12:11:55.36+00:00

    FK refers to primary key and Primary key can't be null.

    Primary key value never can be NULL, that's correct.

    But a foreign key column can be NULL(able), it then references no PK and that's completley valid.

    1 person found this answer helpful.
    0 comments No comments

  2. LiHongMSFT-4306 31,566 Reputation points
    2023-06-06T01:57:56.8333333+00:00

    Hi @sonal khatri

    It will violate the Foreign Key constraint.

    Primary Key Constraints and Foreign Key Constraints are two different things. Foreign key simply requires that the value in that field must exist first in a different table (the parent table).

    NULL by definition is not a value. Null means that we do not yet know what the value is.

    Here is a simple example: Suppose we have an employee table with employee_id and manager_id where manager_id is foreign key and CEO of the organization doesn't have manager so his manager_id will be NULL in Employee table.

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.