繁体   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