본문 바로가기

MySQL

MySQL Foreign Key 설정 오류 해결 (Cannot add foreign key constraint)

  • 상황
기존에 있던 테이블의 PRIMARY KEY를 FOREIGN KEY로 하는 테이블을 생성하려고 하는데 오류가 났음.
(Type 동일한 것도 확인했는데, 해당 오류코드로 검색하면 타입 확인을 잘 하라는 내용 뿐이었음)
mysql> show parent;
+------------+-------------+------+-----+-------------------+-----------------------------+
| Field      | Type        | Null | Key | Default           | Extra                       |
+------------+-------------+------+-----+-------------------+-----------------------------+
| id         | varchar(16) | NO   | PRI | NULL              |                             |
+------------+-------------+------+-----+-------------------+-----------------------------+

mysql> CREATE TABLE child(
  id VARCHAR(16) PRIMARY KEY,
  parent_id VARCHAR(16) NOT NULL,
  FOREIGN KEY (parent_id) REFERENCES parent(id)
);
ERROR 1215 (HY000): Cannot add foreign key constraint
parent.id가 child.parent_id와 형식도 동일한데... 왜 오류가 발생했을까 생각하다 떠오른 게 charset이 다른 경우 이렇게 될 수 있다는 거였어요.우선, DB 자체의 언어 설정이 어떻게 돼있나 봤더니, utf8 또는 utf8mb4였어요
mysql> status;
--------------
mysql  Ver 14.14 Distrib 5.7.32, for Linux (x86_64) using  EditLine wrapper

Connection id:		......................
Current database:	......................
Current user:		......................
SSL:			......................
Current pager:		......................
Using outfile:		......................
Using delimiter:	......................
Server version:		......................
Protocol version:	......................
Connection:		......................
Server characterset:	utf8mb4
Db     characterset:	utf8mb4
Client characterset:	utf8
Conn.  characterset:	utf8
TCP port:		......................
Uptime:			......................

Threads: 13  Questions: 32114425  Slow queries: 0  Opens: 971  Flush tables: 1  Open tables: 656  Queries per second avg: 5.619
--------------

 

그럼 parent의 charset은? utf8_general_ci
mysql> show full columns from parent;
+------------------+-----------------------+-----------------+------+-----+-------------------+-----------------------------+---------------------------------+---------+
| Field            | Type                  | Collation       | Null | Key | Default           | Extra                       | Privileges                      | Comment |
+------------------+-----------------------+-----------------+------+-----+-------------------+-----------------------------+---------------------------------+---------+
| id               | varchar(16)           | utf8_general_ci | NO   | PRI | NULL              |                             | select,insert,update,references |         |
+------------------+-----------------------+-----------------+------+-----+-------------------+-----------------------------+---------------------------------+---------+

 

즉, default charset이 utf8_general_ci 가 아니기 때문에, 별도 설정 없이는 FOREIGN KEY로 설정을 할 수가 없었다....
그래서 CREATE TABLE 문법 뒤에 charset을 지정해줘서 테이블 생성에 성공했어요
mysql> CREATE TABLE child(
  id VARCHAR(16) PRIMARY KEY,
  parent_id VARCHAR(16) NOT NULL,
  FOREIGN KEY (parent_id) REFERENCES parent(id)
) default character set utf8 collate utf8_general_ci;

 

'MySQL' 카테고리의 다른 글

MySQL 조회 결과를 다르게 표현하기  (0) 2022.04.15