【问题标题】:ERROR 1822 (HY000): Failed to add the foreign key constraint. Missing index for constraint problem in SQL [duplicate]ERROR 1822 (HY000): 添加外键约束失败。 SQL中的约束问题缺少索引[重复]
【发布时间】:2020-11-07 08:16:15
【问题描述】:

我通过创建 3 个表 (classes, lectures, taking) 来创建数据库,然后更改表以添加 foreign key

但是,我不断收到以下错误: ERROR 1822 (HY000): Failed to add the foreign key constraint. Missing index for constraint

我似乎没有发现代码有任何问题,那么可能是什么问题? (我使用的是mySQL Workbench。我尝试复制粘贴为txt文件,因为我虽然可能是排序规则问题,但仍然会出现同样的问题)

代码如下:

CREATE TABLE classes (
    course_id VARCHAR(8),
    classes_id VARCHAR(10),
    semester VARCHAR(10),
    year VARCHAR(10),
    PRIMARY KEY (course_id, classes_id, semester, year)
);

CREATE TABLE taking (
    student_id VARCHAR(8),
    course_id VARCHAR(10),
    classes_id VARCHAR(10),
    semester VARCHAR(10),
    year VARCHAR(10),
    grade char(1),
    PRIMARY KEY (student_id, course_id, semester, year)
);

CREATE TABLE lectures (
    professor_id VARCHAR(8),
    course_id VARCHAR(10),
    classes_id VARCHAR(10),
    semester VARCHAR(10),
    year VARCHAR(10),
    PRIMARY KEY (professor_id, course_id, semester, year)
);

ALTER TABLE taking ADD CONSTRAINT consTAKE3 FOREIGN KEY(classes_id) REFERENCES classes(classes_id) ON DELETE CASCADE;
ALTER TABLE taking ADD CONSTRAINT consTAKE4 FOREIGN KEY(semester) REFERENCES classes(semester) ON DELETE CASCADE;
ALTER TABLE taking ADD CONSTRAINT consTAKE5 FOREIGN KEY(year) REFERENCES classes(year) ON DELETE CASCADE;

ALTER TABLE lectures ADD CONSTRAINT consLEC3 FOREIGN KEY(classes_id) REFERENCES classes(classes_id) ON DELETE CASCADE;
ALTER TABLE lectures ADD CONSTRAINT consLEC4 FOREIGN KEY(semester) REFERENCES classes(semester) ON DELETE CASCADE;
ALTER TABLE lectures ADD CONSTRAINT consLEC5 FOREIGN KEY(year) REFERENCES classes(year) ON DELETE CASCADE;

错误如下:

ERROR 1822 (HY000): Failed to add the foreign key constraint. Missing index for constraint 'consTAKE3' in the referenced table 'classes'
ERROR 1822 (HY000): Failed to add the foreign key constraint. Missing index for constraint 'consTAKE4' in the referenced table 'classes'
ERROR 1822 (HY000): Failed to add the foreign key constraint. Missing index for constraint 'consTAKE5' in the referenced table 'classes'
ERROR 1822 (HY000): Failed to add the foreign key constraint. Missing index for constraint 'consLEC3' in the referenced table 'classes'
ERROR 1822 (HY000): Failed to add the foreign key constraint. Missing index for constraint 'consLEC4' in the referenced table 'classes'
ERROR 1822 (HY000): Failed to add the foreign key constraint. Missing index for constraint 'consLEC5' in the referenced table 'classes'

现在,我正在尝试使用 ysql –u root –pcmd 执行此操作。不过,同样的问题正在发生。

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    由于您在所有表中都有复合主键,因此所有引用还必须包含表中的所有复合键列才能正确映射。

    除非您对每个表的复合主键列都有唯一约束/索引。

    要在表中创建关系,请尝试对 dbo.taking 进行此操作,其他表也遵循类似的方法

    ALTER TABLE dbo.taking 
      ADD CONSTRAINT FK_taking_classes
      FOREIGN KEY(course_id, classes_id, semester, year) REFERENCES classes(course_id, 
    classes_id, semester, year)
    

    【讨论】:

    • 两个表中course_id的字符长度不同,导致没有创建FK。请将其设置为 taking 表中的 varchar(8) 与 classes 相同。它肯定会解决问题。我已经使用提供的查询创建了表并且还具有 FK。
    【解决方案2】:

    两个表中course_id的字符长度不同,导致没有创建FK。请在与classes 相同的taking 表中将其设置为varchar(8)。它肯定会解决问题。我已经使用提供的查询创建了表并且还具有 FK。

    然后按照上面提到的查询:

    由于您在所有表中都有一个复合主键,所以所有 引用还必须包括所有复合键列 表来正确映射。

    除非您在复合主键上有唯一的约束/索引 每个表上的列。

    要在表格中创建关系,请尝试 dbo.taking,关注 其他表也采用类似方法

      ALTER TABLE dbo.taking 
      ADD CONSTRAINT FK_taking_classes
      FOREIGN KEY(course_id, classes_id, semester, year) REFERENCES classes(course_id, 
      classes_id, semester, year)
    

    【讨论】:

    • 不,这是不正确的。在外键的情况下允许 varchars 的长度差异。错误消息也非常明确地指出缺少索引。
    猜你喜欢
    • 2021-06-11
    • 2014-12-07
    • 2016-05-23
    • 2019-02-27
    • 2017-09-16
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多