MySQL won't create my modeled database - mysql

I'm modeling my database with MySQL Wordbench in a EER Model, which is this :
So after modeling my database I export to a SQL script and try to run it, but it creates only three tables:
Why is that happening ?
It shouldn't create all tables ?
This is the generated script when I export:
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';
CREATE SCHEMA IF NOT EXISTS `brainset` DEFAULT CHARACTER SET latin1 COLLATE latin1_general_ci ;
USE `brainset` ;
-- -----------------------------------------------------
-- Table `brainset`.`departamento`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brainset`.`departamento` (
`ID` TINYINT UNSIGNED NOT NULL ,
`departamento` VARCHAR(50) NOT NULL ,
PRIMARY KEY (`ID`) ,
UNIQUE INDEX `departamento_UNIQUE` (`departamento` ASC) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brainset`.`documento_escopo`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brainset`.`documento_escopo` (
`ID` TINYINT UNSIGNED NOT NULL ,
`escopo` CHAR(7) NOT NULL ,
PRIMARY KEY (`ID`) ,
UNIQUE INDEX `escopo_UNIQUE` (`escopo` ASC) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brainset`.`procedimento_tipo`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brainset`.`procedimento_tipo` (
`ID` TINYINT UNSIGNED NOT NULL ,
`tipo` VARCHAR(50) NOT NULL ,
PRIMARY KEY (`ID`) ,
UNIQUE INDEX `tipo_UNIQUE` (`tipo` ASC) )
ENGINE = InnoDB
COMMENT = ' ';
-- -----------------------------------------------------
-- Table `brainset`.`procedimento`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brainset`.`procedimento` (
`ID` SMALLINT UNSIGNED NOT NULL ,
`nome` VARCHAR(100) NOT NULL ,
`descricao` VARCHAR(1024) NOT NULL ,
`id_tipo` TINYINT UNSIGNED NOT NULL ,
`id_departamento` TINYINT UNSIGNED NOT NULL ,
`id_documento_complementar` INT UNSIGNED NOT NULL ,
PRIMARY KEY (`ID`) ,
INDEX `fk_procedimento-procedimento_tipo` (`id_tipo` ASC, `id_departamento` ASC) ,
INDEX `fk_procedimento-departamento` (`id_departamento` ASC) ,
CONSTRAINT `fk_procedimento-procedimento_tipo`
FOREIGN KEY (`id_tipo` , `id_departamento` )
REFERENCES `brainset`.`procedimento_tipo` (`ID` , `ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_procedimento-departamento`
FOREIGN KEY (`id_departamento` )
REFERENCES `brainset`.`departamento` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brainset`.`usuario`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brainset`.`usuario` (
`ID` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`nome` VARCHAR(100) NOT NULL ,
`foto` VARCHAR(200) NULL ,
`email` VARCHAR(45) NOT NULL ,
`senha` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`ID`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brainset`.`documento`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brainset`.`documento` (
`ID` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`id_procedimento` SMALLINT UNSIGNED NOT NULL ,
`data` DATETIME NOT NULL ,
`revisao` TINYINT NOT NULL ,
`id_escopo` TINYINT UNSIGNED NOT NULL ,
`id_documento_complementar` INT UNSIGNED NULL ,
`id_usuario` INT UNSIGNED NOT NULL ,
PRIMARY KEY (`ID`) ,
INDEX `fk_documento-documento_escopo` (`id_escopo` ASC) ,
INDEX `fk_documento-procedimento` (`id_procedimento` ASC) ,
INDEX `fk_documento-documento` (`id_documento_complementar` ASC) ,
INDEX `fk_documento-usuario` (`id_usuario` ASC) ,
CONSTRAINT `fk_documento-documento_escopo`
FOREIGN KEY (`id_escopo` )
REFERENCES `brainset`.`documento_escopo` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_documento-procedimento`
FOREIGN KEY (`id_procedimento` )
REFERENCES `brainset`.`procedimento` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_documento-documento`
FOREIGN KEY (`id_documento_complementar` )
REFERENCES `brainset`.`documento` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_documento-usuario`
FOREIGN KEY (`id_usuario` )
REFERENCES `brainset`.`usuario` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brainset`.`questao`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brainset`.`questao` (
`ID` INT UNSIGNED NOT NULL ,
`questao` VARCHAR(1024) NOT NULL ,
`descricao` VARCHAR(1024) NULL ,
`observacao` VARCHAR(1024) NULL ,
`data` DATETIME NOT NULL ,
PRIMARY KEY (`ID`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brainset`.`questao_tipo`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brainset`.`questao_tipo` (
`ID` TINYINT UNSIGNED NOT NULL ,
`nome` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`ID`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brainset`.`questao_campo`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brainset`.`questao_campo` (
`ID` INT UNSIGNED NOT NULL ,
`id_questao` INT NOT NULL ,
`id_questao_tipo` TINYINT NOT NULL ,
`descricao` VARCHAR(1024) NULL ,
PRIMARY KEY (`ID`) ,
INDEX `fk_questao_campo-questao` (`id_questao` ASC) ,
INDEX `fk_questao_campo-questao-tipo` (`id_questao_tipo` ASC) ,
CONSTRAINT `fk_questao_campo-questao`
FOREIGN KEY (`id_questao` )
REFERENCES `brainset`.`questao` (`ID` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_questao_campo-questao-tipo`
FOREIGN KEY (`id_questao_tipo` )
REFERENCES `brainset`.`questao_tipo` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brainset`.`questao_escolha`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brainset`.`questao_escolha` (
`ID` INT UNSIGNED NOT NULL ,
`id_questao_campo` INT NOT NULL ,
`nome` VARCHAR(64) NOT NULL ,
`valor` VARCHAR(64) NOT NULL ,
PRIMARY KEY (`ID`) ,
INDEX `fk_questao_escolha-questao_campo` (`id_questao_campo` ASC) ,
CONSTRAINT `fk_questao_escolha-questao_campo`
FOREIGN KEY (`id_questao_campo` )
REFERENCES `brainset`.`questao_campo` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brainset`.`questao_resposta`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brainset`.`questao_resposta` (
`ID` INT UNSIGNED NOT NULL ,
`id_questao` INT NOT NULL ,
`resposta` VARCHAR(1024) NOT NULL ,
`data` DATETIME NOT NULL ,
PRIMARY KEY (`ID`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brainset`.`questao_consulta`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brainset`.`questao_consulta` (
`ID` INT UNSIGNED NOT NULL ,
`id_usuario` INT NOT NULL ,
`id_questao` INT NOT NULL ,
`id_questao_resposta` INT NOT NULL ,
`data` TIMESTAMP NOT NULL ,
PRIMARY KEY (`ID`) ,
INDEX `fk_questao_consulta-usuario` (`id_usuario` ASC) ,
INDEX `fk_questao_consulta-questao` (`id_questao` ASC) ,
INDEX `fk_questao_consulta-questao_resposta` (`id_questao_resposta` ASC) ,
CONSTRAINT `fk_questao_consulta-usuario`
FOREIGN KEY (`id_usuario` )
REFERENCES `brainset`.`usuario` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_questao_consulta-questao`
FOREIGN KEY (`id_questao` )
REFERENCES `brainset`.`questao` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_questao_consulta-questao_resposta`
FOREIGN KEY (`id_questao_resposta` )
REFERENCES `brainset`.`questao_resposta` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
COMMENT = ' ';
-- -----------------------------------------------------
-- Table `brainset`.`departamento_equipe`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brainset`.`departamento_equipe` (
`ID` INT UNSIGNED NOT NULL ,
`id_departamento` TINYINT UNSIGNED NOT NULL ,
`id_usuario` INT UNSIGNED NOT NULL ,
PRIMARY KEY (`ID`) ,
INDEX `fk_departamento_equipe-departamento` (`id_departamento` ASC) ,
INDEX `fk_departamento_equipe-usuario` (`id_usuario` ASC) ,
CONSTRAINT `fk_departamento_equipe-departamento`
FOREIGN KEY (`id_departamento` )
REFERENCES `brainset`.`departamento` (`ID` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_departamento_equipe-usuario`
FOREIGN KEY (`id_usuario` )
REFERENCES `brainset`.`usuario` (`ID` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
Thanks.

Just in case anyone stumbles upon this; you can't create a table with a relation to a table that's not created yet.
When MySQL creates a relation it links the table you're creating the relation for to the table you're referencing, if the referencing table doesn't exist, MySQL won't be happy about it.
So when creating a lot of tables that reference each other, make sure that any table that is referenced by another is created before the table that references it.

Related

MySQL Foreign Key error 1452

When I go to try to add records to my client and/or the site table I get the following error.
Schema Creation Failed: Cannot add or update a child row: a foreign key constraint fails (db_2_6ceaf.client, CONSTRAINT client2offer FOREIGN KEY (clientID) REFERENCES offer_to_client (clientID) ON DELETE NO ACTION ON UPDATE NO ACTION):
Schema:
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';
-- -----------------------------------------------------
-- Table `offer_to_category`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `offer_to_category` (
`offerID` INT UNSIGNED NOT NULL ,
`categoryID` INT UNSIGNED NOT NULL ,
INDEX `offer_to_category` (`offerID` ASC, `categoryID` ASC) ,
INDEX `o2c_categoryID` (`categoryID` ASC) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `offer_to_client`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `offer_to_client` (
`offerID` INT UNSIGNED NOT NULL ,
`clientID` INT UNSIGNED NOT NULL ,
INDEX `offer_to_client` (`offerID` ASC, `clientID` ASC) ,
INDEX `o2cl_clientID` (`clientID` ASC) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `offer`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `offer` (
`offerID` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`offerName` VARCHAR(255) NULL ,
`offerDescription` LONGTEXT NULL ,
`offerAction` TEXT NULL ,
`offerStart` BIGINT NULL ,
`offerEnd` BIGINT NULL ,
`offerStatus` TINYINT(1) NULL ,
PRIMARY KEY (`offerID`) ,
CONSTRAINT `offer2cat`
FOREIGN KEY (`offerID` )
REFERENCES `offer_to_category` (`offerID` )
ON DELETE CASCADE
ON UPDATE NO ACTION,
CONSTRAINT `offer2client`
FOREIGN KEY (`offerID` )
REFERENCES `offer_to_client` (`offerID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `category`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `category` (
`categoryID` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`categoryName` VARCHAR(255) NULL ,
`categoryDescription` LONGTEXT NULL ,
`categoryStatus` TINYINT(1) NULL ,
PRIMARY KEY (`categoryID`) ,
CONSTRAINT `cat2offer`
FOREIGN KEY (`categoryID` )
REFERENCES `offer_to_category` (`categoryID` )
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `user` (
`userID` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`clientID` INT UNSIGNED NOT NULL ,
`userEmail` VARCHAR(255) NULL ,
`userFirstName` VARCHAR(255) NULL ,
`userLastName` VARCHAR(255) NULL ,
`userRegistered` BIGINT NULL ,
`userStatus` TINYINT(1) NULL ,
PRIMARY KEY (`userID`) ,
INDEX `client` (`clientID` ASC, `userEmail` ASC) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `client`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `client` (
`clientID` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`siteID` INT UNSIGNED NOT NULL ,
`clientName` VARCHAR(255) NULL ,
`clientDescription` LONGTEXT NULL ,
`clientUrl` LONGTEXT NULL ,
`clientStatus` TINYINT(1) NULL ,
PRIMARY KEY (`clientID`) ,
INDEX `clientsiteid` (`siteID` ASC) ,
CONSTRAINT `client2offer`
FOREIGN KEY (`clientID` )
REFERENCES `offer_to_client` (`clientID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `client2user`
FOREIGN KEY (`clientID` )
REFERENCES `user` (`clientID` )
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `admins`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `admins` (
`adminID` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`adminName` VARCHAR(255) NULL ,
`adminEmail` LONGTEXT NULL ,
`adminUsername` VARCHAR(255) NULL ,
`adminStatus` TINYINT(1) NULL ,
PRIMARY KEY (`adminID`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `site`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `site` (
`siteID` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`siteName` VARCHAR(255) NULL ,
`siteURL` LONGTEXT NULL ,
`siteStyles` LONGTEXT NULL ,
`siteImages` LONGTEXT NULL ,
`sitesStatus` TINYINT(1) NULL ,
PRIMARY KEY (`siteID`) ,
CONSTRAINT `site2client`
FOREIGN KEY (`siteID` )
REFERENCES `client` (`siteID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
-- -----------------------------------------------------
-- Data for table `client`
-- -----------------------------------------------------
START TRANSACTION;
INSERT INTO `client` (`clientID`, `siteID`, `clientName`, `clientDescription`, `clientUrl`, `clientStatus`) VALUES (NULL, 1, 'Weight Watchers', 'Weight Watchers microsite', 'weightwachers', 1);
INSERT INTO `client` (`clientID`, `siteID`, `clientName`, `clientDescription`, `clientUrl`, `clientStatus`) VALUES (NULL, 1, 'A Uni', NULL, NULL, 1);
INSERT INTO `client` (`clientID`, `siteID`, `clientName`, `clientDescription`, `clientUrl`, `clientStatus`) VALUES (NULL, 2, 'Ollie Biz', '', NULL, 1);
INSERT INTO `client` (`clientID`, `siteID`, `clientName`, `clientDescription`, `clientUrl`, `clientStatus`) VALUES (NULL, 2, 'Ollie Uni', NULL, 'ollieuni', 1);
COMMIT;
-- -----------------------------------------------------
-- Data for table `site`
-- -----------------------------------------------------
START TRANSACTION;
INSERT INTO `site` (`siteID`, `siteName`, `siteURL`, `siteStyles`, `siteImages`, `sitesStatus`) VALUES (1, 'We Love Rewards', 'http://www.weloverewards.co.uk', NULL, NULL, 1);
INSERT INTO `site` (`siteID`, `siteName`, `siteURL`, `siteStyles`, `siteImages`, `sitesStatus`) VALUES (2, 'Ollie Rewards', 'http://www.olliesrewards.co.uk', NULL, NULL, 1);
COMMIT;
I believe this constraint:
CONSTRAINT `client2offer`
FOREIGN KEY (`clientID` )
REFERENCES `offer_to_client` (`clientID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
Should be moved to be a constraint on the client_to_offer table, not on the client table. At the moment any insert in client must have a matching record in client_to_offer, which I believe is the wrong way around.

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.
Ran this against mysql and it accepted it without error. Cheers

MySql Error: #105 (Code 150). When I create my database schema I receive an error code of 150.

DROP SCHEMA IF EXISTS `YouthMinistry` ;
CREATE SCHEMA IF NOT EXISTS `YouthMinistry` DEFAULT CHARACTER SET utf16 COLLATE utf16_general_ci ;
USE `YouthMinistry` ;
-- -----------------------------------------------------
-- Table `YouthMinistry`.`group`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `YouthMinistry`.`group` ;
CREATE TABLE IF NOT EXISTS `YouthMinistry`.`group` (
`groupid` INT NOT NULL AUTO_INCREMENT ,
`group_name` VARCHAR(100) NOT NULL ,
`group_desc` VARCHAR(255) NULL ,
PRIMARY KEY (`groupid`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `YouthMinistry`.`groupmembers`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `YouthMinistry`.`groupmembers` ;
CREATE TABLE IF NOT EXISTS `YouthMinistry`.`groupmembers` (
`groupid` INT NOT NULL ,
`memberid` INT NOT NULL ,
PRIMARY KEY (`groupid`, `memberid`) ,
INDEX `groupid_idx` (`groupid` ASC) ,
CONSTRAINT `groupid`
FOREIGN KEY (`groupid` )
REFERENCES `YouthMinistry`.`group` (`groupid` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `YouthMinistry`.`role`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `YouthMinistry`.`role` ;
CREATE TABLE IF NOT EXISTS `YouthMinistry`.`role` (
`roleid` INT NOT NULL AUTO_INCREMENT ,
`role_name` VARCHAR(100) NOT NULL ,
`role_desc` VARCHAR(255) NULL ,
PRIMARY KEY (`roleid`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `YouthMinistry`.`rolemembers`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `YouthMinistry`.`rolemembers` ;
CREATE TABLE IF NOT EXISTS `YouthMinistry`.`rolemembers` (
`roleid` INT NOT NULL ,
`memberid` INT NOT NULL ,
PRIMARY KEY (`roleid`, `memberid`) ,
INDEX `groupid_idx` (`roleid` ASC) ,
FOREIGN KEY (`roleid` )
REFERENCES `YouthMinistry`.`role` (`roleid` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `YouthMinistry`.`users`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `YouthMinistry`.`users` ;
CREATE TABLE IF NOT EXISTS `YouthMinistry`.`users` (
`memberid` INT NOT NULL AUTO_INCREMENT ,
`username` VARCHAR(45) NOT NULL ,
`password` VARCHAR(45) NOT NULL ,
`email` VARCHAR(100) NULL ,
`first_name` VARCHAR(45) NOT NULL ,
`last_name` VARCHAR(45) NOT NULL ,
`created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated` DATETIME NOT NULL ,
PRIMARY KEY (`memberid`, `username`, `password`) ,
UNIQUE INDEX `username_UNIQUE` (`username` ASC) ,
INDEX `memberid_idx` (`memberid` ASC) ,
INDEX `memberid_idx1` (`memberid` ASC) ,
FOREIGN KEY (`memberid` )
REFERENCES `YouthMinistry`.`groupmembers` (`memberid` )
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (`memberid` )
REFERENCES `YouthMinistry`.`rolemembers` (`memberid` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `YouthMinistry`.`eventgroup`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `YouthMinistry`.`eventgroup` ;
CREATE TABLE IF NOT EXISTS `YouthMinistry`.`eventgroup` (
`eventid` INT NOT NULL ,
`groupid` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`eventid`, `groupid`) ,
INDEX `groupid_idx` (`groupid` ASC) ,
FOREIGN KEY (`groupid`)
REFERENCES `YouthMinistry`.`group` (`groupid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `YouthMinistry`.`event`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `YouthMinistry`.`event` ;
CREATE TABLE IF NOT EXISTS `YouthMinistry`.`event` (
`eventid` INT NOT NULL AUTO_INCREMENT ,
`event_name` VARCHAR(255) NOT NULL ,
`event_desc` VARCHAR(255) NULL ,
PRIMARY KEY (`eventid`) ,
INDEX `eventid_idx` (`eventid` ASC) ,
FOREIGN KEY (`eventid` )
REFERENCES `YouthMinistry`.`eventgroup` (`eventid` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
I'm working on creating a database for a website that I'm making and when I run the create script it gives me the following error:
Error Code: 1005. Can't create table 'youthministry.users' (errno: 150)
I've looked at the following sources for possible solutions to this error:
http://www.webdeveloper.com/forum/showthread.php?68260-mySQL-multiple-foreign-keys
http://forums.devarticles.com/mysql-development-50/mysql-foreign-key-problem-errno-150t-7704.html
I've checked the primary and foreign key declarations just to make sure everything is correct.
Any help is much appreciated. Also this is still a prototype and any comments on the initial schema is welcome. I'm still new at database design.
You can only create a foreign key on one table that references a key on another table. This specific problem is that memberid is not a key on either groupmembers or rolemembers tables. Simply add KEY (memberid) to those tables and you'll be good to go.
Another issue us that foreign key types must match. eventgroup has groupid varchar, but is referencing the groups table, which has groupid INT. Correct this.
As for suggestions, I very strongly recommend that each primary key be only one column: your auto-increment surrogate key. You should make these unsigned integers too.

SQL Query Throws Error 150

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';
CREATE SCHEMA IF NOT EXISTS `rsmad` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `rsmad` ;
-- -----------------------------------------------------
-- Table `rsmad`.`app_flashobjects`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `rsmad`.`app_flashobjects` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`app_id` INT UNSIGNED NOT NULL ,
`flashobject_id` INT UNSIGNED NOT NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `rsmad`.`app_html`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `rsmad`.`app_html` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`app_id` INT UNSIGNED NOT NULL ,
`html_id` INT UNSIGNED NOT NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `rsmad`.`app_iframes`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `rsmad`.`app_iframes` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`app_id` INT UNSIGNED NOT NULL ,
`iframe_id` INT UNSIGNED NOT NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `rsmad`.`app_images`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `rsmad`.`app_images` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`app_id` INT UNSIGNED NOT NULL ,
`image_id` INT UNSIGNED NOT NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `rsmad`.`app_links`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `rsmad`.`app_links` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`app_id` INT UNSIGNED NOT NULL ,
`link_id` INT UNSIGNED NOT NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `rsmad`.`app_text`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `rsmad`.`app_text` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`app_id` INT UNSIGNED NOT NULL ,
`text_id` INT UNSIGNED NOT NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `rsmad`.`apps`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `rsmad`.`apps` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`webpage_id` INT UNSIGNED NOT NULL ,
`timestamp` DATETIME NULL ,
`status` ENUM('ok','malicious','good') NOT NULL DEFAULT 'ok' ,
PRIMARY KEY (`id`) ,
CONSTRAINT `fk_apps_app_flashobjects1`
FOREIGN KEY (`id` )
REFERENCES `rsmad`.`app_flashobjects` (`app_id` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_apps_app_html1`
FOREIGN KEY (`id` )
REFERENCES `rsmad`.`app_html` (`app_id` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_apps_app_iframes1`
FOREIGN KEY (`id` )
REFERENCES `rsmad`.`app_iframes` (`app_id` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_apps_app_images1`
FOREIGN KEY (`id` )
REFERENCES `rsmad`.`app_images` (`app_id` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_apps_app_links1`
FOREIGN KEY (`id` )
REFERENCES `rsmad`.`app_links` (`app_id` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_apps_app_text1`
FOREIGN KEY (`id` )
REFERENCES `rsmad`.`app_text` (`app_id` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
Can anyone explain what the issue with these queries is?
You are using polymorphic associations, which unfortunately can't be done with constraints because foreign keys can't share referencing columns. To get this working, give each foreign table reference their own column in apps:
CREATE TABLE IF NOT EXISTS `rsmad`.`apps` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`webpage_id` INT UNSIGNED NOT NULL ,
`timestamp` DATETIME NULL ,
`status` ENUM('ok','malicious','good') NOT NULL DEFAULT 'ok' ,
`flashobjects_id` INT UNSIGNED NOT NULL,
`html_id` INT UNSIGNED NOT NULL,
-- etc
PRIMARY KEY (`id`) ,
CONSTRAINT `fk_apps_app_flashobjects1`
FOREIGN KEY (`flashobjects_id` )
REFERENCES `rsmad`.`app_flashobjects` (`app_id` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_apps_app_html1`
FOREIGN KEY (`html_id` )
REFERENCES `rsmad`.`app_html` (`app_id` )
ON DELETE CASCADE
ON UPDATE CASCADE,
-- etc
Edit: Actually, I think you should define the foreign key relationship the other way around. Foreign keys are defined in the child table and apps looks very much like a parent table to me. Now if you delete a row in any of the app_% tables, the entry in apps which they are related to will be cascade deleted.

Unable to create tables in SQL script

I am having a problem running this script... every time I try to run it I get (2)cannot create table errors (1) for members table and (1) for session table
What am I overlooking?
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';
DROP SCHEMA IF EXISTS `test_db` ;
CREATE SCHEMA IF NOT EXISTS `test_db` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `test_db` ;
-- -----------------------------------------------------
-- Table `test_db`.`role`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_db`.`role` ;
CREATE TABLE IF NOT EXISTS `test_db`.`role` (
`role_id` INT NOT NULL ,
`roletype` ENUM('User','Instrct','Admin') NOT NULL ,
PRIMARY KEY (`role_id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_db`.`Student`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_db`.`Student` ;
CREATE TABLE IF NOT EXISTS `test_db`.`Student` (
`student_id` INT NOT NULL AUTO_INCREMENT ,
`Parent_id` INT NOT NULL ,
`firstname` VARCHAR(60) NOT NULL ,
`lastname` VARCHAR(60) NOT NULL ,
`nickname` VARCHAR(45) NULL ,
`birthday` DATE NOT NULL ,
`gender` ENUM('male','female') NOT NULL ,
PRIMARY KEY (`student_id`) ,
UNIQUE INDEX `idStudent_UNIQUE` (`student_id` ASC) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_db`.`transaction_details`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_db`.`transaction_details` ;
CREATE TABLE IF NOT EXISTS `test_db`.`transaction_details` (
`transaction_id` INT NOT NULL ,
`depositamount` INT NOT NULL ,
`depositdate` DATE NOT NULL ,
`balance` INT NULL ,
`paymenttype` ENUM('cash','check','paypal') NULL ,
PRIMARY KEY (`transaction_id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_db`.`transactions`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_db`.`transactions` ;
CREATE TABLE IF NOT EXISTS `test_db`.`transactions` (
`transaction_id` INT NOT NULL ,
`user_id` INT NOT NULL ,
PRIMARY KEY (`transaction_id`) ,
CONSTRAINT `fk_transactions_transaction_details1`
FOREIGN KEY (`transaction_id` )
REFERENCES `test_db`.`transaction_details` (`transaction_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_db`.`member`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_db`.`member` ;
CREATE TABLE IF NOT EXISTS `test_db`.`member` (
`user_id` INT NOT NULL AUTO_INCREMENT ,
`firstname` VARCHAR(60) NOT NULL ,
`lastname` VARCHAR(60) NOT NULL ,
`address_id` INT NULL ,
`phone1` VARCHAR(45) NOT NULL ,
`phone2` VARCHAR(45) NULL ,
PRIMARY KEY (`user_id`) ,
INDEX `address_id_UNIQUE` (`address_id` ASC) ,
CONSTRAINT `fk_member_Student1`
FOREIGN KEY (`user_id` )
REFERENCES `test_db`.`Student` (`Parent_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_member_transactions1`
FOREIGN KEY (`user_id` )
REFERENCES `test_db`.`transactions` (`user_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_db`.`address`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_db`.`address` ;
CREATE TABLE IF NOT EXISTS `test_db`.`address` (
`address_id` INT NOT NULL AUTO_INCREMENT ,
`addressline1` VARCHAR(45) NOT NULL ,
`addressline2` VARCHAR(45) NULL ,
`city` VARCHAR(45) NOT NULL ,
`state` VARCHAR(45) NOT NULL ,
`zipcode` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`address_id`) ,
UNIQUE INDEX `address_id_UNIQUE` (`address_id` ASC) ,
CONSTRAINT `fk_address_member1`
FOREIGN KEY (`address_id` )
REFERENCES `test_db`.`member` (`address_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_db`.`trainer`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_db`.`trainer` ;
CREATE TABLE IF NOT EXISTS `test_db`.`trainer` (
`trainer_id` INT NOT NULL ,
`trainer_firstname` VARCHAR(45) NOT NULL ,
`trainer_lastname` VARCHAR(45) NOT NULL ,
`trainer_email` VARCHAR(45) NOT NULL ,
`trainer_phone` VARCHAR(45) NOT NULL ,
`address_address_id` INT NOT NULL ,
PRIMARY KEY (`trainer_id`) ,
INDEX `fk_trainer_address1` (`address_address_id` ASC) ,
CONSTRAINT `fk_trainer_address1`
FOREIGN KEY (`address_address_id` )
REFERENCES `test_db`.`address` (`address_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_db`.`user_master`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_db`.`user_master` ;
CREATE TABLE IF NOT EXISTS `test_db`.`user_master` (
`user_id` INT NOT NULL AUTO_INCREMENT ,
`username` VARCHAR(45) NOT NULL ,
`password` VARCHAR(45) NOT NULL ,
`role_id` INT NOT NULL ,
PRIMARY KEY (`user_id`) ,
UNIQUE INDEX `username_UNIQUE` (`username` ASC) ,
INDEX `role_id` (`role_id` ASC) ,
CONSTRAINT `role_id`
FOREIGN KEY (`role_id` )
REFERENCES `test_db`.`role` (`role_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_user_master_trainer1`
FOREIGN KEY (`user_id` )
REFERENCES `test_db`.`trainer` (`trainer_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_user_master_member1`
FOREIGN KEY (`user_id` )
REFERENCES `test_db`.`member` (`user_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_db`.`table1`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_db`.`table1` ;
CREATE TABLE IF NOT EXISTS `test_db`.`table1` (
)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_db`.`location`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_db`.`location` ;
CREATE TABLE IF NOT EXISTS `test_db`.`location` (
`location_id` INT NOT NULL ,
`locationname` VARCHAR(45) NOT NULL ,
`locationaddress1` VARCHAR(80) NOT NULL ,
`locationaddress2` VARCHAR(80) NULL ,
`locationcity` VARCHAR(45) NOT NULL ,
`locationZip` VARCHAR(5) NOT NULL ,
`locationphone` VARCHAR(12) NOT NULL ,
PRIMARY KEY (`location_id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_db`.`session_type`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_db`.`session_type` ;
CREATE TABLE IF NOT EXISTS `test_db`.`session_type` (
`style_id` INT NOT NULL ,
`sessiontype` ENUM('private','semi-private','mini-group') NOT NULL ,
`sessionlength` ENUM('20','30') NOT NULL ,
`cost` INT NOT NULL ,
`maxstudent` TINYINT NOT NULL ,
`sessionlocation_id` INT NOT NULL ,
PRIMARY KEY (`style_id`) ,
INDEX `fk_sessionType_location1` (`sessionlocation_id` ASC) ,
CONSTRAINT `fk_sessionType_location1`
FOREIGN KEY (`sessionlocation_id` )
REFERENCES `test_db`.`location` (`location_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test_db`.`session`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `test_db`.`session` ;
CREATE TABLE IF NOT EXISTS `test_db`.`session` (
`session_id` INT NOT NULL AUTO_INCREMENT ,
`session_Student_id` INT NOT NULL ,
`sessionDay` ENUM('monday','tuesday','wednesday','thursday','friday','saturday','sunday') NOT NULL ,
`sessiontime_id` INT NOT NULL ,
`sessionTrainer_id` INT NOT NULL ,
`sessionnotes` VARCHAR(250) NULL ,
`session_type_id` INT NOT NULL ,
`session_cost` INT NULL ,
`transactions_transaction_id` INT NOT NULL ,
PRIMARY KEY (`session_id`) ,
INDEX `fk_session_Student1` (`session_Student_id` ASC) ,
INDEX `fk_session_trainer1` (`sessionTrainer_id` ASC) ,
INDEX `fk_session_cost` (`session_cost` ASC) ,
INDEX `fk_session_session_type1` (`session_type_id` ASC) ,
INDEX `fk_session_transactions1` (`transactions_transaction_id` ASC) ,
CONSTRAINT `fk_session_Student1`
FOREIGN KEY (`session_Student_id` )
REFERENCES `test_db`.`Student` (`student_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_session_trainer1`
FOREIGN KEY (`sessionTrainer_id` )
REFERENCES `test_db`.`trainer` (`trainer_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_session_sessioncost`
FOREIGN KEY (`session_cost` )
REFERENCES `test_db`.`session_type` (`cost` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_session_session_type1`
FOREIGN KEY (`session_type_id` )
REFERENCES `test_db`.`session_type` (`style_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_session_transactions1`
FOREIGN KEY (`transactions_transaction_id` )
REFERENCES `test_db`.`transactions` (`transaction_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
The problem is that your foreign key constraints are referencing columns in the other table which are not indexed. Adding indexes to Student.Parent_id and transactions.user_id allowed me to create the members table. Double-check all your foreign key constraints that they are pointing to indexed columns.
Referenced columns should be unique.
Table test_db.member -> REFERENCES test_db.Student (Parent_id )
Table test_db.session-> REFERENCES test_db.session_type (cost )
CREATE TABLE IF NOT EXISTS test_db.table1 (
)
ENGINE = InnoDB;
Check your schema (primary keys, unique keys, foreign keys) if they are correct.