Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 错误1822(HY000):未能添加外键约束。SQL中缺少约束问题的索引_Mysql_Sql - Fatal编程技术网

Mysql 错误1822(HY000):未能添加外键约束。SQL中缺少约束问题的索引

Mysql 错误1822(HY000):未能添加外键约束。SQL中缺少约束问题的索引,mysql,sql,Mysql,Sql,我创建了一个数据库,创建了3个表(类、讲座、取),然后修改表以添加外键 但是,我不断遇到以下错误: 错误1822(HY000):未能添加外键约束。缺少约束的索引 我似乎没有发现代码有任何问题,那么问题出在哪里?? (我使用的是mySQL Workbench。我尝试将其复制和粘贴为txt文件,因为我认为这可能是排序问题,但仍然会出现相同的问题) 代码如下: CREATE TABLE classes ( course_id VARCHAR(8), classes_id VARCHAR

我创建了一个数据库,创建了3个表(
类、讲座、取
),然后修改表以添加
外键

但是,我不断遇到以下错误:
错误1822(HY000):未能添加外键约束。缺少约束的索引

我似乎没有发现代码有任何问题,那么问题出在哪里?? (我使用的是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'
错误如下:

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–p
cmd
执行此操作。但是,同样的问题仍然存在。

因为所有表中都有一个复合主键,所以所有引用还必须包括表中的所有复合键列才能正确映射

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

要在表中创建关系,请对
dbo尝试此操作。对于其他表,也可以采用类似的方法

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

两个表中
课程id
的字符长度不同,导致不创建FK。请将其设置为
varchar(8)
中的
taking
表中的
classes
。这肯定会解决问题。我已经使用提供的查询创建了表和FKs

然后按照上述查询进行操作:


由于所有表中都有一个复合主键,所以 引用还必须包括 要正确映射的表

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

要在表中创建关系,请对
dbo尝试此操作
其他表也采用类似的方法

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

两个表中
课程id
的字符长度不同,导致不创建FK。请在
taking
表中将其设置为varchar(8),该表与
类相同。这肯定会解决问题。我已经使用提供的查询创建了表和FKs。不,这是不正确的。在外键的情况下,允许使用varchars的长度差异。错误消息也非常明确地说明了缺少索引。