1

我似乎无法在这里创建第三个表。这是怎么回事?我收到通用无法创建表 errno 150 消息。似乎与外键有关

餐桌食谱

CREATE TABLE recipe(
    recipe_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    name VARCHAR(40) NOT NULL,
    description VARCHAR(40) NOT NULL,
    PRIMARY KEY (recipe_id)
)
ENGINE=INNODB CHARACTER SET utf8 COLLATE utf8_swedish_ci;

表成分类型

CREATE TABLE ingredient_type(
    ingredient_type_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    ingredient_type VARCHAR(40) NOT NULL,
    description VARCHAR(40) NOT NULL,
    PRIMARY KEY (ingredient_type_id)
)
ENGINE=INNODB CHARACTER SET utf8 COLLATE utf8_swedish_ci;

餐桌配料

CREATE TABLE ingredient(
    ingredient_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
    ingredient_type_id INT(10) NOT NULL,
    name VARCHAR(40) NOT NULL,
    brand_name VARCHAR(40) NOT NULL,
    FOREIGN KEY (ingredient_type_id) REFERENCES ingredient_type (ingredient_type_id),
    PRIMARY KEY (ingredient_id)
)
ENGINE=INNODB CHARACTER SET utf8 COLLATE utf8_swedish_ci;
4

1 回答 1

1

Corresponding columns in the foreign key and the referenced key must have similar internal data types inside InnoDB so that they can be compared without a type conversion. The size and sign of integer types must be the same. The length of string types need not be the same. For nonbinary (character) string columns, the character set and collation must be the same.

Source: FOREIGN KEY Constraints in the MySQL manual

The problem in your code is that ingredient_type.ingredient_type_id is unsigned, but ingredient.ingredient_type_id is not.

于 2012-09-18T15:26:03.510 回答