CREATE TABLE上的MySQL错误,用于多对多关系

时间:2012-04-10 09:30:57

标签: mysql

我遇到一个简单的CREATE TABLE查询问题。我有两个表“'resort'(带有resortID和resortName)和'season'(带有resortID和resortName)的多对多关系。 我正在尝试使用以下两个查询之一创建加入表resort_season:

CREATE TABLE resort_season (
resortID MEDIUMINT UNSIGNED NOT NULL,
seasonID MEDIUMINT UNSIGNED NOT NULL,
FOREIGN KEY (resortID) REFERENCES resort (resortID),
FOREIGN KEY (seasonID) REFERENCES season (seasonID),
PRIMARY KEY (resortID, seasonID) NOT NULL
);

CREATE TABLE resort_season (
resortID MEDIUMINT UNSIGNED NOT NULL FOREIGN KEY REFERENCES resort (resortID),
seasonID MEDIUMINT UNSIGNED NOT NULL FOREIGN KEY REFERENCES season (seasonID),
PRIMARY KEY (resortID, seasonID) NOT NULL
);

两个查询都给我一个错误读取(在这种情况下是第二个查询): "You have an error in your syntax; check manual....for right syntax to use near'FOREIGN KEY REFERENCES resort (resortID), seasonID MEDIUMINT UNSIGNED NOT NULL F' at line 2"

我做错了什么?这让我头疼,因为我无法理解为什么会出错。

如果我只是

CREATE TABLE resort_season (
resortID MEDIUMINT UNSIGNED NOT NULL,
seasonID MEDIUMINT UNSIGNED NOT NULL,
PRIMARY KEY (resortID, seasonID) NOT NULL
);

然后它工作正常,并创建表。我不确定这张表是否会有效地参考表格度假村和季节。

1 个答案:

答案 0 :(得分:1)

从主键定义中删除NOT NULL -

CREATE TABLE resort_season(
  resortID MEDIUMINT UNSIGNED NOT NULL,
  seasonID MEDIUMINT UNSIGNED NOT NULL,
  FOREIGN KEY (resortID) REFERENCES resort (resortID),
  FOREIGN KEY (seasonID) REFERENCES season (seasonID),
  PRIMARY KEY (resortID, seasonID)
);