简体   繁体   中英

MySQL foreign key issue

I am having a problem trying to load my database into MySQL as I am having a few errors with my foreign keys. Things I have tried to do to fix this issue is: - Putting foreign keys after the primary keys Eg:

CREATE TABLE IF NOT EXISTS Passenger (
.......
PRIMARY KEY(tNum),
FOREIGN KEY (fNum) REFERENCES Flights(fNum),
FOREIGN KEY (fDate) REFERENCES Flights(fDate),
FOREIGN KEY (sCity) REFERENCES Flights(sCity),
FOREIGN KEY (eCity) REFERENCES Flights(eCity)
);

- Bunching the foreign keys in the format of Eg:

ALTER TABLE Passenger ADD FOREIGN KEY (fNum, fDate, sCity, eCity) REFERENCES Flights(fNum, fDate, sCity, eCity);

The error I get is:

1005 - Can't create table 'airline.#sql-1d7_7c' (errno: 150)

My full code is:

DROP DATABASE IF EXISTS airline;
CREATE DATABASE IF NOT EXISTS airline;
USE airline;

CREATE TABLE IF NOT EXISTS Flights (
    fNum char(6) not null,
    pID char(4) not null,
    fDate DATE not null,
    eDate DATE  not null,
    sTime char(4) not null,
    lTime char(4) not null,
    sOStart char(4) null,
    sOEnd char(4) null,
    sCity varchar(30) not null,
    eCity varchar(30) not null,
    sOCity varchar(30),
    sNum char(5) not null,
    PRIMARY KEY (fNum)
);

CREATE TABLE IF NOT EXISTS Passenger (
    tNum char(4) not null,
    dPurch DATE not null,
    pMethod varchar(30) not null,
    fNum char(6) not null,
    fDate DATE not null,
    sCity varchar(30) not null,
    eCity varchar(30) not null,
    tType varchar(30) not null,
    Price decimal(4,2) not null,
    iType varchar(30) not null,
    idNum char(8) not null,
    fName varchar(30) not null,
    lName varchar(30) not null,
    Sex char(1) not null,
    pAddress varchar(30) not null,
    pPhone char(8) not null,
    pEmail varchar(30) not null,
    PRIMARY KEY(tNum)
);

CREATE TABLE IF NOT EXISTS Planes (
    pID char(4) not null,
    pType char(3) not null,
    pDesc varchar(30) not null,
    pRange char(4) not null,
    Capacity char(3) not null,
    mDate DATE not null,
    pDate DATE not null,
    sDate DATE not null,
    PRIMARY KEY (pID)
);

CREATE TABLE IF NOT EXISTS Staff (
    sNum char(5) not null,
    sName varchar(30) not null,
    sDOB DATE not null,
    sAddress varchar(30) not null,
    pCompany varchar(30) ,
    pStart DATE ,
    pEnd DATE ,
    jID char(1) not null,
    PRIMARY KEY (sNum)
);

CREATE TABLE IF NOT EXISTS Emergency (
    eID char(5) not null,
    sNum char(5) not null,
    eName varchar(30) not null,
    eAddress varchar(30) not null,
    ePhone char(8) not null,
    eEmail varchar(30) not null,
    eRelationship varchar(30) not null, 
    PRIMARY KEY(eID)
);

CREATE TABLE IF NOT EXISTS Pilot (
    sNum char(5) not null,
    pID char(4) not null,
    cDate DATE not null,
    jID char(1) not null,
    PRIMARY KEY(jID)
);

CREATE TABLE IF NOT EXISTS Attendant (
    sNum char(5) not null,
    tSDate Date not null,
    tFDate Date not null,
    tDesc Varchar(30) not null,
    jID Char(1) not null,
    PRIMARY KEY(jID)
);

ALTER TABLE Flights ADD FOREIGN KEY (pID) REFERENCES Planes(pID);
ALTER TABLE Flights ADD FOREIGN KEY (sNum) REFERENCES Staff(sNum);

ALTER TABLE Passenger ADD FOREIGN KEY (fNum) REFERENCES Flights(fNum);
ALTER TABLE Passenger ADD FOREIGN KEY (fDate) REFERENCES Flights(fDate);
ALTER TABLE Passenger ADD FOREIGN KEY (sCity) REFERENCES Flights(sCity);
ALTER TABLE Passenger ADD FOREIGN KEY (eCity) REFERENCES Flights(eCity);

ALTER TABLE Emergency ADD FOREIGN KEY (sNum) REFERENCES Staff(sNum);

ALTER TABLE Pilot ADD FOREIGN KEY (sNum) REFERENCES Staff(sNum);
ALTER TABLE Pilot ADD FOREIGN KEY (pID) REFERENCES Planes(pID);
ALTER TABLE Pilot ADD FOREIGN KEY (jID) REFERENCES Staff(jID);

ALTER TABLE Attendant ADD FOREIGN KEY (sNum) REFERENCES Staff(sNum);
ALTER TABLE Attendant ADD FOREIGN KEY (jID) REFERENCES Staff(jID);

For me this is the one it fails on first

ALTER TABLE Passenger ADD FOREIGN KEY (fDate) REFERENCES Flights(fDate);

Try adding an index on Flights.fdate first, then doing the foreign key and doing that for additional references until it works. Let me know if that works for you, it did for me.

By the way you might want to reconsider your schema there. Joins and relations should really be done on integers only. That schema will bog down fast as it grows. Also, and maybe this is just personal preference but I prefer first_name to fName. Easier to read for other developers, this isn't 1986, we can have human readbable names now :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM