[英]MySQL FOREIGN KEY error, ON DELETE CASCADE
I have absolutely no clue why MySQL is having an issue with the second CREATE TABLE
statement. 我完全不知道为什么MySQL在第二个
CREATE TABLE
语句上有问题。
CREATE TABLE User(
uid INTEGER,
url CHAR(100),
firstname CHAR(40),
lastname CHAR(40),
PRIMARY KEY(uid)
);
The below is the one that causes problems: 以下是导致问题的原因:
CREATE TABLE Follows(
uid INTEGER,
url CHAR(100),
PRIMARY KEY(uid,url),
FOREIGN KEY(uid) REFERENCES User(uid), ON DELETE CASCADE,
FOREIGN KEY(url) REFERENCES User(url), ON DELETE CASCADE
);
Error I get is: 我得到的错误是:
#1064 - You have an error in your SQL syntax;
#1064-您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ON DELETE CASCADE, FOREIGN KEY(url) REFERENCES User(url), ON DELETE CASCADE)' at line 1
请在第1行的“ ON DELETE CASCADE,FOREIGN KEY(URL)参考用户(URL),ON DELETE CASCADE)附近检查与您的MySQL服务器版本相对应的手册以使用正确的语法”
There are a few issues here: 这里有一些问题:
First the on delete cascade
is part of the foreign key definition, so the comma ( ,
) before it should be removed. 首先,
on delete cascade
是外键定义的一部分,因此应删除其前的逗号( ,
)。
Second, the second foreign key references url
, which is not a unique key, and therefore is not allowed. 其次,第二个外键引用
url
,它不是唯一键,因此是不允许的。 So either remove this constraints: 因此,要么删除此约束:
CREATE TABLE Follows (
uid INTEGER,
url CHAR(100),
PRIMARY KEY(uid,url),
FOREIGN KEY(uid) REFERENCES User(uid) ON DELETE CASCADE
);
Or define another unique key on url
: 或在
url
上定义另一个唯一键:
CREATE TABLE User(
uid INTEGER,
url CHAR(100),
firstname CHAR(40),
lastname CHAR(40),
PRIMARY KEY(uid),
UNIQUE (url)
);
CREATE TABLE Follows (
uid INTEGER,
url CHAR(100),
PRIMARY KEY(uid,url),
FOREIGN KEY(uid) REFERENCES User(uid) ON DELETE CASCADE,
FOREIGN KEY(url) REFERENCES User(url) ON DELETE CASCADE
);
尝试在引用用户(uid)和引用用户(url)之后删除此“,”
you have to put a gap after User(uid) ON DELETE CASCADE, also you should write in to the end of the query like this: engine='innodb'; 您必须在User(uid)ON DELETE CASCADE后面加上一个空格,也应该像这样写到查询的末尾:engine ='innodb';
i mean between the last ) and ; 我是说在最后)和;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.