Bird
0
0

Which SQL statement correctly creates a junction table named StudentCourse linking Student and Course tables by their IDs?

easy📝 Syntax Q12 of 15
SQL - Table Relationships
Which SQL statement correctly creates a junction table named StudentCourse linking Student and Course tables by their IDs?
ACREATE TABLE StudentCourse (StudentID INT, CourseID INT, FOREIGN KEY (StudentID) REFERENCES Student(ID));
BCREATE TABLE StudentCourse (ID INT PRIMARY KEY, StudentID INT, CourseID INT);
CCREATE TABLE StudentCourse (StudentID INT UNIQUE, CourseID INT UNIQUE);
DCREATE TABLE StudentCourse (StudentID INT, CourseID INT, PRIMARY KEY (StudentID, CourseID));
Step-by-Step Solution
Solution:
  1. Step 1: Define junction table columns

    It needs two columns for foreign keys: StudentID and CourseID.
  2. Step 2: Set primary key on both columns

    Primary key on (StudentID, CourseID) ensures unique pairs and no duplicates.
  3. Final Answer:

    CREATE TABLE StudentCourse (StudentID INT, CourseID INT, PRIMARY KEY (StudentID, CourseID)); -> Option D
  4. Quick Check:

    Junction table needs composite primary key [OK]
Quick Trick: Use composite primary key on both foreign keys [OK]
Common Mistakes:
MISTAKES
  • Using UNIQUE on individual columns instead of composite key
  • Missing one foreign key column
  • Not defining primary key on the pair

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes