1

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

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)
);
4

1 回答 1

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)
);
于 2012-07-03T19:21:32.613 回答