【发布时间】:2021-11-14 06:50:22
【问题描述】:
我在 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)
);
处理时出现此错误:
错误代码:3780。引用列“location_id”并引用 外键约束“departments_ibfk_1”中的“location_id”列 不兼容。
两个表中 location_id 的数据类型相同。我找不到错误。
【问题讨论】:
-
我可以完美地执行查询吗?两个表都被创建并且外键存在?我能想到的唯一另一件事是确保两个表具有相同的排序规则。
-
为什么
departments.location_id声明为UNIQUE?一个地点不能有多个部门吗?
标签: mysql foreign-keys primary-key