I am getting the error
“Number of referencing columns in foreign key differs from number of referenced columns, table ‘StudentGrade'” when trying to execute the following SQL script
CREATE TABLE StudentGrade
(
StudentID INT NOT NULL
CONSTRAINT FK_SG_StudentID FOREIGN KEY (StudentID)
REFERENCES Student(StudentID),
ClassID VARCHAR (6) NOT NULL
CONSTRAINT FK_Class FOREIGN KEY (ClassID, CourseID)
REFERENCES Class(ClassID),
CourseID VARCHAR (64) NOT NULL
CONSTRAINT FK_Course FOREIGN KEY (CourseID)
REFERENCES Course(CourseID),
FacultyID INT NOT NULL
CONSTRAINT FK_Faculty FOREIGN KEY (FacultyID)
REFERENCES Faculty(FacultyID),
Grade NUMERIC NULL,
CONSTRAINT PK_StudentID PRIMARY KEY (StudentID, ClassID, CourseID, FacultyID)
)
I know that there is something I am doing wrong with the foreign keys though I can’t find anywhere where it explains how to use foreign keys and composite keys together. Any help would be greatly appreciated. Thank you so much!
change your second foreign key from
to
In
FK_Classyou are referencing the columnsStudentGrade.ClassIDandStudentGrade.CourseIDinto a single oneClass.ClassIDand this cannot work.Your
FK_Coursealready refersCourseID, so you can simply deleteCourseIDfromFK_Classas I said above.Edit
Add
CourseIDto your definition ofFK_Classas