ALTER TABLE AKUZ.T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3 DROP CONSTRAINT FK_T_CN_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3_T_CDA_DOCUME_14C265FF;

do $$ begin
IF EXISTS(select * from pg_indexes where schemaname ilike 'AKUZ' and tablename ilike 'T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3' and indexname ilike 'UQ_T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3_CardNote')
THEN
drop index AKUZ.UQ_T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3_CardNote;
END IF;
end; $$;
ALTER TABLE AKUZ.T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3 DROP CONSTRAINT if exists UQ_T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3_CardNote;
ALTER TABLE AKUZ.T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3 DROP COLUMN if exists CardNote_UQ;

CREATE UNIQUE INDEX UQ_T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3_CardNote ON AKUZ.T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3(CardNote);

ALTER TABLE AKUZ.T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3 ADD CONSTRAINT FK_T_CARD_NOTE_T_CDA_DOCUMENT_PROTOCOLEXTRACT_REV3_CardNote
FOREIGN KEY("cardnote") REFERENCES AKUZ.T_CARD_NOTE (MedicalRecordID);