Bird
0
0

A foreign key constraint fails to create with error "Cannot add foreign key constraint". Which fix is most appropriate?

medium📝 Debug Q7 of 15
SQL - Table Constraints
A foreign key constraint fails to create with error "Cannot add foreign key constraint". Which fix is most appropriate?
ALTER TABLE Orders ADD FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID);
AChange Orders.CustomerID to VARCHAR if Customers.CustomerID is INT.
BEnsure Customers.CustomerID is a primary or unique key.
CRemove the foreign key from Orders.
DDelete all rows from Orders before adding the constraint.
Step-by-Step Solution
Solution:
  1. Step 1: Check referenced key requirements

    The referenced column must be a primary or unique key.
  2. Step 2: Apply fix

    Ensuring Customers.CustomerID is primary or unique allows foreign key creation.
  3. Final Answer:

    Ensure Customers.CustomerID is a primary or unique key. -> Option B
  4. Quick Check:

    Referenced key must be unique or primary [OK]
Quick Trick: Referenced column must be unique or primary key [OK]
Common Mistakes:
MISTAKES
  • Changing data types incorrectly
  • Deleting data unnecessarily
  • Removing foreign key instead of fixing parent key

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes