首页 > 解决方案 > MySQL 错误 #1215:无法添加外键约束

问题描述

我正在尝试实现图片中显示的表格: 这张图片 (数据库方案取自http://www.databaseanswers.org/data_models/recipes/index.htm)。

但是我遇到了 Recipe_step_ingredients 表的问题。添加外键约束时,我收到 recipe_steps_id 约束的错误 #1215(不添加该约束时,我没有收到错误,我可以添加其他两个约束)。

这是用于创建表的 may 命令:

CREATE TABLE recipe_step_ingredients (
    recipe_id INTEGER,
    step_number INTEGER,
    ingredient_id INTEGER,
    amount_required VARCHAR(128),
    
    CONSTRAINT FOREIGN KEY (recipe_id) REFERENCES recipe_steps (recipe_id) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT FOREIGN KEY (step_number) REFERENCES recipe_steps (step_number) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT FOREIGN KEY (ingredient_id) REFERENCES ingredients (ingredient_id) ON DELETE CASCADE ON UPDATE CASCADE,
    
    PRIMARY KEY (recipe_id, step_number, ingredient_id)
    ) ENGINE = INNODB

有谁知道这可能是什么问题?

编辑:更改代码以匹配图片中的命名约定。此外,这是我使用的 DDL 的其余部分:

CREATE TABLE recipes (
    recipe_id INTEGER NOT NULL AUTO_INCREMENT,
    recipe_name VARCHAR(128),
    recipe_description VARCHAR(4096),
    
    PRIMARY KEY (recipe_id),
    INDEX (recipe_name)
    ) ENGINE = INNODB

CREATE TABLE recipe_steps (
    recipe_id INTEGER,
    step_number INTEGER,
    instructions VARCHAR(4096),
    
    CONSTRAINT FOREIGN KEY (recipe_id) REFERENCES recipes (recipe_id) ON DELETE CASCADE ON UPDATE CASCADE,
    
    PRIMARY KEY (recipe_id, step_number)
    ) ENGINE = INNODB

CREATE TABLE ingredient_types (
    ingredient_type_id INTEGER NOT NULL AUTO_INCREMENT,
    ingredient_type_description VARCHAR(4096),
    
    PRIMARY KEY (ingredient_type_id)
    ) ENGINE = INNODB

ALTER TABLE ingredient_types ADD COLUMN ingredient_type_name VARCHAR(128);
ALTER TABLE ingredient_types ADD INDEX(ingredient_type_name);

CREATE TABLE ingredients (
    ingredient_id INTEGER NOT NULL AUTO_INCREMENT,
    ingredient_type_id INTEGER,
    ingredient_name VARCHAR(128),
    
    CONSTRAINT FOREIGN KEY (ingredient_type_id) REFERENCES ingredient_types (ingredient_type_id) ON DELETE CASCADE ON UPDATE CASCADE,
    PRIMARY KEY (ingredient_id),
    INDEX (ingredient_name)
    ) ENGINE = INNODB

CREATE TABLE recipe_step_ingredients (
    recipe_id INTEGER,
    step_number INTEGER,
    ingredient_id INTEGER,
    amount_required VARCHAR(128),
    
    CONSTRAINT FOREIGN KEY (recipe_id) REFERENCES recipe_steps (recipe_id) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT FOREIGN KEY (step_number) REFERENCES recipe_steps (step_number) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT FOREIGN KEY (ingredient_id) REFERENCES ingredients (ingredient_id) ON DELETE CASCADE ON UPDATE CASCADE,
    
    PRIMARY KEY (recipe_id, step_number, ingredient_id)
    ) ENGINE = INNODB

标签: mysqlsql

解决方案


确保:

  • 外键引用了被引用关系的键的所有属性(你不能只引用其中的一些)。所以如果R的键是(A,B),你应该同时引用A和B。你不能只引用A或只引用B。
  • 您在外键约束中引用的属性被定义为 UNIQUE 或者是关系的主键。在 Mysql 中,这也可以通过将属性定义为 KEY 或 UNIQUE KEY 来完成。
  • 如果要引用主键,则无需在表后指明属性名称(默认为主键)。

在你的情况下,

  • 确保表 recipe_steps 有一个属性 recipe_steps_id,并且
  • 此属性是 UNIQUE KEY、KEY 或关系的主键
  • 此外,请确保已创建相应的索引(用于键)。

推荐阅读