Bird
0
0

Which of the following is the correct syntax to add a FOREIGN KEY constraint to an existing table Orders referencing Customers(CustomerID)?

easy📝 Syntax Q12 of 15
SQL - Table Constraints
Which of the following is the correct syntax to add a FOREIGN KEY constraint to an existing table Orders referencing Customers(CustomerID)?
AALTER TABLE Orders ADD FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID);
BALTER TABLE Orders ADD PRIMARY KEY (CustomerID) REFERENCES Customers(CustomerID);
CALTER TABLE Orders ADD FOREIGN KEY CustomerID REFERENCES Customers(CustomerID);
DALTER TABLE Orders ADD FOREIGN KEY (CustomerID) TO Customers(CustomerID);
Step-by-Step Solution
Solution:
  1. Step 1: Recall correct ALTER TABLE syntax for FOREIGN KEY

    The correct syntax uses: ALTER TABLE table_name ADD FOREIGN KEY (column) REFERENCES other_table(column);
  2. Step 2: Check each option

    ALTER TABLE Orders ADD FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID); matches the correct syntax exactly. Options A, B, and C have syntax errors or wrong keywords.
  3. Final Answer:

    ALTER TABLE Orders ADD FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID); -> Option A
  4. Quick Check:

    Correct ALTER TABLE FOREIGN KEY syntax = D [OK]
Quick Trick: Use ADD FOREIGN KEY (col) REFERENCES table(col) syntax [OK]
Common Mistakes:
MISTAKES
  • Using PRIMARY KEY instead of FOREIGN KEY
  • Omitting parentheses around column name
  • Using TO instead of REFERENCES keyword

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes