
-- Update laboratory_inquiries and xray_inquiries to link with patients and visits
ALTER TABLE laboratory_inquiries ADD COLUMN patient_id INT NULL AFTER id;
ALTER TABLE laboratory_inquiries ADD COLUMN visit_id INT NULL AFTER patient_id;
ALTER TABLE laboratory_inquiries MODIFY patient_name VARCHAR(255) NULL;

ALTER TABLE laboratory_inquiries ADD CONSTRAINT fk_lab_patient FOREIGN KEY (patient_id) REFERENCES patients(id) ON DELETE CASCADE;
ALTER TABLE laboratory_inquiries ADD CONSTRAINT fk_lab_visit FOREIGN KEY (visit_id) REFERENCES visits(id) ON DELETE SET NULL;

ALTER TABLE xray_inquiries ADD COLUMN patient_id INT NULL AFTER id;
ALTER TABLE xray_inquiries ADD COLUMN visit_id INT NULL AFTER patient_id;
ALTER TABLE xray_inquiries MODIFY patient_name VARCHAR(255) NULL;

ALTER TABLE xray_inquiries ADD CONSTRAINT fk_xray_patient FOREIGN KEY (patient_id) REFERENCES patients(id) ON DELETE CASCADE;
ALTER TABLE xray_inquiries ADD CONSTRAINT fk_xray_visit FOREIGN KEY (visit_id) REFERENCES visits(id) ON DELETE SET NULL;
