drop index if exists IX_T_CARD_EPICRISIS_MovementsDivisionCard on AKUZ.T_CARD_EPICRISIS;

ALTER TABLE AKUZ.T_CARD_EPICRISIS ADD "medicalepisode" uuid NULL;

update AKUZ.T_CARD_EPICRISIS set MedicalEpisode = MovementsDivisionCard where MovementsDivisionCard is not null;
delete from AKUZ.T_CARD_EPICRISIS where MovementsDivisionCard is null;

ALTER TABLE AKUZ.T_CARD_EPICRISIS ALTER COLUMN "medicalepisode" SET NOT NULL;

ALTER TABLE AKUZ.T_CARD_EPICRISIS DROP CONSTRAINT FK_T_MOVEMENTS_DIVISION_CARD_T_CARD_EPICRISIS_Movement_ECC08103;
ALTER TABLE AKUZ.T_CARD_EPICRISIS DROP COLUMN "movementsdivisioncard";
ALTER TABLE AKUZ.T_CARD_EPICRISIS ADD CONSTRAINT FK_T_MEDICAL_EPISODE_T_CARD_EPICRISIS_MedicalEpisode
FOREIGN KEY("medicalepisode") REFERENCES AKUZ.T_MEDICAL_EPISODE (MedicalEpisodeID);
