I created a schema in MySQL 8.0 named hr. I'm creating two tables: one is locations and the other one departments, which has a foreign key referencing the table 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)
);
When processing it this error appears:
Error Code: 3780. Referencing column 'location_id' and referenced column 'location_id' in foreign key constraint 'departments_ibfk_1' are incompatible.
Data type is the same for location_id in both tables. I can't find the mistake.
departments.location_id
declaredUNIQUE
? Can't you have more than one department at a location? – Surbased