开发者

SQL Script Foreign Key Error

开发者 https://www.devze.com 2023-03-11 17:53 出处:网络
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

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)
        R开发者_运维百科EFERENCES 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

CONSTRAINT FK_Class FOREIGN KEY (ClassID, CourseID)

to

CONSTRAINT FK_Class FOREIGN KEY (ClassID)

In FK_Class you are referencing the columns StudentGrade.ClassID and StudentGrade.CourseID into a single one Class.ClassID and this cannot work.

Your FK_Course already refers CourseID, so you can simply delete CourseID from FK_Class as I said above.

Edit

Add CourseID to your definition of FK_Class as

CONSTRAINT FK_Class FOREIGN KEY (ClassID, CourseID)
        REFERENCES Class(ClassID, CourseID)
0

精彩评论

暂无评论...
验证码 换一张
取 消