我收到错误 “尝试执行以下SQL脚本时,外键中的引用列数与引用列数,表'StudentGrade'”不同
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)
)
我知道我的外键有问题,但我无法找到解释如何使用外键和复合键的地方。任何帮助将不胜感激。非常感谢你!
答案 0 :(得分:1)
从
更改您的第二个外键CONSTRAINT FK_Class FOREIGN KEY (ClassID, CourseID)
到
CONSTRAINT FK_Class FOREIGN KEY (ClassID)
在FK_Class
中,您将列StudentGrade.ClassID
和StudentGrade.CourseID
引用为一个Class.ClassID
,但这不起作用。
您的FK_Course
已提及CourseID
,因此您只需按照上述说明从CourseID
删除FK_Class
。
修改强>
将CourseID
添加到FK_Class
的定义为
CONSTRAINT FK_Class FOREIGN KEY (ClassID, CourseID)
REFERENCES Class(ClassID, CourseID)