SQL脚本外键错误
我收到错误 尝试执行以下 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)
)
我知道我在外键上做错了什么,尽管我找不到任何地方它解释了如何一起使用外键和复合键。任何帮助将不胜感激。太感谢了!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将第二个外键从 更改为
在
FK_Class
中,您将StudentGrade.ClassID
和StudentGrade.CourseID
列引用到单个Class 中.ClassID
这不起作用。您的
FK_Course
已经引用了CourseID
,因此您只需从FK_Class
中删除CourseID
,就像我上面所说的那样。编辑
将
CourseID
添加到FK_Class
的定义中:change your second foreign key from
to
In
FK_Class
you are referencing the columnsStudentGrade.ClassID
andStudentGrade.CourseID
into a single oneClass.ClassID
and this cannot work.Your
FK_Course
already refersCourseID
, so you can simply deleteCourseID
fromFK_Class
as I said above.Edit
Add
CourseID
to your definition ofFK_Class
as