Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

database table not being created

I am designing a sql db for business listings in my local area. I am using mySQL WorkBench to design the DB, how ever I am having some issues with one of the tables in the DB not being able to be created.

This is what the db design looks like

enter image description here

The issue I am having is with BusinessHours, I am finding it abit confusing because I want to relate it with a Foregin key to BusinessDirectory (each businessDirectory has many BusinessHours) 'one to many' so that you can represent the 7 days of the week. Hope that makes sense.

This is the sql generated using mySQL workbench

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';

CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;

-- -----------------------------------------------------
-- Table `mydb`.`Members`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Members` ;

CREATE  TABLE IF NOT EXISTS `mydb`.`Members` (
  `idMembers` INT NOT NULL AUTO_INCREMENT ,
  `firstName` VARCHAR(45) NOT NULL ,
  `lastName` VARCHAR(45) NOT NULL ,
  `email` VARCHAR(45) NOT NULL ,
  `password` VARCHAR(45) NOT NULL ,
  PRIMARY KEY (`idMembers`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`BusinessDirectory`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`BusinessDirectory` ;

CREATE  TABLE IF NOT EXISTS `mydb`.`BusinessDirectory` (
  `idBusinessDirectory` INT NOT NULL ,
  `businessName` VARCHAR(45) NOT NULL ,
  `businessDescription` VARCHAR(1000) NULL ,
  `businessLogo` VARCHAR(45) NULL ,
  `idMembers` INT NULL ,
  `directoryCategory` VARCHAR(45) NULL ,
  PRIMARY KEY (`idBusinessDirectory`) ,
  INDEX `idMembers_idx` (`idMembers` ASC) ,
  CONSTRAINT `idMembers`
    FOREIGN KEY (`idMembers` )
    REFERENCES `mydb`.`Members` (`idMembers` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`BusinessAddress`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`BusinessAddress` ;

CREATE  TABLE IF NOT EXISTS `mydb`.`BusinessAddress` (
  `idBusinessDirectory` INT NOT NULL ,
  `addressNumber` VARCHAR(5) NULL ,
  `addressAreaName` VARCHAR(45) NULL ,
  `addressLat` FLOAT(10,6) NULL ,
  `addressLong` FLOAT(10,6) NULL ,
  PRIMARY KEY (`idBusinessDirectory`) ,
  CONSTRAINT `idBusinessDirectory`
    FOREIGN KEY (`idBusinessDirectory` )
    REFERENCES `mydb`.`BusinessDirectory` (`idBusinessDirectory` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`BusinessHours`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`BusinessHours` ;

CREATE  TABLE IF NOT EXISTS `mydb`.`BusinessHours` (
  `idBusinessDirectory` INT NOT NULL ,
  `dayOfWeek` INT NULL ,
  `openingTime` TIME NULL ,
  `closingTime` TIME NULL ,
  PRIMARY KEY (`idBusinessDirectory`) ,
  CONSTRAINT `idBusinessDirectory`
    FOREIGN KEY (`idBusinessDirectory` )
    REFERENCES `mydb`.`BusinessDirectory` (`idBusinessDirectory` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

USE `mydb` ;


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

So I was hoping that someone can help me figure out why I am unable to add BusinessHours table to my db.

Here are two screenshots showing the error msgs I am reciving in phpMyAdmin

enter image description here

enter image description here

any help would be appreciated

Final Updated sql;

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';

CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;

-- -----------------------------------------------------
-- Table `mydb`.`Members`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Members` ;

CREATE  TABLE IF NOT EXISTS `mydb`.`Members` (
  `idMembers` INT NOT NULL AUTO_INCREMENT ,
  `firstName` VARCHAR(45) NOT NULL ,
  `lastName` VARCHAR(45) NOT NULL ,
  `email` VARCHAR(45) NOT NULL ,
  `password` VARCHAR(45) NOT NULL ,
  PRIMARY KEY (`idMembers`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`BusinessDirectory`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`BusinessDirectory` ;

CREATE  TABLE IF NOT EXISTS `mydb`.`BusinessDirectory` (
  `idBusinessDirectory` INT NOT NULL ,
  `businessName` VARCHAR(45) NOT NULL ,
  `businessDescription` VARCHAR(1000) NULL ,
  `businessLogo` VARCHAR(45) NULL ,
  `idMembers` INT NULL ,
  `directoryCategory` VARCHAR(45) NULL ,
  PRIMARY KEY (`idBusinessDirectory`) ,
  INDEX `idMembers_idx` (`idMembers` ASC) ,
  CONSTRAINT `idMembers`
    FOREIGN KEY (`idMembers` )
    REFERENCES `mydb`.`Members` (`idMembers` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`BusinessAddress`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`BusinessAddress` ;

CREATE  TABLE IF NOT EXISTS `mydb`.`BusinessAddress` (
  `idBusinessDirectory_BusinessAddress` INT NOT NULL ,
  `addressNumber` VARCHAR(5) NULL ,
  `addressAreaName` VARCHAR(45) NULL ,
  `addressLat` FLOAT(10,6) NULL ,
  `addressLong` FLOAT(10,6) NULL ,
  PRIMARY KEY (`idBusinessDirectory_BusinessAddress`) ,
  CONSTRAINT `idBusinessDirectory_BusinessAddress`
    FOREIGN KEY (`idBusinessDirectory_BusinessAddress` )
    REFERENCES `mydb`.`BusinessDirectory` (`idBusinessDirectory` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`BusinessHours`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`BusinessHours` ;

CREATE  TABLE IF NOT EXISTS `mydb`.`BusinessHours` (
  `idBusinessDirectory_BusinessHours` INT NOT NULL ,
  `dayOfWeek` INT NULL ,
  `openingTime` TIME NULL ,
  `closingTime` TIME NULL ,
  PRIMARY KEY (`idBusinessDirectory_BusinessHours`) ,
  CONSTRAINT `idBusinessDirectory_BusinessHours`
    FOREIGN KEY (`idBusinessDirectory_BusinessHours` )
    REFERENCES `mydb`.`BusinessDirectory` (`idBusinessDirectory` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

USE `mydb` ;


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

this is what the Foregin key tab should look like

enter image description here

thanks for the help guys

like image 904
HurkNburkS Avatar asked Nov 13 '22 09:11

HurkNburkS


1 Answers

The constraint name idBusinessDirectory for foreign key is repeated in table BusinessHours and BusinessAddress .

Give different name to the constraint.

UPDATED

Follow a proper FK naming convention so that such error never pops up and just by the name of constraint we can know the tables involved in the constraint.

fk_[referencing table name]_[referenced table name]_[referencing field name]

So in your case the constraints will be

EDIT Update your code with this

BusinessAddress Table

CONSTRAINT `fk_BusinessAddress_BusinessDirectory_idBusinessDirectory`
FOREIGN KEY (`idBusinessDirectory` )
REFERENCES `mydb`.`BusinessDirectory` (`idBusinessDirectory` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)


BusinessHours Table

CONSTRAINT `fk_BusinessHours_BusinessDirectory_idBusinessDirectory`
FOREIGN KEY (`idBusinessDirectory` )
REFERENCES `mydb`.`BusinessDirectory` (`idBusinessDirectory` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)

All your constraints are stored in this INFORMATION_SCHEMA.KEY_COLUMN_USAGE in INNODB

REFER

Hope it helps.

like image 165
Meherzad Avatar answered Nov 15 '22 05:11

Meherzad