ALTER TABLE AKUZ.T_MEDICAL_RECORD 
ADD COLUMN "momain" UUID;

UPDATE AKUZ.T_MEDICAL_RECORD mr
SET "momain" = b."lpumain"
FROM AKUZ.T_RESOURCE r
JOIN AKUZ.T_DIVISION d ON d."divisionid" = r."division"
JOIN AKUZ.T_BRANCH b ON b."lpuid" = d."branch"
WHERE mr."post" = r."resourceid";

ALTER TABLE AKUZ.T_MEDICAL_RECORD 
ALTER COLUMN "momain"  SET NOT NULL;

ALTER TABLE AKUZ.T_MEDICAL_RECORD
ADD CONSTRAINT FK_T_LPU_MAIN_T_MEDICAL_RECORD_MOMain
FOREIGN KEY("momain") REFERENCES AKUZ.T_LPU_MAIN ("lpuid");