실제 DB 검증MySQL 8.4.13에서 재현·해결 확인 · 2026-11-29 · 컨테이너 이미지 mysql:8.4
3780은 외래키로 잇는 두 컬럼의 타입이 같지 않다는 뜻이에요. 메시지에 두 컬럼과 제약 이름이 나오니, information_schema.columns에서 두 컬럼의 column_type(문자열이면 문자셋까지)을 비교하는 게 첫 단계예요.
타입이 정확히 같아야 해요. int와 bigint도, int와 int unsigned도 달라요. 메시지는 한국어 설정에서도 영어로 나와요.
01BIGINT PK를 INT 컬럼으로 참조
customers.id는 BIGINT AUTO_INCREMENT인데 orders.customer_id는 INT로 만들었어요. ALTER TABLE로 외래키를 추가하면 실패해요. 조회하면 bigint와 int가 나란히 보여요.
사전 준비
CREATE TABLE customers (id BIGINT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50));
CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT);Query OK, 0 rows affected (0.02 sec)
Query OK, 0 rows affected (0.01 sec)ALTER TABLE orders ADD CONSTRAINT fk_orders_customer FOREIGN KEY (customer_id) REFERENCES customers (id);ERROR 3780 (HY000): Referencing column 'customer_id' and referenced column 'id' in foreign key constraint 'fk_orders_customer' are incompatible.SELECT table_name, column_name, column_type FROM information_schema.columns WHERE table_schema = 'lab_a' AND ((table_name = 'customers' AND column_name = 'id') OR (table_name = 'orders' AND column_name = 'customer_id'));+------------+-------------+-------------+
| TABLE_NAME | COLUMN_NAME | COLUMN_TYPE |
+------------+-------------+-------------+
| customers | id | bigint |
| orders | customer_id | int |
+------------+-------------+-------------+
2 rows in set (0.00 sec)ALTER TABLE orders MODIFY customer_id BIGINT;Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0자식 컬럼을 부모 타입으로 바꾸세요: ALTER TABLE orders MODIFY customer_id BIGINT;. 그 뒤 같은 ALTER TABLE ... ADD CONSTRAINT가 성공해요.
ALTER TABLE orders ADD CONSTRAINT fk_orders_customer FOREIGN KEY (customer_id) REFERENCES customers (id);Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 002INT UNSIGNED PK를 일반 INT로 참조
categories.id는 int unsigned이고, 새 products.category_id는 그냥 INT예요. 둘 다 4바이트 정수지만 외래키에서는 부호 있음/없음이 다른 타입이에요.
사전 준비
CREATE TABLE categories (id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50));Query OK, 0 rows affected (0.02 sec)CREATE TABLE products (id INT PRIMARY KEY, category_id INT, FOREIGN KEY (category_id) REFERENCES categories (id));ERROR 3780 (HY000): Referencing column 'category_id' and referenced column 'id' in foreign key constraint 'products_ibfk_1' are incompatible.SELECT column_name, column_type FROM information_schema.columns WHERE table_schema = 'lab_a' AND table_name = 'categories' AND column_name = 'id';+-------------+--------------+
| COLUMN_NAME | COLUMN_TYPE |
+-------------+--------------+
| id | int unsigned |
+-------------+--------------+
1 row in set (0.00 sec)자식 컬럼도 INT UNSIGNED로 만들면 CREATE TABLE이 성공해요.
CREATE TABLE products (id INT PRIMARY KEY, category_id INT UNSIGNED, FOREIGN KEY (category_id) REFERENCES categories (id));Query OK, 0 rows affected (0.02 sec)03문자열 컬럼의 문자셋이 다름 (예전 latin1 테이블, 새 utf8mb4 테이블)
depts는 예전 DB처럼 DEFAULT CHARSET = latin1로 만들었어요. 새 emps는 데이터베이스 기본값 utf8mb4 / utf8mb4_0900_ai_ci로 만들어져요. 둘 다 VARCHAR(10)이지만 문자셋이 달라서 외래키가 거부돼요.
사전 준비
CREATE TABLE depts (code VARCHAR(10) PRIMARY KEY, name VARCHAR(50)) DEFAULT CHARSET = latin1;Query OK, 0 rows affected (0.02 sec)CREATE TABLE emps (id INT PRIMARY KEY, dept_code VARCHAR(10), FOREIGN KEY (dept_code) REFERENCES depts (code));ERROR 3780 (HY000): Referencing column 'dept_code' and referenced column 'code' in foreign key constraint 'emps_ibfk_1' are incompatible.SELECT table_name, character_set_name, collation_name FROM information_schema.columns WHERE table_schema = 'lab_a' AND table_name = 'depts' AND column_name = 'code';
SELECT @@character_set_database, @@collation_database;+------------+--------------------+-------------------+
| TABLE_NAME | CHARACTER_SET_NAME | COLLATION_NAME |
+------------+--------------------+-------------------+
| depts | latin1 | latin1_swedish_ci |
+------------+--------------------+-------------------+
1 row in set (0.00 sec)
+--------------------------+----------------------+
| @@character_set_database | @@collation_database |
+--------------------------+----------------------+
| utf8mb4 | utf8mb4_0900_ai_ci |
+--------------------------+----------------------+
1 row in set (0.00 sec)ALTER TABLE depts CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0부모 테이블을 같은 문자셋으로 바꾸세요: ALTER TABLE depts CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;. 그 뒤 같은 CREATE TABLE이 성공해요. CONVERT TO는 테이블 전체를 다시 쓰니 큰 테이블이면 시간을 고려하세요.
CREATE TABLE emps (id INT PRIMARY KEY, dept_code VARCHAR(10), FOREIGN KEY (dept_code) REFERENCES depts (code));Query OK, 0 rows affected (0.01 sec)자주 묻는 질문
Referencing column and referenced column are incompatible 해결 방법은?
information_schema.columns에서 두 컬럼의 column_type, character_set_name, collation_name을 비교하고 자식 컬럼을 부모와 똑같이 맞추세요.
INT 컬럼으로 BIGINT PK를 참조할 수 있나요?
안 돼요. 실험에서 INT→BIGINT, INT→INT UNSIGNED 모두 3780이 났어요. 타입과 UNSIGNED 여부를 똑같이 맞추세요.
둘 다 VARCHAR(10)인데 왜 3780이 나나요?
문자셋이 달라서예요. 예전 테이블은 latin1, 새 테이블은 utf8mb4인 경우가 흔해요. 한쪽을 변환해 같은 문자셋으로 맞추세요.
검증 방법
시나리오마다 새 DB 사용자를 만들어 시작해요. 스크립트가 사전 준비 → 에러 재현(에러 코드 일치 확인) → 해결 → 원래 문장 재실행 순서로 실행하고, 화면의 출력은 DB가 실제로 낸 결과를 스크립트가 그대로 저장한 거예요.
lab_a, lab_b는 테스트용 일반 사용자(같은 이름의 데이터베이스가 기본, 그 안에서 모든 권한), root는 관리자예요. MySQL 8.4.13