Поскольку airport_codes
имеет несколько возможных строк на airport_code
и airline_code
(как составной ключ), другие внешние ключи не могут ссылаться на него.Переместите отношения FK в airport_codes
, указывая на airport_locations
и airport_codenames
.
USE hw7;
SET foreign_key_checks = 0;
DROP TABLE IF EXISTS airport_codes;
DROP TABLE IF EXISTS airport_locations;
DROP TABLE IF EXISTS airport_codenames;
SET foreign_key_checks = 1;
CREATE TABLE airport_locations(
airport_code char(3) not null,
city varchar(20) not null,
state char(2) not null,
primary key (airport_code)
);
INSERT INTO airport_locations SELECT DISTINCT airport_code, city, state
FROM airport_airlines;
CREATE TABLE airport_codenames(
airline_code char(2) not null,
name varchar(20) not null,
primary key (airline_code)
);
INSERT INTO airport_codenames SELECT DISTINCT airline_code, name
FROM airport_airlines;
/* airport_codes moved after the other 2 tables, and FKs defined here */
CREATE TABLE airport_codes(
airport_code char(3) not null,
airline_code char(2) not null,
primary key (airport_code, airline_code),
/* FK relationships are defined here, rather than in the other tables,
since the PKs for airport_code and airline_code are defined in the
other tables.
*/
constraint ap_code_fk
foreign key (airport_code)
references airport_locations (airport_code),
constraint al_code_fk
foreign key (airline_code)
references airport_codenames (airline_code)
);
INSERT INTO airport_codes SELECT DISTINCT airport_code, airline_code
FROM airport_airlines;