Bird
0
0

Which of the following is the correct syntax to add a foreign key constraint to an existing table?

easy📝 Syntax Q3 of 15
SQL - Table Relationships
Which of the following is the correct syntax to add a foreign key constraint to an existing table?
AALTER TABLE Orders ADD FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID);
BCREATE FOREIGN KEY Orders.CustomerID REFERENCES Customers(CustomerID);
CALTER TABLE Orders ADD PRIMARY KEY (CustomerID);
DADD FOREIGN KEY Orders.CustomerID TO Customers(CustomerID);
Step-by-Step Solution
Solution:
  1. Step 1: Recall correct ALTER TABLE syntax for foreign keys

    The correct syntax uses ALTER TABLE with ADD FOREIGN KEY and REFERENCES clauses.
  2. Step 2: Check each option

    ALTER TABLE Orders ADD FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID); matches the correct syntax. Options A, C, and D are incorrect or incomplete.
  3. Final Answer:

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

    Correct foreign key syntax = ALTER TABLE Orders ADD FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID); [OK]
Quick Trick: Use ALTER TABLE ADD FOREIGN KEY with REFERENCES [OK]
Common Mistakes:
MISTAKES
  • Using CREATE instead of ALTER TABLE
  • Confusing PRIMARY KEY with FOREIGN KEY
  • Omitting REFERENCES keyword

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes