-- MySQL Script generated by MySQL Workbench
-- Sat Apr  6 18:53:00 2019
-- Model: New Model    Version: 1.0
-- MySQL Workbench Forward Engineering

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

-- -----------------------------------------------------
-- Schema Thoth
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `Thoth` ;

-- -----------------------------------------------------
-- Schema Thoth
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `Thoth` DEFAULT CHARACTER SET utf8 ;
USE `Thoth` ;

-- -----------------------------------------------------
-- Table `Thoth`.`object`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Thoth`.`object` ;

CREATE TABLE IF NOT EXISTS `Thoth`.`object` (
  `ObjectId` INT(11) NOT NULL AUTO_INCREMENT,
  `ZipCode` VARCHAR(6) NOT NULL,
  `City` VARCHAR(11) NOT NULL,
  `Street` VARCHAR(11) NOT NULL,
  `IsShop` TINYINT(1) NOT NULL,
  PRIMARY KEY (`ObjectId`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `Thoth`.`role`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Thoth`.`role` ;

CREATE TABLE IF NOT EXISTS `Thoth`.`role` (
  `RoleId` INT(11) NOT NULL AUTO_INCREMENT,
  `Position` VARCHAR(25) NOT NULL,
  PRIMARY KEY (`RoleId`));


-- -----------------------------------------------------
-- Table `Thoth`.`user`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Thoth`.`user` ;

CREATE TABLE IF NOT EXISTS `Thoth`.`user` (
  `UserId` INT(11) NOT NULL AUTO_INCREMENT,
  `FirstName` VARCHAR(25) NOT NULL,
  `LastName` VARCHAR(50) NOT NULL,
  `Login` VARCHAR(25) NOT NULL,
  `Password` TEXT NOT NULL,
  `RoleId` INT(11) NOT NULL,
  `State` TINYINT(1) NOT NULL,
  `ObjectId` INT(11) NOT NULL,
  PRIMARY KEY (`UserId`),
  CONSTRAINT `fk_user_RoleId`
    FOREIGN KEY (`RoleId`)
    REFERENCES `Thoth`.`role` (`RoleId`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_user_ObjectId`
    FOREIGN KEY (`ObjectId`)
    REFERENCES `Thoth`.`object` (`ObjectId`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

ALTER TABLE `Thoth`.`user` ADD INDEX `ObjectId_idx` (`ObjectId`);
ALTER TABLE `Thoth`.`user` ADD INDEX `RoleId_idx` (`RoleId`);


-- -----------------------------------------------------
-- Table `Thoth`.`receipt`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Thoth`.`receipt` ;

CREATE TABLE IF NOT EXISTS `Thoth`.`receipt` (
  `ReceiptId` INT(11) NOT NULL AUTO_INCREMENT,
  `ObjectId` INT(11) NOT NULL,
  `TotalValue` DECIMAL(8,2) NOT NULL,
  `UserId` INT(11) NOT NULL,
  `Date` DATE NOT NULL,
  PRIMARY KEY (`ReceiptId`),
  CONSTRAINT `fk_receipt_ObjectId`
    FOREIGN KEY (`ObjectId`)
    REFERENCES `Thoth`.`object` (`ObjectId`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_receipt_UserId`
    FOREIGN KEY (`UserId`)
    REFERENCES `Thoth`.`user` (`UserId`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

ALTER TABLE `Thoth`.`receipt` ADD INDEX `ObjectId_idx` (`ObjectId`);
ALTER TABLE `Thoth`.`receipt` ADD INDEX `UserId_idx` (`UserId`);


-- -----------------------------------------------------
-- Table `Thoth`.`product`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Thoth`.`product` ;

CREATE TABLE IF NOT EXISTS `Thoth`.`product` (
  `ProductId` INT(11) NOT NULL AUTO_INCREMENT,
  `Name` VARCHAR(25) NOT NULL,
  `Price` DECIMAL(8,2) NOT NULL,
  `Discount` INT(11) NULL DEFAULT NULL,
  PRIMARY KEY (`ProductId`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `Thoth`.`product-receipt`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Thoth`.`product_receipt` ;

CREATE TABLE IF NOT EXISTS `Thoth`.`product_receipt` (
  `ProductId` INT(11) NOT NULL,
  `ReceiptId` INT(11) NOT NULL,
  `Amount` INT(11) NOT NULL,
  `Price` DECIMAL(8,2) NOT NULL,
  CONSTRAINT `fk_product_receipt_ReceiptId`
    FOREIGN KEY (`ReceiptId`)
    REFERENCES `Thoth`.`receipt` (`ReceiptId`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_product_receipt_ProductId`
    FOREIGN KEY (`ProductId`)
    REFERENCES `Thoth`.`product` (`ProductId`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

ALTER TABLE `Thoth`.`product_receipt` ADD INDEX `ReceiptId_idx` (`ReceiptId`);

ALTER TABLE `Thoth`.`product_receipt` ADD INDEX `ProductId_idx` (`ProductId`);


-- -----------------------------------------------------
-- Table `Thoth`.`state-on-object`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Thoth`.`state_on_object` ;

CREATE TABLE IF NOT EXISTS `Thoth`.`state_on_object` (
  `ProductId` INT(11) NOT NULL,
  `ObjectId` INT(11) NOT NULL,
  `Amount` DECIMAL(8,2) NOT NULL,
  CONSTRAINT `fk_state_on_object_ProductId`
    FOREIGN KEY (`ProductId`)
    REFERENCES `Thoth`.`product` (`ProductId`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_state_on_object_ObjectId`
    FOREIGN KEY (`ObjectId`)
    REFERENCES `Thoth`.`object` (`ObjectId`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

ALTER TABLE `Thoth`.`state_on_object` ADD INDEX `ProductId_idx` (`ProductId`);
ALTER TABLE `Thoth`.`state_on_object` ADD INDEX `ObjectId_idx` (`ObjectId`);


-- -----------------------------------------------------
-- Table `Thoth`.`user-object`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Thoth`.`user_object` ;

CREATE TABLE IF NOT EXISTS `Thoth`.`user_object` (
  `ObjectId` INT(11) NOT NULL,
  `UserId` INT(11) NOT NULL,
  CONSTRAINT `fk_user_object_ObjectId`
    FOREIGN KEY (`ObjectId`)
    REFERENCES `Thoth`.`object` (`ObjectId`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_user_object_UserId`
    FOREIGN KEY (`UserId`)
    REFERENCES `Thoth`.`user` (`UserId`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

ALTER TABLE `Thoth`.`user_object` ADD INDEX `ObjectId_idx` (`ObjectId`);
ALTER TABLE `Thoth`.`user_object` ADD INDEX `UserId_idx` (`UserId`);


-- -----------------------------------------------------
-- Table `Thoth`.`customer`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Thoth`.`customer` ;

CREATE TABLE IF NOT EXISTS `Thoth`.`customer` (
  `CustomerId` INT(11) NOT NULL AUTO_INCREMENT,
  `FirstName` VARCHAR(25) NOT NULL,
  `LastName` VARCHAR(25) NOT NULL,
  `PhoneNumber` INT(11) NOT NULL,
  PRIMARY KEY (`CustomerId`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `Thoth`.`order`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Thoth`.`order` ;

CREATE TABLE IF NOT EXISTS `Thoth`.`order` (
  `OrderId` INT(11) NOT NULL AUTO_INCREMENT,
  `ObjectId_need` INT(11) NOT NULL,
  `CustomerId` INT(11) NULL,
  `ObjectId_delivery` INT(11) NOT NULL,
  `DateOfOrder` DATE NOT NULL,
  `ParentId` INT(11) NULL,
  PRIMARY KEY (`OrderId`),
  CONSTRAINT `fk_order_ObjectId_need`
    FOREIGN KEY (`ObjectId_need`)
    REFERENCES `Thoth`.`object` (`ObjectId`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_order_ObjectId_delivery`
    FOREIGN KEY (`ObjectId_delivery`)
    REFERENCES `Thoth`.`object` (`ObjectId`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_order_ParentId`
    FOREIGN KEY (`ParentId`)
    REFERENCES `Thoth`.`order` (`OrderId`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_order_CustomerId`
    FOREIGN KEY (`CustomerId`)
    REFERENCES `Thoth`.`customer` (`CustomerId`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

ALTER TABLE `Thoth`.`order` ADD INDEX `ObjectId_need_idx` (`ObjectId_need`);
ALTER TABLE `Thoth`.`order` ADD INDEX `ObjectId_delivery_idx` (`ObjectId_delivery`);
ALTER TABLE `Thoth`.`order` ADD INDEX `ParentId_idx` (`ParentId`);
ALTER TABLE `Thoth`.`order` ADD INDEX `CustomerId_idx` (`CustomerId`);


-- -----------------------------------------------------
-- Table `Thoth`.`order-product`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Thoth`.`order_product` ;

CREATE TABLE IF NOT EXISTS `Thoth`.`order_product` (
  `Id` INT(11) NOT NULL AUTO_INCREMENT,
  `OrderId` INT(11) NOT NULL,
  `ProductId` INT(11) NOT NULL,
  `Amount` DECIMAL(8,2) NOT NULL,
  PRIMARY KEY (`Id`),
  CONSTRAINT `fk_order_product_OrderId`
    FOREIGN KEY (`OrderId`)
    REFERENCES `Thoth`.`order` (`OrderId`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_order_product_ProductId`
    FOREIGN KEY (`ProductId`)
    REFERENCES `Thoth`.`product` (`ProductId`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

ALTER TABLE `Thoth`.`order_product` ADD INDEX `OrderId_idx` (`OrderId`);
ALTER TABLE `Thoth`.`order_product` ADD INDEX `ProductId_idx` (`ProductId`);


-- -----------------------------------------------------
-- Table `Thoth`.`state`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Thoth`.`state` ;

CREATE TABLE IF NOT EXISTS `Thoth`.`state` (
  `StateId` INT NOT NULL AUTO_INCREMENT,
  `Name` VARCHAR(45) NULL,
  PRIMARY KEY (`StateId`))
ENGINE = InnoDB;

ALTER TABLE `Thoth`.`state` ADD UNIQUE `Name_idx` (`Name`);


-- -----------------------------------------------------
-- Table `Thoth`.`state-of-order`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Thoth`.`state_of_order` ;

CREATE TABLE IF NOT EXISTS `Thoth`.`state_of_order` (
  `Id` INT NOT NULL AUTO_INCREMENT,
  `UserId` INT NOT NULL,
  `OrderId` INT NOT NULL,
  `StateId` INT NOT NULL,
  PRIMARY KEY (`Id`),
  CONSTRAINT `fk_state_of_order_UserId`
    FOREIGN KEY (`UserId`)
    REFERENCES `Thoth`.`user` (`UserId`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_state_of_order_OrderId`
    FOREIGN KEY (`OrderId`)
    REFERENCES `Thoth`.`order` (`OrderId`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_state_of_order_StateId`
    FOREIGN KEY (`StateId`)
    REFERENCES `Thoth`.`state` (`StateId`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

ALTER TABLE `Thoth`.`state_of_order` ADD INDEX `UserId_idx` (`UserId`);
ALTER TABLE `Thoth`.`state_of_order` ADD INDEX `OrderId_idx` (`OrderId`);
ALTER TABLE `Thoth`.`state_of_order` ADD INDEX `StateId_idx` (`StateId`);


SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
