Skip to content
Exercises · Q2

Q.Why foreign keys are allowed to have NULL values? Explain with an example.

Rajasthan RbseTextbookSubjective· 3mImportance★★★★★
21% · 3/14 Questions
✓ Free question

A foreign key links a tuple to a tuple in another relation — but sometimes that relationship is optional or not yet decided. So the referential-integrity rule is deliberately written as: every non-NULL foreign key value must match a primary-key value in the referenced relation. NULL means "no related tuple (yet)", and that is legal unless you explicitly add NOT NULL.

The reasoning

The foreign key constraint (referential integrity) exists to prevent dangling references — you must never point at a tuple that does not exist. But there is a second, different situation: not pointing at anything at all. That is not a dangling reference; it is an honest statement that the relationship is unknown or not applicable for this tuple. Relational databases represent exactly this with NULL. Common real cases:

  • A new employee who has not yet been allotted a department.
  • A student whose sports preference has not been given yet.
  • An order whose delivery agent is not yet assigned.

If foreign keys were forced to be non-NULL, you could not store such tuples at all until the related fact existed — losing real data to satisfy a formality.

Worked example in SQL

CREATE TABLE DEPARTMENT (
  DeptID   INT PRIMARY KEY,
  DeptName VARCHAR(30) NOT NULL
);

CREATE TABLE EMPLOYEE (
  EmpID  INT PRIMARY KEY,
  EName  VARCHAR(30) NOT NULL,
  DeptID INT,                                   -- no NOT NULL: relationship is optional
  FOREIGN KEY (DeptID) REFERENCES DEPARTMENT(DeptID)
);

INSERT INTO DEPARTMENT VALUES (10, 'Sales'), (20, 'Accounts');

INSERT INTO EMPLOYEE VALUES (101, 'Meera', 10);    -- OK: 10 exists in DEPARTMENT
INSERT INTO EMPLOYEE VALUES (105, 'Rahul', NULL);  -- OK: NULL allowed, dept not yet assigned
INSERT INTO EMPLOYEE VALUES (108, 'Zoya',  30);    -- REJECTED: 30 not in DEPARTMENT

State of EMPLOYEE after these statements:

EmpIDENameDeptID
101Meera10
105RahulNULL

The third INSERT fails with a foreign-key violation (there is no department 30), while Rahul's NULL is accepted.

Key lines explained

  • FOREIGN KEY (DeptID) REFERENCES DEPARTMENT(DeptID) — enforces referential integrity: any non-NULL DeptID in EMPLOYEE must already exist as a primary-key value in DEPARTMENT.
  • DeptID INT without NOT NULL — this is what makes the relationship optional. Later, UPDATE EMPLOYEE SET DeptID = 20 WHERE EmpID = 105; records the assignment when it happens.
  • If the rule were "every employee MUST belong to a department", you would write DeptID INT NOT NULL — the designer chooses; the relational model itself does not force it.
✓Final answer

Foreign keys may hold NULL because the relationship they represent can be optional or currently unknown. Referential integrity requires only that every non-NULL foreign-key value matches an existing primary-key value in the referenced relation; NULL means "no related tuple yet" and causes no dangling reference. Example: employee (105, 'Rahul', NULL) in EMPLOYEE(EmpID, EName, DeptID → DEPARTMENT) is valid — Rahul simply has no department assigned yet.

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.