gpt4 book ai didi

mysql - 我不知道我的错误在哪里 'incorrect syntax near the keyword CONSTRAINT'

转载 作者:太空宇宙 更新时间:2023-11-03 10:48:17 25 4
gpt4 key购买 nike

CREATE TABLE ClassRegistration(
StudentID int NOT NULL,
CourseID int NOT NULL,

CONSTRAINT PK_ClassRegistration PRIMARY KEY (StudentID, CourseID),
CONSTRAINT FK_ClassRegistration_Students,
FOREIGN KEY(StudentID) REFERENCES Students(ID),
ON DELETE CASCADE ON UPDATE CASCADE,

CONSTRAINT FK_ClassRegistration_Classes,
FOREIGN KEY(CourseID) REFERENCES Course(ClassNumber),
ON DELETE CASCADE ON UPDATE CASCADE
)

我认为这只是逗号放在哪里的问题,但我是自学的,所以很难弄清楚。

最佳答案

约束定义没有“嵌入”逗号:

CREATE TABLE ClassRegistration
(
StudentID int NOT NULL,
CourseID int NOT NULL,

CONSTRAINT PK_ClassRegistration PRIMARY KEY (StudentID, CourseID),

CONSTRAINT FK_ClassRegistration_Students --- no comma here
FOREIGN KEY(StudentID) REFERENCES Students(ID) -- no comma here
ON DELETE CASCADE ON UPDATE CASCADE,

CONSTRAINT FK_ClassRegistration_Classes -- no comma here
FOREIGN KEY(CourseID) REFERENCES Course(ClassNumber) -- no comma here
ON DELETE CASCADE ON UPDATE CASCADE
);

顺便说一句:CONSTRAINT PK_ClassRegistration 部分没用。你不能在 MySQL 中给主键约束一个名字(你可以指定它,但它会被忽略并总是命名为PRIMARY)

关于mysql - 我不知道我的错误在哪里 'incorrect syntax near the keyword CONSTRAINT',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28226019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com