SQL脚本外键错误

发布于 2024-11-14 00:49:36 字数 792 浏览 1 评论 0原文

我收到错误 尝试执行以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

却一份温柔 2024-11-21 00:49:36

将第二个外键从 更改为

CONSTRAINT FK_Class FOREIGN KEY (ClassID, CourseID)

CONSTRAINT FK_Class FOREIGN KEY (ClassID)

FK_Class 中,您将 StudentGrade.ClassIDStudentGrade.CourseID 列引用到单个 Class 中.ClassID 这不起作用。

您的 FK_Course 已经引用了 CourseID,因此您只需从 FK_Class 中删除 CourseID,就像我上面所说的那样。

编辑

CourseID 添加到 FK_Class 的定义中:

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文