--T_EHR_RUS_42Q2UOMG02
ALTER TABLE EHR.T_EHR_RUS_42Q2UOMG02 DROP CONSTRAINT t_ehr_rus_42q2uomg02_pkey;
ALTER TABLE EHR.T_EHR_RUS_42Q2UOMG02 ADD COLUMN "EhrRusID_int" INTEGER;
UPDATE EHR.T_EHR_RUS_42Q2UOMG02 SET "EhrRusID_int" = "idehr";
ALTER TABLE EHR.T_EHR_RUS_42Q2UOMG02 DROP COLUMN "ehrrusid";
ALTER TABLE EHR.T_EHR_RUS_42Q2UOMG02 RENAME COLUMN "EhrRusID_int" TO "ehrrusid";
ALTER TABLE EHR.T_EHR_RUS_42Q2UOMG02 ALTER COLUMN "ehrrusid" SET NOT NULL;
ALTER TABLE EHR.T_EHR_RUS_42Q2UOMG02 ADD CONSTRAINT t_ehr_rus_42q2uomg02_pkey
PRIMARY KEY ("ehrrusid");
INSERT INTO public.t_entity_ids ("entname", "fieldname", "id")
SELECT 'EHR.EHR_RUS_42Q2UOMG02', 'PrimaryKey', 4
WHERE NOT EXISTS (
    SELECT 1
    FROM public.t_entity_ids
    WHERE "entname" = 'EHR.EHR_RUS_42Q2UOMG02'
      AND "fieldname" = 'PrimaryKey'
);

--T_EHR_RUS_Groupi_risk_razvitiia_prof_zabol

DROP TABLE IF EXISTS EHR.T_EHR_RUS_Groupi_risk_razvitiia_prof_zabol;
DELETE FROM EHR.T_EHR_REFERENCE 
WHERE "ehrentity" = (
    SELECT "entityid"
    FROM VCLib.T_ENTITIES
    WHERE "entname" = 'EHR_RUS_Groupi_risk_razvitiia_prof_zabol'
);
DELETE FROM EHR.T_EHR_RUS_REF_BOOKS
WHERE "entities" = (
    SELECT "entityid"
    FROM VCLib.T_ENTITIES
    WHERE "entname" = 'EHR_RUS_Groupi_risk_razvitiia_prof_zabol'
);
CREATE TABLE EHR.T_EHR_RUS_Groupi_risk_razvitiia_prof_zabol
(
    "ehrrusid" integer NOT NULL,
    "idehr" integer NULL,
    "nameehr" varchar(255) NULL,
    "description" varchar(255) NULL,
    "ehrrusrefbooks" integer NOT NULL
);
ALTER TABLE EHR.T_EHR_RUS_Groupi_risk_razvitiia_prof_zabol ADD CONSTRAINT T_EHR_RUS_GROUPI_RISK_RAZVITIIA_PROF_ZABOL_pkey
PRIMARY KEY ("ehrrusid");
ALTER TABLE EHR.T_EHR_RUS_Groupi_risk_razvitiia_prof_zabol ADD CONSTRAINT FK_T_EHR_RUS_REF_BOOKS_T_EHR_RUS_GROUPI_RISK_RAZVITIIA_652ACFD1
FOREIGN KEY("ehrrusrefbooks") REFERENCES EHR.T_EHR_RUS_REF_BOOKS ("ehrrusrefbooksid");

--T_EHR_RUS_Vidi_sporta

DROP TABLE IF EXISTS ehr.T_EHR_RUS_Vidi_sporta;
DELETE FROM EHR.T_EHR_REFERENCE 
WHERE "ehrentity" = (
    SELECT "entityid"
    FROM VCLib.T_ENTITIES
    WHERE "entname" = 'EHR_RUS_Vidi_sporta'
);
DELETE FROM EHR.T_EHR_RUS_REF_BOOKS
WHERE "entities" = (
    SELECT "entityid"
    FROM VCLib.T_ENTITIES
    WHERE "entname" = 'EHR_RUS_Vidi_sporta'
);
CREATE TABLE EHR.T_EHR_RUS_Vidi_sporta
(
    "ehrrusid" integer NOT NULL,
    "idehr" integer NULL,
    "nameehr" varchar(255) NULL,
    "codeehr" varchar(255) NULL,
    "part" varchar(255) NULL,
    "normative" integer NULL,
    "actual" boolean NOT NULL,
    "ehrrusrefbooks" integer NOT NULL
);
ALTER TABLE EHR.T_EHR_RUS_Vidi_sporta ADD CONSTRAINT T_EHR_RUS_VIDI_SPORTA_pkey
PRIMARY KEY ("ehrrusid");
ALTER TABLE EHR.T_EHR_RUS_Vidi_sporta ADD CONSTRAINT FK_T_EHR_RUS_REF_BOOKS_T_EHR_RUS_VIDI_SPORTA_EhrRusRefBooks
FOREIGN KEY("ehrrusrefbooks") REFERENCES EHR.T_EHR_RUS_REF_BOOKS ("ehrrusrefbooksid");