Bird
0
0

You try to create a foreign key constraint with:

medium📝 Debug Q6 of 15
SQL - Table Constraints
You try to create a foreign key constraint with:
ALTER TABLE Orders ADD FOREIGN KEY (CustID) REFERENCES Customers(CustomerID);
But get an error: "Cannot add foreign key constraint." What is a likely cause?
AThe Customers table does not have a primary key.
BThe Orders table already has a foreign key.
CThe CustID column data type does not match Customers.CustomerID.
DThe Orders table has no rows.
Step-by-Step Solution
Solution:
  1. Step 1: Check foreign key requirements

    Foreign key and referenced column must have matching data types.
  2. Step 2: Analyze error cause

    If data types differ, the constraint cannot be created, causing the error.
  3. Final Answer:

    The CustID column data type does not match Customers.CustomerID. -> Option C
  4. Quick Check:

    Data type mismatch = foreign key creation error [OK]
Quick Trick: Foreign key and referenced key must share data type [OK]
Common Mistakes:
MISTAKES
  • Assuming multiple foreign keys cause error
  • Ignoring primary key existence in parent table
  • Thinking empty tables cause foreign key errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes