MySQL错误150(外键问题?)

时间:2012-07-03 19:17:59

标签: mysql foreign-keys

我正在尝试创建一系列具有多个外键关系的表。两个表中的第一个具有两列主键,一旦由我们指定,另一个由制造商指定。尝试第三个查询时发生错误。我不确定这里出了什么问题,所以任何帮助都会得到真诚的感激。

SQL

CREATE TABLE rugs (
    id INTEGER NOT NULL AUTO_INCREMENT,
    manufacturer_id INTEGER NOT NULL,
    name VARCHAR(255),
    description TEXT,
    PRIMARY KEY (id, manufacturer_id)
);

CREATE TABLE carpets (
    id INTEGER NOT NULL AUTO_INCREMENT,
    manufacturer_id INTEGER NOT NULL,
    name VARCHAR(255),
    warranty TEXT,
    description TEXT,
    fiber_name VARCHAR(255),
    brand_name VARCHAR(255),
    texture_name VARCHAR(255),
    PRIMARY KEY (id, manufacturer_id)
);

CREATE TABLE carpet_styles (
    id INTEGER NOT NULL AUTO_INCREMENT,
    carpet_id INTEGER,
    style_name VARCHAR(255),
    PRIMARY KEY (id),
    FOREIGN KEY (carpet_id) REFERENCES carpets (manufacturer_id)
);

CREATE TABLE carpet_colors (
    id INTEGER NOT NULL AUTO_INCREMENT,
    carpet_id INTEGER,
    color_name VARCHAR(255),
    color_category VARCHAR(255),
    PRIMARY KEY (id),
    FOREIGN KEY (carpet_id) REFERENCES carpets (manufacturer_id)
);

CREATE TABLE carpet_custom_names (
    id INTEGER NOT NULL AUTO_INCREMENT,
    carpet_id INTEGER,
    custom_name VARCHAR(255),
    PRIMARY KEY (id),
    FOREIGN KEY (carpet_id) REFERENCES carpets (manufacturer_id)
);

CREATE TABLE rug_styles (
    id INTEGER NOT NULL AUTO_INCREMENT,
    rug_id INTEGER,
    style_name VARCHAR(255),
    PRIMARY KEY (id),
    FOREIGN KEY (rug_id) REFERENCES rugs (manufacturer_id)
);

CREATE TABLE rug_colors (
    id INTEGER NOT NULL AUTO_INCREMENT,
    rug_id INTEGER,
    color_name VARCHAR(255),
    color_category VARCHAR(255),
    PRIMARY KEY (id),
    FOREIGN KEY (rug_id) REFERENCES rugs (manufacturer_id)
);

CREATE TABLE rug_shapes (
    id INTEGER NOT NULL AUTO_INCREMENT,
    rug_id INTEGER,
    shape_name VARCHAR(255),
    shape_category VARCHAR(255),
    PRIMARY KEY (id),
    FOREIGN KEY (rug_id) REFERENCES rugs (manufacturer_id)
);

CREATE TABLE catalog_contents (
    id INTEGER NOT NULL AUTO_INCREMENT,
    catalog_id INTEGER,
    carpet_id INTEGER,
    rug_id INTEGER,
    PRIMARY KEY (id),
    FOREIGN KEY (rug_id) REFERENCES rugs (id),
    FOREIGN KEY (carpet_id) REFERENCES carpets (id),
    FOREIGN KEY (catalog_id) REFERENCES catalogs (id)
);

1 个答案:

答案 0 :(得分:2)

这似乎是因为虽然carpets.manufacturer_id是复合键的一部分,但它没有自己的索引。您必须与复合PRIMARY KEY分开创建索引。 rugs.manufacturer_id

也是如此
CREATE TABLE rugs (
    id INTEGER NOT NULL AUTO_INCREMENT,
    manufacturer_id INTEGER NOT NULL,
    name VARCHAR(255),
    description TEXT,
    /* manufacturer_id gets its own index */
    PRIMARY KEY (id, manufacturer_id),
    INDEX idx_rugs_manufacturer_id (manufacturer_id)
);


CREATE TABLE carpets (
    id INTEGER NOT NULL AUTO_INCREMENT,
    manufacturer_id INTEGER NOT NULL,
    name VARCHAR(255),
    warranty TEXT,
    description TEXT,
    fiber_name VARCHAR(255),
    brand_name VARCHAR(255),
    texture_name VARCHAR(255),
    PRIMARY KEY (id, manufacturer_id),
    /* manufacturer_id gets its own index */
    INDEX idx_manufacturer_id (manufacturer_id)
);