0

尝试做一些数据建模。我正在尝试在我的游戏、比赛和转牌桌上跟踪两个机器人。这些机器人在 Bots 表中列出,在提到的三个表中,外键需要出现两次(每个机器人一次)。

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

不确定这是否是模型的好习惯,但在尝试实施时,我得到错误号: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;

数据模型

4

1 回答 1

0

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

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

更新模型

对mysql运行这个并且它没有错误地接受它。干杯

于 2013-07-06T19:00:22.650 回答