Bird
0
0

Which foreign key option should you use?

hard📝 Application Q15 of 15
SQL - Table Relationships
You want to enforce referential integrity between Employees and Departments tables. When a department is deleted, you want all employees in that department to be reassigned to department ID 0 (which means 'Unassigned'). Which foreign key option should you use?
AFOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID) ON DELETE RESTRICT
BFOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID) ON DELETE CASCADE
CFOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID) ON DELETE SET NULL
DFOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID) ON DELETE SET DEFAULT
Step-by-Step Solution
Solution:
  1. Step 1: Understand ON DELETE SET DEFAULT

    This option sets the foreign key column to its default value when the referenced row is deleted.
  2. Step 2: Match requirement

    Since you want employees reassigned to department ID 0, set DepartmentID column default to 0 and use ON DELETE SET DEFAULT to assign that value automatically.
  3. Final Answer:

    FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID) ON DELETE SET DEFAULT -> Option D
  4. Quick Check:

    Reassign on delete = ON DELETE SET DEFAULT [OK]
Quick Trick: Use ON DELETE SET DEFAULT to assign default on delete [OK]
Common Mistakes:
MISTAKES
  • Using CASCADE deletes employees instead of reassigning
  • Using SET NULL when column disallows NULL
  • Using RESTRICT blocks deletion

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes