CREATE TABLE中的MySQL错误#1064

时间:2012-11-27 10:10:44

标签: mysql syntax foreign-keys

这是我正在创建的两个表的sql:

CREATE TABLE IF NOT EXISTS tbl_a(
    id INT(6) UNSIGNED NOT NULL,
    PRIMARY KEY(id)
)ENGINE = INNODB;

CREATE TABLE IF NOT EXISTS tbl_b(
    id INT(6) UNSIGNED NOT NULL,
    PRIMARY KEY(id),
    FOREIGN KEY id REFERENCES tbl_a(id) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE = INNODB;

这是我得到的错误:

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 'REFERENCES a(id) ON DELETE CASCADE ON UPDATE CASCADE )ENGINE = INNODB' at line 4

有人可以建议我一个解决方案吗?

1 个答案:

答案 0 :(得分:1)

在密钥名称

周围添加()
CREATE TABLE IF NOT EXISTS tbl_b(
    id INT(6) UNSIGNED NOT NULL,
    PRIMARY KEY(id),
    FOREIGN KEY (id) REFERENCES tbl_a(id) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE = INNODB;