无法创建表格' xxx' (错误:150)

时间:2014-03-22 00:39:43

标签: mysql sql foreign-keys

以下是相关的sql语句:

CREATE TABLE VISIT
(
Date_of_Visit           DATE                NOT NULL,
Patient_Username        VARCHAR(20)     NOT NULL,
Doctor_Username     VARCHAR(20)     NOT NULL,
Diastolic_Blood_Pressure    INTEGER,
Systolic_Blood_Pressure INTEGER,
Billing_Amount      INTEGER         NOT NULL,
PRIMARY KEY (Date_of_Visit, Doctor_Username, Patient_Username),
FOREIGN KEY(Doctor_Username) REFERENCES DOCTOR(Username) 
ON DELETE CASCADE 
ON UPDATE CASCADE,
FOREIGN KEY(Patient_Username) REFERENCES PATIENT(Username) 
ON DELETE CASCADE 
ON UPDATE CASCADE
)
ENGINE = InnoDB;

CREATE TABLE PRESCRIPTION
(
    Patient_Username    VARCHAR(20)     NOT NULL,
Doctor_Username VARCHAR(20)     NOT NULL,   
Date_of_Visit       DATE                NOT NULL,
Medicine_Name   VARCHAR(20)     NOT NULL,
Notes           VARCHAR(255),
Dosage      INTEGER         NOT NULL,
Duration        INTEGER         NOT NULL,
Is_Ordered      BOOLEAN,
PRIMARY KEY (Patient_Username, Doctor_Username, Date_of_Visit, Medicine_Name),
FOREIGN KEY(Patient_Username, Doctor_Username, Date_of_Visit) 
REFERENCES VISIT(Patient_Username, Doctor_Username, Date_of_Visit) 
ON DELETE CASCADE 
ON UPDATE CASCADE
)
ENGINE = InnoDB;

患者,医生,访问表已正确创建。但是当创建处方时,它给了我

1005 - 无法创建表格&cs4400_Group_52.PRESCRIPTION' (错误:150)

知道为什么吗?

编辑:患者和医生表创建sql

CREATE TABLE PATIENT
(
    Username        VARCHAR(20)     NOT NULL,
    Password        VARCHAR(20)     NOT NULL,
    Name            VARCHAR(20)     NOT NULL,
    Home_Phone      INTEGER         NOT NULL,   
    Date_of_Birth       DATE                NOT NULL,
    Annual_Income   INTEGER         NOT NULL,
    Gender      ENUM('M','F')           NOT NULL,
    Address     VARCHAR(255)        NOT NULL,
    Work_Phone      INTEGER         NOT NULL,
    Contact_Name    VARCHAR(20)     NOT NULL,
    Contact_Phone   INTEGER         NOT NULL,
    Weight      INTEGER,
    Height          INTEGER,
    Card_No     INTEGER,
    PRIMARY KEY(Username),
    FOREIGN KEY(Card_No) REFERENCES PAYMENT_INFO(Card_No) 
ON DELETE CASCADE 
ON UPDATE CASCADE
)
ENGINE = InnoDB;

CREATE TABLE DOCTOR
(
    Username        VARCHAR(20)     NOT NULL,
    Password        VARCHAR(20)     NOT NULL,
    First_Name      VARCHAR(20)     NOT NULL,
    Last_Name       VARCHAR(20)     NOT NULL,
    Date_Of_Birth       DATE                NOT NULL,
    Work_Phone      INTEGER         NOT NULL,
    Speciality      VARCHAR(20)     NOT NULL,
    Room_Number INTEGER         NOT NULL,
    Home_Address    VARCHAR(255)        NOT NULL,
    Average_Rating  INTEGER         NOT NULL,
    License_Number  INTEGER         NOT NULL,
    PRIMARY KEY (Username)
)
ENGINE = InnoDB; 

enter image description here

2 个答案:

答案 0 :(得分:0)

两个错误:

A)在CREATE TABLE VISIT需要INDEX(Date_of_Visit,Patient_Username,Doctor_Username),

B)需要更改外键中字段的顺序,以匹配VISIT表主键中的字段顺序@Sparky

答案 1 :(得分:0)

问题是外键的字段顺序与相关表的主键的字段顺序不匹配...

 CREATE TABLE VISIT
    (
     <fields>
    PRIMARY KEY (Date_of_Visit, Doctor_Username, Patient_Username)


 CREATE TABLE PRESCRIPTION
 ( 
   <fields>
 REFERENCES VISIT(Patient_Username, Doctor_Username, Date_of_Visit) 

OP通过调整VISIT引用中的字段顺序来解决问题