MYSQL中的外键问题

时间:2014-02-12 14:35:54

标签: mysql foreign-keys

我试图用FK创建表,但是我在插入SQL代码时遇到了错误:

CREATE TABLE appointment(
    appointment_id INT UNSIGNED AUTO_INCREMENT,
    patient_id     MEDIUMINT UNSIGNED,
    doctor_id      TINYINT UNSIGNED,
    date           TIMESTAMP NOT NULL,
    start_time     TIME NOT NULL,
    end_time       TIME NOT NULL,
    medical_report TEXT,
    supplement_id    SMALLINT,


    CONSTRAINT pk_appointment PRIMARY KEY (appointment_id),
    CONSTRAINT fk_appointment_patient_id FOREIGEN KEY(patient_id) REFERENCES patient(patient_id), 
    CONSTRAINT fk_appointment_doctor_id  FOREIGEN KEY(doctor_id) REFERENCES doctor(doctor_id),
    CONSTRAINT fk_appointment_medicine_id FOREIGEN KEY(supplement_id) REFERENCES medicine(medicine_id)
);

错误:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FOREIGEN KEY(patient_id) REFERENCES patient(patient_id), CONSTRAINT fk_appointm' at line 13

有人帮帮我!

1 个答案:

答案 0 :(得分:1)

试试这个:

CREATE TABLE appointment(
    appointment_id INT UNSIGNED AUTO_INCREMENT,
    patient_id     MEDIUMINT UNSIGNED,
    doctor_id      TINYINT UNSIGNED,
    date           TIMESTAMP NOT NULL,
    start_time     TIME NOT NULL,
    end_time       TIME NOT NULL,
    medical_report TEXT,
    supplement_id    SMALLINT,


    CONSTRAINT pk_appointment PRIMARY KEY (appointment_id),
    CONSTRAINT fk_appointment_patient_id FOREIGN  KEY(patient_id) REFERENCES patient(patient_id), 
    CONSTRAINT fk_appointment_doctor_id  FOREIGN  KEY(doctor_id) REFERENCES doctor(doctor_id),
    CONSTRAINT fk_appointment_medicine_id FOREIGN  KEY(supplement_id) REFERENCES medicine(medicine_id)
);