Bird
0
0

You have two tables from an ER diagram: Student(id, name) and Enrollment(student_id, course_id). You want to add a foreign key constraint to Enrollment.student_id. Which SQL statement is correct?

medium📝 Debug Q14 of 15
SQL - Table Relationships
You have two tables from an ER diagram: Student(id, name) and Enrollment(student_id, course_id). You want to add a foreign key constraint to Enrollment.student_id. Which SQL statement is correct?
AALTER TABLE Enrollment ADD FOREIGN KEY (student_id) REFERENCES Student(id);
BALTER TABLE Student ADD FOREIGN KEY (id) REFERENCES Enrollment(student_id);
CCREATE FOREIGN KEY Enrollment.student_id REFERENCES Student.id;
DALTER Enrollment ADD CONSTRAINT FOREIGN KEY student_id Student(id);
Step-by-Step Solution
Solution:
  1. Step 1: Identify the correct syntax for adding foreign key

    The standard syntax is ALTER TABLE [table] ADD FOREIGN KEY (column) REFERENCES [other_table](column).
  2. Step 2: Apply to given tables

    Enrollment.student_id references Student.id, so the statement must alter Enrollment table.
  3. Final Answer:

    ALTER TABLE Enrollment ADD FOREIGN KEY (student_id) REFERENCES Student(id); -> Option A
  4. Quick Check:

    ALTER TABLE + ADD FOREIGN KEY + REFERENCES [OK]
Quick Trick: Foreign key added on referencing table with ALTER TABLE [OK]
Common Mistakes:
MISTAKES
  • Adding foreign key on referenced table
  • Wrong ALTER TABLE syntax
  • Using CREATE FOREIGN KEY instead of ALTER TABLE

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes