MySQL引用相同的外键两次

时间:2013-07-06 04:43:29

标签: mysql mysql-workbench

尝试做一些数据建模。我正试图在我的Games,Matches和Turns表中跟踪两个机器人。这些机器人在Bots表中列出,在上面提到的三个表中,foriegn键需要出现两次(每个机器人一次)。

此数据库旨在记录两个AI相互竞争的结果

不确定这是否是该模型的良好实践,但在尝试实现此时,我得到errorno:150。不了解如何解决此问题。任何帮助和建议将不胜感激。下面列出的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 `battleship` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `battleship` ;

-- -----------------------------------------------------
-- Table `battleship`.`Security`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `battleship`.`Security` ;

CREATE  TABLE IF NOT EXISTS `battleship`.`Security` (
  `SecurityID` INT NOT NULL ,
  `Level` VARCHAR(45) NULL ,
  `Description` VARCHAR(200) NULL ,
  PRIMARY KEY (`SecurityID`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `battleship`.`Users`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `battleship`.`Users` ;

CREATE  TABLE IF NOT EXISTS `battleship`.`Users` (
  `UserID` INT NOT NULL ,
  `Username` VARCHAR(45) NULL ,
  `First_name` VARCHAR(45) NULL ,
  `Last_name` VARCHAR(45) NULL ,
  `Email` VARCHAR(45) NULL ,
  `Student Number` INT NULL ,
  `Enabled` BINARY NULL ,
  `SecurityID` INT NOT NULL ,
  PRIMARY KEY (`UserID`) ,
  INDEX `fk_Users_Security_idx` (`SecurityID` ASC) ,
  CONSTRAINT `fk_Users_Security`
    FOREIGN KEY (`SecurityID` )
    REFERENCES `battleship`.`Security` (`SecurityID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `battleship`.`News`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `battleship`.`News` ;

CREATE  TABLE IF NOT EXISTS `battleship`.`News` (
  `EventID` INT NOT NULL ,
  `Event_Name` VARCHAR(45) NULL ,
  `Event_Date` DATETIME NULL ,
  `Event_Description` VARCHAR(45) NULL ,
  `UserID` INT NOT NULL ,
  PRIMARY KEY (`EventID`, `UserID`) ,
  INDEX `fk_News_Users1_idx` (`UserID` ASC) ,
  CONSTRAINT `fk_News_Users1`
    FOREIGN KEY (`UserID` )
    REFERENCES `battleship`.`Users` (`UserID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `battleship`.`Bots`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `battleship`.`Bots` ;

CREATE  TABLE IF NOT EXISTS `battleship`.`Bots` (
  `BotID` INT NOT NULL ,
  `Name` VARCHAR(45) NULL ,
  `UserID` INT NOT NULL ,
  `Revision` INT NULL ,
  `SubmissionDate` DATETIME NULL ,
  `Approved` BINARY NULL ,
  PRIMARY KEY (`BotID`, `UserID`) ,
  INDEX `fk_Bots_Users1_idx` (`UserID` ASC) ,
  CONSTRAINT `fk_Bots_Users1`
    FOREIGN KEY (`UserID` )
    REFERENCES `battleship`.`Users` (`UserID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `battleship`.`Competitions`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `battleship`.`Competitions` ;

CREATE  TABLE IF NOT EXISTS `battleship`.`Competitions` (
  `CompetitionsID` INT NOT NULL ,
  `CompetitionName` VARCHAR(45) NULL ,
  `Description` VARCHAR(45) NULL ,
  `Date` VARCHAR(45) NULL ,
  PRIMARY KEY (`CompetitionsID`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `battleship`.`Matches`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `battleship`.`Matches` ;

CREATE  TABLE IF NOT EXISTS `battleship`.`Matches` (
  `MatchID` INT NOT NULL ,
  `Winner` VARCHAR(45) NULL ,
  `Bots_BotID1` INT NOT NULL ,
  `Bots_BotID2` INT NOT NULL ,
  `CompetitionsID` INT NOT NULL ,
  PRIMARY KEY (`MatchID`) ,
  INDEX `fk_Matches_Bots1_idx` (`Bots_BotID1` ASC, `Bots_BotID2` ASC) ,
  INDEX `fk_Matches_Competitions1_idx` (`CompetitionsID` ASC) ,
  CONSTRAINT `fk_Matches_Bots1`
    FOREIGN KEY (`Bots_BotID1` , `Bots_BotID2` )
    REFERENCES `battleship`.`Bots` (`BotID` , `BotID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Matches_Competitions1`
    FOREIGN KEY (`CompetitionsID` )
    REFERENCES `battleship`.`Competitions` (`CompetitionsID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `battleship`.`Entrants`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `battleship`.`Entrants` ;

CREATE  TABLE IF NOT EXISTS `battleship`.`Entrants` (
  `EntryID` INT NOT NULL ,
  `Bots_BotID` INT NOT NULL ,
  `Competitions_CompetitionsID` INT NOT NULL ,
  PRIMARY KEY (`EntryID`) ,
  INDEX `fk_Entrants_Bots1_idx` (`Bots_BotID` ASC) ,
  INDEX `fk_Entrants_Competitions1_idx` (`Competitions_CompetitionsID` ASC) ,
  CONSTRAINT `fk_Entrants_Bots1`
    FOREIGN KEY (`Bots_BotID` )
    REFERENCES `battleship`.`Bots` (`BotID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Entrants_Competitions1`
    FOREIGN KEY (`Competitions_CompetitionsID` )
    REFERENCES `battleship`.`Competitions` (`CompetitionsID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `battleship`.`Games`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `battleship`.`Games` ;

CREATE  TABLE IF NOT EXISTS `battleship`.`Games` (
  `GameID` INT NOT NULL ,
  `Matches_MatchID` INT NOT NULL ,
  `Bots_BotID1` INT NOT NULL ,
  `Bots_BotID2` INT NOT NULL ,
  `Winner` VARCHAR(45) NULL ,
  PRIMARY KEY (`GameID`) ,
  INDEX `fk_Games_Matches1_idx` (`Matches_MatchID` ASC) ,
  INDEX `fk_Games_Bots1_idx` (`Bots_BotID1` ASC, `Bots_BotID2` ASC) ,
  CONSTRAINT `fk_Games_Matches1`
    FOREIGN KEY (`Matches_MatchID` )
    REFERENCES `battleship`.`Matches` (`MatchID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Games_Bots1`
    FOREIGN KEY (`Bots_BotID1` , `Bots_BotID2` )
    REFERENCES `battleship`.`Bots` (`BotID` , `BotID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `battleship`.`Turns`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `battleship`.`Turns` ;

CREATE  TABLE IF NOT EXISTS `battleship`.`Turns` (
  `TurnID` INT NOT NULL ,
  `Bots_BotID1` INT NOT NULL ,
  `Bots_BotID2` INT NOT NULL ,
  `Bot1Move` VARCHAR(45) NULL ,
  `Bot2Move` VARCHAR(45) NULL ,
  `ThinkingTime` VARCHAR(45) NULL ,
  `Turnscol` VARCHAR(45) NULL ,
  `Games_GameID` INT NOT NULL ,
  PRIMARY KEY (`TurnID`) ,
  INDEX `fk_Turns_Bots1_idx` (`Bots_BotID1` ASC, `Bots_BotID2` ASC) ,
  INDEX `fk_Turns_Games1_idx` (`Games_GameID` ASC) ,
  CONSTRAINT `fk_Turns_Bots1`
    FOREIGN KEY (`Bots_BotID1` , `Bots_BotID2` )
    REFERENCES `battleship`.`Bots` (`BotID` , `BotID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Turns_Games1`
    FOREIGN KEY (`Games_GameID` )
    REFERENCES `battleship`.`Games` (`GameID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

USE `battleship` ;


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

datamodel

1 个答案:

答案 0 :(得分:0)

想想我在这里找到了一个可行的解决方案,并修改了我的模型。

我定义了许多关系,最终创建了3个表来将机器人与转弯,游戏和比赛相关联。下面是模型的更新屏幕截图,用于说明。

updated_model

对抗mysql,它接受了它没有错误。干杯