Two errors:
A) in CREATE TABLE VISIT need to INDEX (Date_of_Visit, Patient_Username, Doctor_Username),
B) need change the order of the fields in the Foreign key to match the order of the fields in the VISIT table's primary key @Sparky
문제
Here are the relevant sql statements:
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;
Patient, Doctor, Visit tables have been created correctly. But when creating prescription, it gives me
Any idea why?
EDIT: patient and doctor table creation 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;
해결책 2
Two errors:
A) in CREATE TABLE VISIT need to INDEX (Date_of_Visit, Patient_Username, Doctor_Username),
B) need change the order of the fields in the Foreign key to match the order of the fields in the VISIT table's primary key @Sparky
다른 팁
The issue was that the field order of the foreign key did not match the field order of the related table's primary key...
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)
The OP solved the issue by adjusting the field order in the VISIT references