数据库表未创建

时间:2013-03-19 07:23:19

标签: mysql phpmyadmin mysql-workbench

我正在为我所在地区的商家信息设计一个sql db。我正在使用mySQL WorkBench来设计数据库,但是我如何解决数据库中某个表无法创建的问题。

这就是db设计的样子

enter image description here

我遇到的问题是BusinessHours,我发现它令人困惑,因为我想将它与BusinessDirectory的Foregin键(每个businessDirectory有很多BusinessHours)“一对多”联系起来,这样你就可以代表7天了一周中的。希望这是有道理的。

这是使用mySQL workbench生成的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` 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;

所以我希望有人可以帮我找出为什么我无法将BusinessHours表添加到我的数据库中。

以下两个屏幕截图显示了我在phpMyAdmin中收到的错误信息

enter image description here

enter image description here

任何帮助将不胜感激

最终更新的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;

这就是Foregin键标签应该是什么样子

enter image description here

感谢帮助人员

1 个答案:

答案 0 :(得分:1)

外键的约束名idBusinessDirectory在表BusinessHoursBusinessAddress中重复。

为约束指定不同的名称。

<强>已更新

遵循正确的FK命名约定,以便永远不会弹出这样的错误,只需通过约束名称,我们就可以知道约束中涉及的表。

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

所以在你的情况下,约束将是

编辑使用此

更新您的代码
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)

所有约束都存储在INNODB中的INFORMATION_SCHEMA.KEY_COLUMN_USAGE

REFER

希望它有所帮助。