简体   繁体   English

MySQL外键错误,删除级联

[英]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.

相关问题 雄辩的删除和MySQL外键级联 - Eloquent delete and MySQL foreign key cascade 与外键和删除级联有关的MySQL问题 - Mysql Issue Related to foreign key and on delete cascade 跨3个表的MySQL外键“ ON DELETE CASCADE” - MySQL Foreign Key “ON DELETE CASCADE” across 3 tables MySQL外键约束,级联删除 - MySQL foreign key constraints, cascade delete 使用SQLAlchemy删除级联外键约束错误 - Delete cascade foreign key constraint error with SQLAlchemy MySQL:DROP TABLE 上的错误 1217。 外键引用自己的主键。 ON DELETE CASCADE 开启 - MySQL : error 1217 on DROP TABLE. Foreign key refering on its own primary key. ON DELETE CASCADE is on InnoDB MySql Error 1215无法添加外键约束,即使在删除DELETE的级联义务之后也是如此 - InnoDB MySql Error 1215 Cannot add foreign key constraint / even after removing cascade obligations on DELETE (Django)MySQL外键级联是否首先删除父级或子级 - (Django) Does MySQL foreign key cascade delete parent or children first 当父级被删除时,Mysql“级联删除”不会在外键行上删除 - Mysql 'Cascade On Delete' Not Deleting on Foreign Key Row When Parent Is Deleted MySQL外键级联删除不起作用,完全陷入困境 - MySQL foreign key cascade delete not working, completely stumped
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM