CREATE TABLE "activity" (
  "id" SERIAL CONSTRAINT "pk_activity" PRIMARY KEY,
  "name" TEXT NOT NULL
);

CREATE TABLE "contact_info" (
  "id" SERIAL CONSTRAINT "pk_contact_info" PRIMARY KEY,
  "email" TEXT NOT NULL
);

CREATE TABLE "enterprise" (
  "id" SERIAL CONSTRAINT "pk_enterprise" PRIMARY KEY,
  "name" TEXT NOT NULL,
  "short_name" TEXT NOT NULL,
  "contact_info" INTEGER NOT NULL
);

CREATE INDEX "idx_enterprise__contact_info" ON "enterprise" ("contact_info");

ALTER TABLE "enterprise" ADD CONSTRAINT "fk_enterprise__contact_info" FOREIGN KEY ("contact_info") REFERENCES "contact_info" ("id");

CREATE TABLE "division" (
  "id" SERIAL CONSTRAINT "pk_division" PRIMARY KEY,
  "enterprise" INTEGER NOT NULL,
  "contact_info" INTEGER NOT NULL
);

CREATE INDEX "idx_division__contact_info" ON "division" ("contact_info");

CREATE INDEX "idx_division__enterprise" ON "division" ("enterprise");

ALTER TABLE "division" ADD CONSTRAINT "fk_division__contact_info" FOREIGN KEY ("contact_info") REFERENCES "contact_info" ("id");

ALTER TABLE "division" ADD CONSTRAINT "fk_division__enterprise" FOREIGN KEY ("enterprise") REFERENCES "enterprise" ("id");

CREATE TABLE "do_activity" (
  "id" SERIAL CONSTRAINT "pk_do_activity" PRIMARY KEY,
  "enterprise" INTEGER,
  "activity" INTEGER NOT NULL,
  "division" INTEGER
);

CREATE INDEX "idx_do_activity__activity" ON "do_activity" ("activity");

CREATE INDEX "idx_do_activity__division" ON "do_activity" ("division");

CREATE INDEX "idx_do_activity__enterprise" ON "do_activity" ("enterprise");

ALTER TABLE "do_activity" ADD CONSTRAINT "fk_do_activity__activity" FOREIGN KEY ("activity") REFERENCES "activity" ("id");

ALTER TABLE "do_activity" ADD CONSTRAINT "fk_do_activity__division" FOREIGN KEY ("division") REFERENCES "division" ("id");

ALTER TABLE "do_activity" ADD CONSTRAINT "fk_do_activity__enterprise" FOREIGN KEY ("enterprise") REFERENCES "enterprise" ("id");

CREATE TABLE "head" (
  "id" SERIAL CONSTRAINT "pk_head" PRIMARY KEY,
  "date_of_start" DATE NOT NULL,
  "date_of_end" DATE,
  "contact_info" INTEGER NOT NULL
);

CREATE INDEX "idx_head__contact_info" ON "head" ("contact_info");

ALTER TABLE "head" ADD CONSTRAINT "fk_head__contact_info" FOREIGN KEY ("contact_info") REFERENCES "contact_info" ("id");

CREATE TABLE "location" (
  "id" SERIAL CONSTRAINT "pk_location" PRIMARY KEY,
  "country" TEXT NOT NULL,
  "city" TEXT NOT NULL,
  "street" TEXT NOT NULL,
  "house" TEXT NOT NULL,
  "room" TEXT NOT NULL,
  "date_of_start" DATE NOT NULL,
  "date_of_end" DATE,
  "contact_info" INTEGER NOT NULL
);

CREATE INDEX "idx_location__contact_info" ON "location" ("contact_info");

ALTER TABLE "location" ADD CONSTRAINT "fk_location__contact_info" FOREIGN KEY ("contact_info") REFERENCES "contact_info" ("id");

CREATE TABLE "phone_number" (
  "id" SERIAL CONSTRAINT "pk_phone_number" PRIMARY KEY,
  "contact_info" INTEGER NOT NULL,
  "date_of_start" DATE NOT NULL,
  "date_of_end" DATE
);

CREATE INDEX "idx_phone_number__contact_info" ON "phone_number" ("contact_info");

ALTER TABLE "phone_number" ADD CONSTRAINT "fk_phone_number__contact_info" FOREIGN KEY ("contact_info") REFERENCES "contact_info" ("id")