简体   繁体   中英

MySQL error #1064 in CREATE TABLE

Here's my sql for two tables I'm creating:

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;

This is the error I'm getting:

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

Could anyone suggest me a solution, please?

Add () around the key name

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM