CREATE TABLE VSISP12.EstateAgent
(
  ID INT PRIMARY KEY NOT NULL GENERATED ALWAYS AS IDENTITY(START WITH 1 INCREMENT BY 1),
  name VARCHAR(255) DEFAULT '' NOT NULL,
  address VARCHAR(255) DEFAULT '' NOT NULL,
  login VARCHAR(255) DEFAULT '' NOT NULL,
  password VARCHAR(255)
);
CREATE UNIQUE INDEX EstateAgent_login_uindex ON VSISP12.EstateAgent (login);

CREATE TABLE VSISP12.Estate
(
  ID INT PRIMARY KEY NOT NULL GENERATED ALWAYS AS IDENTITY(START WITH 1 INCREMENT BY 1),
  city VARCHAR(255) DEFAULT 'Musterstadt' NOT NULL,
  postalCode VARCHAR(5) DEFAULT '00000' NOT NULL,
  street VARCHAR(255) DEFAULT 'Musterstraße' NOT NULL,
  streetNumber SMALLINT DEFAULT 0 NOT NULL,
  squareArea INT DEFAULT 0 NOT NULL,
  agent INT NOT NULL,
  CONSTRAINT Estate_ESTATEAGENT_ID_fk FOREIGN KEY (agent) REFERENCES ESTATEAGENT (ID)
);

CREATE TABLE VSISP12.Apartment
(
  ID INT PRIMARY KEY NOT NULL,
  floor SMALLINT DEFAULT 0 NOT NULL,
  rent INT DEFAULT 0 NOT NULL,
  rooms SMALLINT DEFAULT 1 NOT NULL,
  balcony SMALLINT DEFAULT 0 NOT NULL,
  builtInKitchen SMALLINT DEFAULT 1 NOT NULL,
  CONSTRAINT Apartment_ESTATE_ID_fk FOREIGN KEY (ID) REFERENCES ESTATE (ID)
);

CREATE TABLE VSISP12.House
(
  ID INT PRIMARY KEY NOT NULL,
  price INT DEFAULT 0 NOT NULL,
  garden SMALLINT DEFAULT 0 NOT NULL,
  CONSTRAINT House_ESTATE_ID_fk FOREIGN KEY (ID) REFERENCES ESTATE (ID)
);

CREATE TABLE VSISP12.Contract
(
  contractNumber INT PRIMARY KEY NOT NULL GENERATED ALWAYS AS IDENTITY(START WITH 1 INCREMENT BY 1),
  date DATE,
  place VARCHAR(255) DEFAULT 'Musterstadt' NOT NULL
);

CREATE TABLE VSISP12.PurchaseContract
(
  contractNumber INT PRIMARY KEY NOT NULL,
  numberOfInstallments SMALLINT DEFAULT 0 NOT NULL,
  interestRate SMALLINT DEFAULT 0 NOT NULL,
  CONSTRAINT PurchaseContract_CONTRACT_CONTRACTNUMBER_fk FOREIGN KEY (contractNumber) REFERENCES CONTRACT (CONTRACTNUMBER)
);

CREATE TABLE VSISP12.TenancyContract
(
  contractNumber INT PRIMARY KEY NOT NULL,
  startDate TIMESTAMP,
  duration TIMESTAMP,
  additionalCosts INT DEFAULT 0 NOT NULL,
  CONSTRAINT TenancyContract_CONTRACT_CONTRACTNUMBER_fk FOREIGN KEY (contractNumber) REFERENCES CONTRACT (CONTRACTNUMBER)
);

CREATE TABLE VSISP12.Person
(
  ID INT PRIMARY KEY NOT NULL GENERATED ALWAYS AS IDENTITY(START WITH 1 INCREMENT BY 1),
  firstName VARCHAR(255) DEFAULT 'Max' NOT NULL,
  name VARCHAR(255) DEFAULT 'Mustermann' NOT NULL,
  address VARCHAR(255) DEFAULT 'Musterstraße' NOT NULL
);

CREATE TABLE VSISP12.Sales
(
  contractNumber INT PRIMARY KEY NOT NULL,
  person INT NOT NULL,
  house INT NOT NULL,
  CONSTRAINT Sales_PURCHASECONTRACT_CONTRACTNUMBER_fk FOREIGN KEY (contractNumber) REFERENCES PURCHASECONTRACT (CONTRACTNUMBER),
  CONSTRAINT Sales_PERSON_ID_fk FOREIGN KEY (person) REFERENCES PERSON (ID),
  CONSTRAINT Sales_HOUSE_ID_fk FOREIGN KEY (house) REFERENCES HOUSE (ID)
);
CREATE UNIQUE INDEX Sales_house_uindex ON VSISP12.Sales (house);

CREATE TABLE VSISP12.Rentals
(
  contractNumber INT PRIMARY KEY NOT NULL,
  person INT NOT NULL,
  apartment INT NOT NULL,
  CONSTRAINT Rentals_TENANCYCONTRACT_CONTRACTNUMBER_fk FOREIGN KEY (contractNumber) REFERENCES TENANCYCONTRACT (CONTRACTNUMBER),
  CONSTRAINT Rentals_PERSON_ID_fk FOREIGN KEY (person) REFERENCES PERSON (ID),
  CONSTRAINT Rentals_APARTMENT_ID_fk FOREIGN KEY (apartment) REFERENCES APARTMENT (ID)
);
CREATE UNIQUE INDEX Rentals_apartment_uindex ON VSISP12.Rentals (apartment);