Question: i need help with sql code highlighted . i keep getting this Error Code: 1 4 5 2 . Cannot add or update a child

i need help with sql code highlighted . i keep getting this Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`healthcare`.`billing`, CONSTRAINT `billing_ibfk_1` FOREIGN KEY (`Record_ID`) REFERENCES `medicalrecord`(`Record_ID`)) dont know what i did wrong ... Here is my full sql code CREATE SCHEMA healthcare;
Use healthcare;
CREATE TABLE Patient (
Patient_ID INT PRIMARY KEY,
Patient_Name VARCHAR(255) NOT NULL,
Patient_Address VARCHAR(255) NOT NULL,
Contact VARCHAR(255) NOT NULL,
Insurance VARCHAR(255) NOT NULL
);
CREATE TABLE Doctor (
Doctor_ID INT PRIMARY KEY,
Name VARCHAR(255) NOT NULL,
Specialization VARCHAR(255) NOT NULL,
Contact VARCHAR(255) NOT NULL
);
CREATE TABLE Appointment (
RecordID INT PRIMARY KEY,
PatientID INT,
DoctorID INT,
Date DATETIME NOT NULL,
FOREIGN KEY (PatientID) REFERENCES Patient(PatientID),
FOREIGN KEY (DoctorID) REFERENCES Doctor(DoctorID)
);
CREATE TABLE MedicalRecord (
RecordID INT PRIMARY KEY,
PatientID INT,
Diagnosis VARCHAR(255) NOT NULL,
Treatment VARCHAR(255) NOT NULL,
FOREIGN KEY (PatientID) REFERENCES Patient(PatientID)
);
CREATE TABLE Billing (
CREATE TABLE Billing (
BillingID INT PRIMARY KEY,
RecordID INT,
PatientID INT,
TotalAmount DECIMAL(10,2) NOT NULL,
PaymentStatus VARCHAR(255) NOT NULL,
FOREIGN KEY (RecordID) REFERENCES MedicalRecord(RecordID),
FOREIGN KEY (PatientID) REFERENCES Patient(PatientID)
);
ALTER TABLE MedicalReco rd
ADD CONSTRAINT FK_MedicalRecord_Appointment
FOREIGN KEY (RecordID)
REFERENCES Appointment(RecordID);
INSERT INTO Patient (Patient_ID, Patient_Name, Patient_Address, Contact, Insurance)
VALUES
(1, 'John Doe', '123 Main St, Anytown, CA 91234','555-123-4567', 'Aetna'),
(2, 'Jane Smith', '456 Elm St, Anytown, CA 91234','555-987-6543', 'Blue Cross Blue Shield'),
(3, 'Mike Jones', '789 Oak St, Anytown, CA 91234','555-234-5678', 'Cigna'),
(4, 'Alice Lee', '1011 Cedar St, Anytown, CA 91234','555-345-6789', 'UnitedHealthcare'),
(5, 'Bob Brown', '1213 Walnut St, Anytown, CA 91234','555-456-7890', 'Humana');
INSERT INTO Patient (PatientID, PatientName, PatientAddress, Contact, Insurance)
VALUES
(1, 'John Doe', '123 Main St, Anytown, CA 91234','555-123-4567', 'Aetna'),
(2, 'Jane Smith', '456 Elm St, Anytown, CA 91234','555-987-6543', 'Blue Cross Blue Shield'),
(3, 'Mike Jones', '789 Oak St, Anytown, CA 91234','555-234-5678', 'Cigna'),
(4, 'Alice Lee', '1011 Cedar St, Anytown, CA 91234','555-345-6789', 'UnitedHealthcare'),
(5, 'Bob Brown', '1213 Walnut St, Anytown, CA 91234','555-456-7890', 'Humana');
INSERT INTO Doctor (DoctorID, Name, Specialization, Contact)
VALUES
(1,'Dr. Smith', 'General Medicine', '555-555-1212'),
(2,'Dr. Jones', 'Cardiology', '555-555-2323'),
(3,'Dr. Lee', 'Dermatology', '555-555-3434'),
(4,'Dr. Brown', 'Neurology', '555-555-4545'),
(5,'Dr. Williams', 'Orthopedics', '555-555-5656');
INSERT INTO Appointment (RecordID, PatientID, DoctorID, Date)
VALUES
(1,1,1,'2023-10-26'),
(2,2,3,'2023-11-02'),
(3,3,2,'2023-11-09'),
(4,4,4,'2023-11-16'),
(5,5,5,'2023-11-23'),
(6,1,2,'2023-12-07'),
(7,3,1,'2023-12-14'),
(8,5,4,'2023-12-21');
INSERT INTO MedicalRecord (RecordID, PatientID, Diagnosis, Treatment)
VALUES
(1,1, 'Hypertension', 'Medications, lifestyle changes'),
(2,2, 'Eczema', 'Topical steroids, emollients'),
(3,3, 'Anxiety', 'Therapy, medication'),
(4,4, 'Migraine headaches', 'Pain medication, preventive medication'),
(5,5, 'Knee pain', 'Physical therapy, pain medication');
INSERT INTO Billing (BillingID, RecordID, PatientID, TotalAmount, PaymentStatus)
VALUES
(1,1,1,200.00, 'Paid'),
(2,2,2,150.00, 'Paid'),
(3,3,3,100.00, 'Paid'),
(4,4,4,250.00, 'Paid'),
(5,5,5,300.00, 'Paid'),
(6,6,1,175.00, 'Paid'),
(7,7,3,125.00, 'Paid'),
(8,8,5,225.00, 'Paid');

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!