简体   繁体   中英

MySQL Referencing Same Foreign key twice

Trying to do some data modelling. I'm trying to track two bots in my Games, Matches, and Turns tables. These bots are listed in the Bots table, and in the three mentioned tables the foriegn key needs to appear twice (once for each bot).

This database is intended to record the results of two AI's competing against each other

Not sure if this is good practice for the model, but when trying to implement this I get errorno: 150. Not understanding how to resolve this issue. Any help and advice would be appreciated. SQL code listed below.

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;

数据模型

Think I found a viable solution here with modifications to my model.

I defined many to many relationships and ended up creating 3 more tables to associate bots against turns, games, and matches. Below is an updated screen shot of the model to illustrate.

Updated_model

Ran this against mysql and it accepted it without error. Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM