我在 MySQL 8.0 中创建了一个名为 hr 的模式。我正在创建两个表:一个是locations,另一个是departments,它有一个引用表locations的外键。
create table hr.locations(
location_id varchar(4) not null unique,
street_address varchar(25) not null,
country_id char(2) not null unique,
primary key(location_id),
foreign key(country_id) references countries(country_id)
);
create table hr.departments(
department_id varchar(4) not null unique,
department_name varchar(30) not null,
manager_id varchar(9) not null unique,
location_id varchar(4) not null unique,
primary key(department_id),
foreign key(location_id) references locations(location_id)
);
Run Code Online (Sandbox Code Playgroud)
处理的时候出现这个错误:
错误代码:3780。引用列“location_id”和外键约束“departments_ibfk_1”中引用的列“location_id”不兼容。
两个表中 location_id 的数据类型相同。我找不到错误。