[Mysql] error code: 1822. failed to add the foreign key constraint. 오류
728x90
SMALL

에러메세지

error code: 1822. failed to add the foreign key constraint

 

 

 

참고

https://stackoverflow.com/questions/43511183/mysql-error-1822-failed-to-add-foreign-key-constraint-missing-index-for-contra

 

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

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 tblAircraft table which is...

stackoverflow.com

 

 

relation을 생성할 때, 참조하려는 테이블의 column이 unique하거나 primary해야한다.

(= 참조되는 column은 PK이거나 UK(unique key)여야 한다.)

 

 

728x90

 

 

 

해결

나의 경우는,

FOREIGN KEY (b) REFERENCES br (b),
FOREIGN KEY (m) REFERENCES mo (m)

여기서 실수 했다.

 

br table의 PK는 b가 맞으나, mo table의 PK는 (b,m)이었다.

그렇다면 새로 생성하는 테이블의 릴레이션에서

mo table과 br table의 기본키를 참조하려면

 

FOREIGN KEY (b) REFERENCES br (b),
FOREIGN KEY (b, m) REFERENCES mo (b, m),

 

이렇게 적어줘야한다.

728x90
LIST