簡體   English   中英

MySQL錯誤:1005無法創建表“ myTable”(錯誤號:150)

[英]MySQL ERROR: 1005 Can't create table 'myTable' (errno : 150)

我已經閱讀了許多有關此錯誤的文章,但是沒有一個解決方案能夠解決該問題(假設我已經正確嘗試了它們)。

這是導致錯誤的代碼:

CREATE TABLE season
(
  id          smallint unsigned NOT NULL auto_increment,
  title       varchar(25) NOT NULL,

  PRIMARY KEY (id)
);

CREATE INDEX seasonId ON season(id);

DROP TABLE IF EXISTS event;
CREATE TABLE event
(
  id           smallint unsigned NOT NULL auto_increment,
  title        varchar(255) NOT NULL,
  season_id    smallint NOT NULL,

  PRIMARY KEY (id),
  FOREIGN KEY (season_id) REFERENCES season(id)
  ON UPDATE RESTRICT ON DELETE RESTRICT
);

因此,根據錯誤,我的外鍵聲明有問題。 但是我已經在機器上運行了這段代碼,沒有任何問題,並且它也可以在我的Linux機器上完美運行(我目前在Windows 7下工作)。

這是SHOW ENGINE INNODB STATUS的輸出:

------------------------
LATEST FOREIGN KEY ERROR
------------------------
120229 17:43:28 Error in foreign key constraint of table fcrcontent/event:
FOREIGN KEY (season_id) REFERENCES season(id)
  ON UPDATE RESTRICT ON DELETE RESTRICT
):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
for correct foreign key definition.

我還嘗試在一個新的數據庫上運行腳本,但沒有成功。

這是show create table season的輸出:

| season | CREATE TABLE `season` (
  `id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(25) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `seasonId` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |

由於season.id是未簽名的,所以event.season_id也需要是未簽名的:

CREATE TABLE event
(
  id           smallint unsigned NOT NULL auto_increment,
  title        varchar(255) NOT NULL,
  season_id    smallint unsigned NOT NULL,

  PRIMARY KEY (id),
  FOREIGN KEY (season_id) REFERENCES season(id)
  ON UPDATE RESTRICT ON DELETE RESTRICT
);

對於“無法創建表'X'(errno:150)”的問題,請查看此頁面

他指出的最重要的事情是,在發生這種情況后立即從命令行登錄到您的mysql服務器並鍵入:

顯示引擎的INNODB狀態;

這會吐出各種各樣的廢話,但是最重要的是,您應該看到標題為“最新外來鍵錯誤”的部分,在該部分中您將看到實際的問題,並說出這樣的話:


最新外鍵錯誤

121114 16:22:57表dgweb / company的外鍵約束錯誤:被引用表中沒有索引,該索引將包含列作為第一列,或者被引用表中的數據類型與表中的數據類型不匹配。 約束:,約束“fk_company_wf_reporting_info”外鍵(“wf_reporting_info”)參考>“wf_reporting_info”(“wf_reporting_info_id”)在表的外鍵索引是“fk_company_wf_reporting_info”見http://dev.mysql.com/doc/refman /5.5/zh-CN/innodb-foreign-key-constraints.html,以獲取正確的外鍵定義。

然后,您將知道到底是什么錯了:-)。

由於您尚未顯示show create table season的輸出,因此我不確定您的問題是什么。

但是, MySQL文檔有一個清單:

  • 相應的列必須具有相似的內部數據類型。 [..] 整數類型的大小和符號必須相同

  • InnoDB需要外鍵和引用鍵上的索引。

  • [...]在引用表中,必須有一個索引,其中引用列以相同的順序列為第一列。

(強調我的)。

請確保您的桌子符合這些條件; 如果仍然失敗,請閱讀docs頁面上的其余標准。

這個 :

`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,

需要與此完全相同的類型:

season_id    smallint NOT NULL,

所以改成它

smallint(5)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM