Skip to content
Exercises · Q2

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

Puducherry TnboardTextbookSubjective· 3mImportance★★★★★
10% · 3/29 Questions
✓ Free question

Foreign keys can be NULL because a child row may legitimately exist without being linked to any parent row — NULL means "no relationship," not "invalid relationship."

The question gets at a design choice that often confuses students. A foreign key constraint says: if this column has a value, that value must exist in the parent table's primary key. But the constraint does not require the column to have a value at all. That distinction is the whole point.

Think about real-world data. An employee table might have a DeptID foreign key referencing a department table. What happens when a new employee joins but hasn't been assigned to a department yet? You cannot put a fake department ID — that would violate referential integrity. You cannot leave the employee out of the database — you need their payroll details. The clean solution is to let DeptID be NULL, meaning "not yet assigned." The foreign key constraint still protects you: if someone later types DeptID = 999, the database will reject it because 999 isn't in the department table. But NULL passes through untouched — the constraint simply does not check it.

Note

This is the optional participation rule in database design. A child entity may participate in a relationship optionally (zero occurrences) or mandatorily (at least one). NULL foreign keys implement the optional case.

Here is a concrete example using two tables:

Departments

DeptIDDeptName
10Sales
20Engineering
30HR

Employees (with a foreign key on DeptID referencing Departments(DeptID))

EmpIDEmpNameDeptID
101Alice10
102Bob20
103CarolNULL
104David30
105EveNULL

Carol and Eve have NULL in DeptID. This is perfectly valid — Carol might be a new hire awaiting placement, and Eve might be an independent consultant not tied to any department. The database will accept these rows because the foreign key constraint only enforces that non-NULL values exist in the parent table.

Now try inserting INSERT INTO Employees VALUES (106, 'Frank', 99). The database rejects it — 99 is not in Departments.DeptID. The constraint works exactly as designed.

Watch out

A common mistake is thinking a foreign key must always match a parent row. That is only true for non-NULL values. NULL is the exception — it means "no parent," not "invalid parent."

Tip

When designing a schema, ask: "Can this child row exist without a parent?" If yes, make the foreign key nullable. If every child must have a parent (e.g., an order must have a customer), add NOT NULL to the foreign key column.

✓Final answer

Foreign keys are allowed to have NULL values because a child row may exist without being linked to any parent row — NULL represents the absence of a relationship, not a broken one.

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.