带有ON DELETE CASCADE的MySQL语法错误#1064

时间:2013-07-18 11:55:18

标签: mysql syntax-error

对于我的生活,我无法找出为什么MySQL不喜欢这句话:

CREATE TABLE IF NOT EXISTS personnel
(
    id INTEGER AUTO_INCREMENT PRIMARY KEY,
    firstname VARCHAR(30) NOT NULL,
    lastname VARCHAR(30) NOT NULL,
    role VARCHAR(50) NOT NULL,
    line_manager INTEGER NULL,

    FOREIGN KEY (role) REFERENCES roles(name)
        ON DELETE CASCADE
        ON UPDATE CASCADE,
    FOREIGN KEY (line_manager) REFERENCES personnel(id)
        ON DELETE CASCADE
        ON UPDATE CASCADE
) ENGINE=InnoDB;

插入此项后,MySQL的结果输出为ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELETE CASCADE UPDATE CASCADE, FOREIGN KEY (line_manager) REFERENCES personnel(id) ' at line 10

有人能说出我做错了吗?

1 个答案:

答案 0 :(得分:2)

嘿,我已经检查了你的mysql查询并且它有效我认为你应该检查以下约束

1) table roles must have same **engine** that is InnoDB
2) the length of both column that is personnel(role) and roles(name) must be **equal**
3) column roles(name) must be **primary key**

检查一下,我认为它会起作用