-- Tables

/***** Object:  Table EHR.T_EHR_RUS_Professionalnie_zabolevanija *****/
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.tables 
        WHERE table_schema = 'EHR' AND table_name = 'T_EHR_RUS_Professionalnie_zabolevanija'
    ) THEN
CREATE TABLE EHR.T_EHR_RUS_Professionalnie_zabolevanija
(
    "ehrrusid" integer NOT NULL,
    "idehr" integer NOT NULL,
    "nameehr" varchar(512) NULL,
    "ehrrusrefbooks" integer NOT NULL,
    "pidehr" integer NULL,
    "ehrrustree" integer NULL,
    "number" varchar(15) NULL,
    "diseases" varchar(1024) NULL,
    "icd10_name" varchar(256) NULL,
    "icd10" varchar(10) NULL,
    "external_cause" varchar(5) NULL
);
    END IF;

-- Alters

    IF NOT EXISTS (
        SELECT 1 FROM information_schema.tables 
        WHERE table_schema = 'EHR' AND table_name = 'T_EHR_RUS_Professionalnie_zabolevanija'
    ) THEN
ALTER TABLE EHR.T_EHR_RUS_Professionalnie_zabolevanija ADD CONSTRAINT PK_T_EHR_RUS_PROFESSIONALNIE_ZABOLEVANIJA
PRIMARY KEY (EhrRusID);
    END IF;
    
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.tables 
        WHERE table_schema = 'EHR' AND table_name = 'T_EHR_RUS_Professionalnie_zabolevanija'
    ) THEN
ALTER TABLE EHR.T_EHR_RUS_Professionalnie_zabolevanija ADD CONSTRAINT FK_T_EHR_RUS_REF_BOOKS_T_EHR_RUS_PROFESSIONALNIE_ZABOL_E63F02CA
FOREIGN KEY("ehrrusrefbooks") REFERENCES EHR.T_EHR_RUS_REF_BOOKS (EhrRusRefBooksID);
    END IF;
    
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.tables 
        WHERE table_schema = 'EHR' AND table_name = 'T_EHR_RUS_Professionalnie_zabolevanija'
    ) THEN
ALTER TABLE EHR.T_EHR_RUS_Professionalnie_zabolevanija ADD CONSTRAINT FK_T_EHR_RUS_PROFESSIONALNIE_ZABOLEVANIJA_T_EHR_RUS_PR_7055B271
FOREIGN KEY("ehrrustree") REFERENCES EHR.T_EHR_RUS_Professionalnie_zabolevanija (EhrRusID);
    END IF;

    
END $$;