N:M关系语法代码错误

时间:2011-09-27 08:04:07

标签: mysql sql syntax

我有这个SQL(对于MySQL):

create table apartmentcaretakers
(
apartmentID int,
caretakerID int,
PRIMARY KEY (apartmentID, caretakerID),
FOREIGN KEY (apartmentID) REFERENCES apartments(apartmentID),
FOREIGN KEY (caretakerID) REFERENCES caretakers(caretakerID)
)engine = innodb;

我的错误: 'references apartments(apartmentID)附近的SQL语法错误,外键caretakerID引用看护人'

这对我来说是一个小错误,但有人可以指出吗?

编辑:经过一些编码和重新运行后,错误是1064.我可以理解为什么它没有真正帮助,因为这是一个广谱错误。

1 个答案:

答案 0 :(得分:0)

也为我工作。

确保您引用的表也使用INNODB引擎。使用以下查询检查:SHOW TABLE STATUS WHERE Name = 'xxx'

mysql> CREATE TABLE apartments (
    -> apartmentID int PRIMARY KEY NOT NULL) ENGINE = innodb;
Query OK, 0 rows affected (0.01 sec)

mysql> CREATE TABLE caretakers (
    -> caretakerID int PRIMARY KEY NOT NULL) ENGINE = innodb;
Query OK, 0 rows affected (0.02 sec)

mysql> CREATE TABLE apartmentcaretakers (
    -> apartmentID int,
    -> caretakerID int,
    -> PRIMARY KEY (apartmentID, caretakerID),
    -> FOREIGN KEY ac_fk_apartments_id (apartmentID) REFERENCES apartments (apartmentID),
    -> FOREIGN KEY ac_fk_caretakers_id(caretakerID) REFERENCES caretakers (caretakerID)
    -> ) ENGINE = innodb;

Query OK, 0 rows affected (0.02 sec)