2012-03-26 6 views
1

MySQL에서 다른 테이블을 정규화하는 스크립트를 만들려고합니다. 이 오류에MySQL 오류 150

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_codes(
airport_code char(3) not null, 
airline_code char(2) not null, 
primary key (airport_code, airline_code) 
); 

INSERT INTO airport_codes SELECT DISTINCT airport_code, airline_code 
    FROM airport_airlines; 

CREATE TABLE airport_locations(
airport_code char(3) not null, 
city varchar(20) not null, 
state char(2) not null, 
primary key (airport_code), 
constraint ap_code_fk 
    foreign key (airport_code) 
    references airport_codes(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), 
constraint al_code_fk 
    foreign key (airline_code) 
    references airport_codes(airline_code) 
); 

INSERT INTO airport_codenames SELECT DISTINCT airline_code, name 
    FROM airport_airlines; 

이 코드의 결과 : 당신이 테이블 순서를 드롭

Can't create table hw7.airport_codenames errno:150

+0

이러한 FK 관계는 거꾸로 보입니다. 일반적으로'airport_codenames'는 기본 테이블이 될 것이고'airport_codes'는 FK를'airport_codenames'에 넣을 것입니다. 마찬가지로'airport_locations'는'airport_codes'에 FK가있는 PK 테이블입니다. 둘 다 나는 다음과 같은 기능 종속성을 asuming했다 –

+0

... 이전 버전과 같다 : AIRPORT_CODE> 도시, 주 AIRLINE_CODE> 이름 – kmaz13

+0

하지만 당신은 복합 키 (airport_locations''에서 airport_code's'당 여러'airline_code'을 허용하는 경우),'airport_codenames'에서 외래 키를 분리합니다. 왜냐하면'airport_locations'의 여러 행을 가리 키기 때문입니다. 그게 당신 문제의 근원이라고 생각해. –

답변

1

은 복합 키로서 airport_codeairline_code에 대해 가능한 여러 행을 가지고 있으므로 다른 외래 키에서 참조 할 수 없습니다. FK 관계를 airport_codes으로 이동하고 airport_locationsairport_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; 
+0

오, 이제 알겠습니다. 이것은 제가 시도했던 것보다 훨씬 더 의미가 있습니다. 모든 도움을 주셔서 대단히 감사합니다! 나는 네가 두 번 이상 너를 위로 할 수 있었으면 좋겠다. 좋은 하루 되세요. – kmaz13

0

그것은 와야 아래는 내가 가지고있는 것입니다. 당신은 참조하기 전에 외래 키를 삭제

DROP TABLE IF EXISTS airport_locations; 
DROP TABLE IF EXISTS airport_codenames; 
DROP TABLE IF EXISTS airport_codes; 

: MySQL의 문서처럼

InnoDB does not permit you to drop a table that is referenced by a FOREIGN KEY constraint, unless you do SET foreign_key_checks = 0

그래서 당연히 외부 키 체크를 설정하거나에 당신의 하락 순서를 변경 말한다.

+0

음, 위의 코드를 다시 읽으십시오. 테이블이 삭제되기 바로 전에'SET foreign_key_checks = 0'이 발생합니다. –

+0

미안하지만, 외래 키 참조를 찾고있었습니다. – grifos

+0

Micheal이 말했듯이 제 스크립트의 일부분은 정확하다고 생각합니다. 시도해 줘서 고마워. 나는 아직도 이것을 이해할 수 없다. – kmaz13