簡體   English   中英

mysql錯誤150:無法創建表

[英]mysql error 150: cannot create table

我在mysql中遇到此錯誤no 150問題,我知道存在討論該問題的問題,但我仍然找不到我錯了的地方。 這是我要創建的數據庫:

create table business (
    ident varchar(40) NOT NULL,
    name varchar(50) NOT NULL,
    rating INT UNSIGNED NOT NULL,
    PRIMARY KEY(ident)
) ENGINE=InnoDB;

create table deals (
    business_id varchar(40) NOT NULL,
    deals_id varchar(20) NOT NULL,
    deals_title varchar(50) NOT NULL,
    PRIMARY KEY (business_id, deals_id),
    FOREIGN KEY (business_id) REFERENCES business(ident) ON DELETE CASCADE
) ENGINE=InnoDB;

create table d_options (
    business_id varchar(40) NOT NULL,
    dealid varchar(20) NOT NULL,
    option_title varchar(40) NOT NULL,
    PRIMARY KEY(business_id, dealid, option_title), 
    FOREIGN KEY(business_id) REFERENCES business(ident) ON DELETE CASCADE,
    FOREIGN KEY(dealid) REFERENCES deals(deals_id) 
) ENGINE=InnoDB;

我收到錯誤消息:錯誤1005(HY000):無法創建表“ test.d_options”(錯誤號:150)

我知道要滿足外鍵約束,根據mysql文檔,父表中應該有一個索引,但是我認為默認情況下主鍵上有索引。

innodb狀態的結果是:

120530  0:47:48 Error in foreign key constraint of table test/d_options:
FOREIGN KEY(dealid) REFERENCES deals(deals_id) 
) ENGINE=InnoDB:
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.1/en/innodb-foreign-key-constraints.html
for correct foreign key definition.

任何幫助都適用。

您在(business_id, deal_id)上有一個復合主鍵(business_id, deal_id)並且它們被成對索引,但是要滿足FK的需要,您只需要在deal_id上另外一個索引:

create table deals (
    business_id varchar(40) NOT NULL,
    deals_id varchar(20) NOT NULL,
    deals_title varchar(50) NOT NULL,
    PRIMARY KEY (business_id, deals_id),
    FOREIGN KEY (business_id) REFERENCES business(ident) ON DELETE CASCADE,
    /* Add an index on deals_id, separate from the compound PK */
    INDEX idx_deals_id (deals_id)
) ENGINE=InnoDB;

暫無
暫無

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

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