-- MySQL Script generated by MySQL Workbench
-- Wed 11 Mar 2020 10:06:41 EET
-- 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='TRADITIONAL,ALLOW_INVALID_DATES';

-- -----------------------------------------------------
-- Schema inventory
-- -----------------------------------------------------

-- -----------------------------------------------------
-- Table `categories`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `categories` (
  `catid` INT NOT NULL AUTO_INCREMENT,
  `catname` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`catid`),
  UNIQUE INDEX `catid_UNIQUE` (`catid` ASC))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `locations`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `locations` (
  `locid` INT NOT NULL AUTO_INCREMENT,
  `locname` VARCHAR(100) NOT NULL,
  `loccode` VARCHAR(45) NOT NULL,
  `locinfo` VARCHAR(500) NULL,
  PRIMARY KEY (`locid`),
  UNIQUE INDEX `locid_UNIQUE` (`locid` ASC))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `items`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `items` (
  `itemid` INT NOT NULL AUTO_INCREMENT,
  `catid` INT NOT NULL,
  `locid` INT NOT NULL,
  `name` VARCHAR(100) NOT NULL,
  `code1` VARCHAR(50) NOT NULL,
  `code2` VARCHAR(50) NOT NULL,
  `text1` TEXT(500) NOT NULL,
  `text2` TEXT(500) NOT NULL,
  `text3` TEXT(500) NOT NULL,
  `qty` INT NOT NULL DEFAULT 1,
  `want` INT NOT NULL DEFAULT 0,
  `userid` INT NULL,
  `cost` DECIMAL(10,2) NULL,
  `price` DECIMAL(10,2) NULL,
  PRIMARY KEY (`itemid`),
  INDEX `fk_items_categories_idx` (`catid` ASC),
  INDEX `fk_items_locations_idx` (`locid` ASC),
  UNIQUE INDEX `itemid_UNIQUE` (`itemid` ASC),
  CONSTRAINT `fk_items_categories`
    FOREIGN KEY (`catid`)
    REFERENCES `categories` (`catid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_items_locations`
    FOREIGN KEY (`locid`)
    REFERENCES `locations` (`locid`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `labels`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `labels` (
  `rowid` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(50) NOT NULL,
  `value` VARCHAR(100) NOT NULL,
  PRIMARY KEY (`rowid`))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `permissions`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `permissions` (
  `userid` INT NOT NULL,
  `itemid` INT NOT NULL,
  `canedit` TINYINT(1) NOT NULL DEFAULT 0,
  PRIMARY KEY (`userid`, `itemid`),
  INDEX `fk_permissions_items_idx` (`itemid` ASC),
  CONSTRAINT `fk_permissions_items`
    FOREIGN KEY (`itemid`)
    REFERENCES `items` (`itemid`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `report_access_codes`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `report_access_codes` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `code` VARCHAR(45) NULL,
  `expires` DATETIME NULL,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `id_UNIQUE` (`id` ASC))
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `images`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `images` (
  `imageid` INT NOT NULL AUTO_INCREMENT,
  `itemid` INT NOT NULL,
  `imagename` VARCHAR(255) NOT NULL,
  `primary` TINYINT(1) NOT NULL DEFAULT 0,
  PRIMARY KEY (`imageid`, `itemid`),
  UNIQUE INDEX `imageid_UNIQUE` (`imageid` ASC),
  INDEX `fk_images_items_idx` (`itemid` ASC),
  CONSTRAINT `fk_images_items`
    FOREIGN KEY (`itemid`)
    REFERENCES `items` (`itemid`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `stock`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `stock` (
  `stockid` INT NOT NULL AUTO_INCREMENT,
  `itemid` INT NOT NULL,
  `stock` INT NOT NULL,
  `text1` TEXT(500) NOT NULL,
  `userid` INT NOT NULL,
  `timestamp` TIMESTAMP NOT NULL,
  PRIMARY KEY (`stockid`, `itemid`),
  UNIQUE INDEX `stockid_UNIQUE` (`stockid` ASC),
  INDEX `fk_stock_items_idx` (`itemid` ASC),
  CONSTRAINT `fk_stock_items`
    FOREIGN KEY (`itemid`)
    REFERENCES `items` (`itemid`)
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;


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

-- -----------------------------------------------------
-- Data for table `labels`
-- -----------------------------------------------------
START TRANSACTION;
INSERT INTO `labels` (`rowid`, `name`, `value`) VALUES (1, 'itemid', 'Item ID');
INSERT INTO `labels` (`rowid`, `name`, `value`) VALUES (2, 'catid', 'Category ID');
INSERT INTO `labels` (`rowid`, `name`, `value`) VALUES (3, 'itemname', 'Item Name');
INSERT INTO `labels` (`rowid`, `name`, `value`) VALUES (4, 'itemnumber1', 'Number Value 1');
INSERT INTO `labels` (`rowid`, `name`, `value`) VALUES (5, 'itemnumber2', 'Number Value 2');
INSERT INTO `labels` (`rowid`, `name`, `value`) VALUES (6, 'itemtext1', 'Text Value 1');
INSERT INTO `labels` (`rowid`, `name`, `value`) VALUES (7, 'itemtext2', 'Text Value 2');
INSERT INTO `labels` (`rowid`, `name`, `value`) VALUES (8, 'itemtext3', 'Text Value 3');
INSERT INTO `labels` (`rowid`, `name`, `value`) VALUES (9, 'catname', 'Category Name');

COMMIT;

