MySQL Error 1822: Failed to add foreign key constraint; missing index for contraint BUT index exists

后端 未结 2 1284
野趣味
野趣味 2021-01-03 01:55

I am trying to add an foreign key to my flightschedule table but it fails, but I do not really know why. The foreign key should reference the txtAC_tag attribute from the tb

2条回答
  •  时光说笑
    2021-01-03 02:16

    It seems you have created Composite Primary Key for the table tblAircrafts.

    If you wanted to add the composite key reference to the table tblflightschedule, you need to use the below syntax:

    alter table tblflightschedule
    add foreign key ('int Column', txtAC_tag) references tblaircrafts **(idAC, txtAC_tag);**
    

    And you have to refer two columns for adding foreign key ('int Column', txtAC_tag).

    So you can either add one more column in your tblflightschedule table or drop one column from tblaircrafts table.

提交回复
热议问题