错误代码:1822.无法添加外键约束.引用表"pwlhseis"中约束"ekdromes_ibfk_1"的缺少索引 [英] Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'ekdromes_ibfk_1' in the referenced table 'pwlhseis'

查看:226
本文介绍了错误代码:1822.无法添加外键约束.引用表"pwlhseis"中约束"ekdromes_ibfk_1"的缺少索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这两个表:

create table pwlhseis(
ma int,
hmeromhnia date,
wra time,
hmer_diek date,
ae_po int,
amka_po int,
constraint foreign key (amka_po) references pelates(amka),
primary key (ma));

并且:

create table ekdromes(
ae int,
diarkeia int,
proorismos varchar(20),
kostos float,
timh float,
afeteria varchar(20),
hm_enarkshs date,
primary key (ae));

然后:

alter table ypallhloi 
  add constraint foreign key (arithmos_up) references grafeia(arithmos_g);

alter table ekdromes 
  add constraint foreign key (ae) references pwlhseis(ae_po);

第一个更改效果很好,当我尝试运行第二个更改时,出现1822错误.有什么问题吗?

The first alter works well, when I try to run the second one i get that 1822 error. What is the problem?

推荐答案

如果您想要针对pwlhseis.ae_po的外键,则此列需要具有唯一约束.

If you want a foreign key against pwlhseis.ae_po this column needs to have a unique constraint.

这意味着:

  • 它是表的主键(不是),或者
  • 它有一个额外的独特约束.

如果要尝试第二个选项,可以运行:

If you want to try the second option, you can run:

alter table pwlhseis add constraint uq1 unique (ae_po);

然后,您可以尝试再次添加外键.

Then you can try adding the foreign key again.

这篇关于错误代码:1822.无法添加外键约束.引用表"pwlhseis"中约束"ekdromes_ibfk_1"的缺少索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆