2012-03-26 12 views
1

Ich versuche ein Skript zu erstellen, um eine andere Tabelle in MySQL zu normalisieren. Im Folgenden finden Sie, was ich habe:MySQL Fehler 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; 

Dieser Code führt zu diesem Fehler:

Can't create table hw7.airport_codenames errno:150

+0

Diese FK-Beziehungen scheinen rückwärts zu sein. Normalerweise wäre 'airport_codenames' die Haupttabelle und' airport_codes' würde die FK in 'airport_codenames' haben. Ebenso wäre 'airport_locations' die PK-Tabelle mit einem FK in' airport_codes'. Beide scheinen rückwärts ... –

+0

Ich war asuming die folgenden funktionalen Abhängigkeiten: AIRPORT_CODE> Stadt, Staat Airline_code> Name – kmaz13

+0

Aber wenn Sie erlauben, mehrere 'airline_code' pro' airport_code's in 'airport_locations' (von zusammengesetzten Schlüssel), die den Fremdschlüssel von 'airport_codenames' abbricht, da er auf mehrere Zeilen in' airport_locations' verweisen würde. Ich denke, das ist die Ursache Ihres Problems. –

Antwort

1

Da airport_codes mehrere mögliche Zeilen pro airport_code und airline_code (als zusammengesetzter Schlüssel) hat, kann sie nicht von anderen Fremdschlüsseln referenziert werden. Verschieben Sie die FK-Beziehungen in airport_codes und zeigen Sie auf airport_locations und 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; 
+0

Oh, ich sehe jetzt. Das macht viel mehr Sinn als das, was ich versuchte. Vielen Dank für Ihre Hilfe! Ich wünschte, ich könnte dich mehr als einmal verpfänden. Haben Sie einen guten Tag. – kmaz13

0

Es sollte kommen von Ihnen Tabelle Auftrag. Wie mysql doc sagen

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

So die Fremdschlüsselprüfung gesetzt zu Recht oder Ihre Drop, um in ändern:

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

Sie Fremd Drop Schlüssel vor der Referenz.

+0

Ähm, lies den obigen Code nochmal. 'SET foreign_key_checks = 0' passiert unmittelbar bevor die Tabellen gelöscht werden. –

+0

Sorry mein schlecht, war auf der Suche nach Ihrem ausländischen Schlüssel Referenz – grifos

+0

Wie Michael sagte, ich denke, ich habe diesen Teil meines Skripts korrekt. Danke fürs ausprobieren. Ich kann das immer noch nicht herausfinden. – kmaz13

Verwandte Themen