CREATE TABLE AKUZ.T_CDA_DELIVERY_PLAN
(
    "cdadeliveryplanid" uuid NOT NULL,
    "datecreationplan" timestamp NOT NULL,
    "cdaexaminationspecialist" uuid NULL,
    "timecreationplan" varchar(255) NOT NULL,
    "ehrrusfrn430" uuid NOT NULL
);

-- Alters

ALTER TABLE AKUZ.T_CDA_DELIVERY_PLAN ADD CONSTRAINT PK_T_CDA_DELIVERY_PLAN
PRIMARY KEY (CdaDeliveryPlanID);

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

CREATE UNIQUE INDEX UQ_T_CDA_DELIVERY_PLAN_CdaExaminationSpecialist ON AKUZ.T_CDA_DELIVERY_PLAN(CdaExaminationSpecialist);

ALTER TABLE AKUZ.T_CDA_DELIVERY_PLAN ADD CONSTRAINT FK_T_CDA_EXAMINATION_SPECIALIST_T_CDA_DELIVERY_PLAN_CdaExaminationSpecialist
FOREIGN KEY("cdaexaminationspecialist") REFERENCES AKUZ.T_CDA_EXAMINATION_SPECIALIST (CdaExaminationSpecialistID);

ALTER TABLE AKUZ.T_CDA_DELIVERY_PLAN ADD CONSTRAINT FK_T_EHR_RUS_TREE_T_CDA_DELIVERY_PLAN_EhrRusFrn430
FOREIGN KEY("ehrrusfrn430") REFERENCES EHR.T_EHR_RUS_TREE (EhrRusID);