CREATE TABLE akuz.f_medical_record_medicalepisode (
    id UUID NOT NULL,
    value UUID,
    CONSTRAINT pk_f_medical_record_medicalepisode PRIMARY KEY (id)
);

ALTER TABLE akuz.f_medical_record_medicalepisode 
ADD CONSTRAINT fk_spread_akuz_medical_episode_medicalepisode 
FOREIGN KEY (value) REFERENCES akuz.t_medical_episode(medicalepisodeid);

ALTER TABLE akuz.f_medical_record_medicalepisode 
ADD CONSTRAINT fk_spread_akuz_medical_episode_medicalrecordid 
FOREIGN KEY (ID) REFERENCES akuz.t_medical_record(medicalrecordid);

INSERT INTO AKUZ.F_MEDICAL_RECORD_MedicalEpisode (Id, value)
SELECT medicalrecordid, medicalepisode
FROM AKUZ.T_MEDICAL_RECORD 
WHERE medicalepisode IS NOT NULL;

ALTER TABLE AKUZ.T_MEDICAL_RECORD 
DROP CONSTRAINT FK_T_MEDICAL_EPISODE_T_MEDICAL_RECORD_MedicalEpisode;

call public.sp_alterdropcolumn('AKUZ.T_MEDICAL_RECORD' , 'MedicalEpisode'); 


