Related
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
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.
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.
With the following scripts, when i try to create NetBankingTransaction table, it fails with the following message:
Error Code: 1005. Can't create table 'wah_schema.netbankingtransaction' (errno: 150)
DB scripts:
CREATE TABLE IF NOT EXISTS `wah_schema`.`Transaction` (
`idTransaction` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`idOrder` INT UNSIGNED NOT NULL ,
`type` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`idTransaction`, `idOrder`, `type`) )
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `wah_schema`.`NetBankingTransaction` (
`idTransaction` INT NOT NULL ,
`bankCode` VARCHAR(45) NOT NULL ,
`type` VARCHAR(45) NOT NULL DEFAULT 'NETBANKING' ,
PRIMARY KEY (`idTransaction`, `type`) ,
INDEX `fk_NetBankingTransaction_Transaction1` (`idTransaction` ASC, `type` ASC) ,
CONSTRAINT `fk_NetBankingTransaction_Transaction1`
FOREIGN KEY (`idTransaction` , `type` )
REFERENCES `wah_schema`.`Transaction` (`idTransaction` , `type` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
Can someone please help me understand why am I getting this error ?
This one works for me:
/*drop table if exists `Transaction`;*/
CREATE TABLE IF NOT EXISTS `Transaction`
(
`idTransaction` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`idOrder` INT UNSIGNED NOT NULL ,
`type` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`idTransaction`, `idOrder`, `type`),
KEY idx_idTransaction_type (idTransaction, `type`)
) ENGINE = InnoDB;
/*drop table if exists NetBankingTransaction;*/
CREATE TABLE IF NOT EXISTS `NetBankingTransaction`
(
`idTransaction` INT UNSIGNED NOT NULL ,
`bankCode` VARCHAR(45) NOT NULL ,
`type` VARCHAR(45) NOT NULL DEFAULT 'NETBANKING' ,
PRIMARY KEY (`idTransaction`, `type`),
CONSTRAINT `fk_NetBankingTransaction_Transaction1`
FOREIGN KEY (`idTransaction` , `type` )
REFERENCES `Transaction` (`idTransaction` , `type` )
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE = InnoDB;
idTransaction in NetBankingTransaction table has to be unsigned. And you need an index on (idTransaction, type) in the Transaction table. Your primary key does not cover the FK requires since it's over three columns.
P.S: You don't need this in NetBankingTransaction table
INDEX fk_NetBankingTransaction_Transaction1 (idTransaction ASC, type ASC)
You need a UNIQUE KEY in Transaction (idTransaction, type) to properly add that Foreign Key constraint.
idTransaction is a different type in each table. One is unsigned and the other is signed. Try making idTransaction unsigned in the second table.
I'm using MySQL to design a database, I'm exporting the code for generating the database by using the 'SQL Create Script' option on the workbench. However, when I run the code, and use SHOW TABLES I get null, which probably means no table was created in the database. This is the code that was generated (its long but since it was auto generated the error is probably from a setting I had when exporting it.)
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 `travel_agency` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `travel_agency` ;
-- -----------------------------------------------------
-- Table `travel_agency`.`Region`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Region` (
`region_name` VARCHAR(45) NOT NULL ,
`languages` VARCHAR(100) NOT NULL ,
PRIMARY KEY (`region_name`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Country`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Country` (
`country_name` VARCHAR(15) NOT NULL ,
`Region_region_name` VARCHAR(45) NOT NULL ,
`currency` CHAR(20) NOT NULL ,
PRIMARY KEY (`country_name`, `Region_region_name`) ,
INDEX `fk_Country_Region1` (`Region_region_name` ASC) ,
CONSTRAINT `fk_Country_Region1`
FOREIGN KEY (`Region_region_name` )
REFERENCES `travel_agency`.`Region` (`region_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Transport`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Transport` (
`Country_country_name` VARCHAR(15) NOT NULL ,
`cost_estimate` DECIMAL(8,2) NOT NULL ,
`transport_deals` BLOB NOT NULL ,
`transport_rating` CHAR(1) NOT NULL ,
PRIMARY KEY (`Country_country_name`) ,
CONSTRAINT `fk_Transport_Country`
FOREIGN KEY (`Country_country_name` )
REFERENCES `travel_agency`.`Country` (`country_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Resort`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Resort` (
`resort_name` VARCHAR(50) NOT NULL ,
`city` VARCHAR(50) NOT NULL ,
`Country_country_name` VARCHAR(15) NOT NULL ,
`resort_type` CHAR(20) NOT NULL ,
INDEX `fk_Resort_Country1` (`Country_country_name` ASC) ,
PRIMARY KEY (`resort_name`, `city`) ,
CONSTRAINT `fk_Resort_Country1`
FOREIGN KEY (`Country_country_name` )
REFERENCES `travel_agency`.`Country` (`country_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Hotel`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Hotel` (
`hotel_name` CHAR NOT NULL ,
`rating` CHAR(1) NOT NULL ,
`address1` CHAR(50) NOT NULL ,
`address2` CHAR(50) NOT NULL ,
`postcode` CHAR(10) NOT NULL ,
`telephone` CHAR(20) NOT NULL ,
`Resort_resort_name` VARCHAR(50) NOT NULL ,
PRIMARY KEY (`hotel_name`) ,
INDEX `fk_Hotel_Resort1` (`Resort_resort_name` ASC) ,
CONSTRAINT `fk_Hotel_Resort1`
FOREIGN KEY (`Resort_resort_name` )
REFERENCES `travel_agency`.`Resort` (`resort_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Room`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Room` (
`room_num` INT NOT NULL ,
`room_type` CHAR(10) NOT NULL ,
`Hotel_hotel_name` CHAR NOT NULL ,
`minibar` BIT NOT NULL ,
`tv` BIT NOT NULL ,
`smoking_permitted` BIT NOT NULL ,
INDEX `fk_Room_Hotel1` (`Hotel_hotel_name` ASC) ,
PRIMARY KEY (`room_num`, `room_type`) ,
CONSTRAINT `fk_Room_Hotel1`
FOREIGN KEY (`Hotel_hotel_name` )
REFERENCES `travel_agency`.`Hotel` (`hotel_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Guest`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Guest` (
`guest_id` INT NOT NULL ,
`firstname` VARCHAR(20) NOT NULL ,
`surname` VARCHAR(20) NOT NULL ,
`mobilephone` CHAR(20) NOT NULL ,
PRIMARY KEY (`guest_id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Bookings`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Bookings` (
`Guest_guest_id` INT NOT NULL ,
`Hotel_hotel_name` CHAR NOT NULL ,
`guest_count` INT NOT NULL ,
PRIMARY KEY (`Guest_guest_id`, `Hotel_hotel_name`) ,
INDEX `fk_Booking_Guest1` (`Guest_guest_id` ASC) ,
INDEX `fk_Booking_Hotel1` (`Hotel_hotel_name` ASC) ,
CONSTRAINT `fk_Booking_Guest1`
FOREIGN KEY (`Guest_guest_id` )
REFERENCES `travel_agency`.`Guest` (`guest_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Booking_Hotel1`
FOREIGN KEY (`Hotel_hotel_name` )
REFERENCES `travel_agency`.`Hotel` (`hotel_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`HotelFacilities`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`HotelFacilities` (
`Hotel_hotel_name` CHAR NOT NULL ,
`internet_access` BIT NOT NULL ,
`hotel_restaurant` BIT NOT NULL ,
`games_room` BIT NOT NULL ,
`bar` BIT NOT NULL ,
`evening_shows` BIT NOT NULL ,
`massage_parlour` BIT NOT NULL ,
`misc_details` BLOB NOT NULL ,
PRIMARY KEY (`Hotel_hotel_name`) ,
CONSTRAINT `fk_HotelFacilities_Hotel1`
FOREIGN KEY (`Hotel_hotel_name` )
REFERENCES `travel_agency`.`Hotel` (`hotel_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Attractions`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Attractions` (
`Region_region_name` VARCHAR(45) NOT NULL ,
`dirt_mountains` BIT NOT NULL ,
`beaches` BIT NOT NULL ,
`casinos` BIT NOT NULL ,
`safari` BIT NOT NULL ,
`snow_mountains` BIT NOT NULL ,
`misc_details` BLOB NOT NULL ,
PRIMARY KEY (`Region_region_name`) ,
CONSTRAINT `fk_Attractions_Region1`
FOREIGN KEY (`Region_region_name` )
REFERENCES `travel_agency`.`Region` (`region_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `travel_agency`.`Rate`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `travel_agency`.`Rate` (
`Hotel_hotel_name` CHAR NOT NULL ,
`Room_room_num` INT NOT NULL ,
`Room_room_type` CHAR(10) NOT NULL ,
`first_quarter` DECIMAL(20,2) NOT NULL ,
`second_quarter` DECIMAL(20,2) NOT NULL ,
`third_quarter` DECIMAL(20,2) NOT NULL ,
`fourth_quarter` DECIMAL(20,2) NOT NULL ,
`discount_information` BLOB NOT NULL ,
PRIMARY KEY (`Hotel_hotel_name`, `Room_room_num`, `Room_room_type`) ,
INDEX `fk_Rate_Room1` (`Room_room_num` ASC, `Room_room_type` ASC) ,
CONSTRAINT `fk_Rate_Hotel1`
FOREIGN KEY (`Hotel_hotel_name` )
REFERENCES `travel_agency`.`Hotel` (`hotel_name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Rate_Room1`
FOREIGN KEY (`Room_room_num` , `Room_room_type` )
REFERENCES `travel_agency`.`Room` (`room_num` , `room_type` )
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;
I'm using sql command line client and putting the code but it doesn't work.
Its because you're using command line client as your database. That just makes your pc function as a database but doesn't exactly have all features a professional database would have. And MySQL workbench is designed to export code for databases more professional.
Since you're using command line client I'm assuming this is either practice or homework of some sort? Either way, this is the way the code would work in Command line client. Try this.
CREATE TABLE Region
(
region_name VARCHAR(45) NOT NULL ,
languages VARCHAR(100) NOT NULL ,
PRIMARY KEY (region_name)
);
CREATE TABLE Country
(
country_name VARCHAR(15) NOT NULL ,
Region_region_name VARCHAR(45) NOT NULL ,
currency CHAR(20) NOT NULL ,
PRIMARY KEY (country_name, Region_region_name) ,
CONSTRAINT fk_Country_Region1
FOREIGN KEY (Region_region_name )
REFERENCES Region (region_name )
);
CREATE TABLE Transport (
Country_country_name VARCHAR(15) NOT NULL ,
cost_estimate DECIMAL(8,2) NOT NULL ,
transport_deals CHAR(100) NULL ,
transport_rating CHAR(20) NOT NULL ,
PRIMARY KEY (Country_country_name) ,
CONSTRAINT fk_Transport_Country
FOREIGN KEY (Country_country_name)
REFERENCES Country (country_name )
);
CREATE TABLE Resort (
resort_name VARCHAR(50) NOT NULL ,
Country_country_name VARCHAR(15) NOT NULL ,
resort_type CHAR(20) NOT NULL ,
PRIMARY KEY (resort_name) ,
CONSTRAINT fk_Resort_Country1
FOREIGN KEY (Country_country_name)
REFERENCES Country (country_name)
);
CREATE TABLE Hotel (
hotel_name CHAR(20) NOT NULL ,
rating CHAR NOT NULL ,
address1 CHAR(50) NOT NULL ,
address2 CHAR(50) NOT NULL ,
postcode CHAR(10) NOT NULL ,
telephone CHAR(20) NOT NULL ,
Resort_resort_name VARCHAR(50) NOT NULL ,
PRIMARY KEY (hotel_name) ,
CONSTRAINT fk_Hotel_Resort1
FOREIGN KEY (Resort_resort_name)
REFERENCES Resort (resort_name)
);
CREATE TABLE Room (
room_num INT NOT NULL ,
room_type CHAR(10) NOT NULL ,
Hotel_hotel_name CHAR(20) NOT NULL ,
minibar BOOLEAN NOT NULL ,
tv BOOLEAN NOT NULL ,
smoking_permitted BOOLEAN NOT NULL ,
PRIMARY KEY (room_num) ,
CONSTRAINT fk_Room_Hotel1
FOREIGN KEY (Hotel_hotel_name)
REFERENCES Hotel (hotel_name)
);
CREATE TABLE Guest (
guest_id INT NOT NULL ,
firstname VARCHAR(20) NOT NULL ,
surname VARCHAR(20) NOT NULL ,
mobilephone CHAR(20) NOT NULL ,
PRIMARY KEY (guest_id)
);
CREATE TABLE Bookings (
Guest_guest_id INT NOT NULL ,
Hotel_hotel_name CHAR(20) NOT NULL ,
guest_count INT NOT NULL ,
PRIMARY KEY (Guest_guest_id, Hotel_hotel_name) ,
CONSTRAINT fk_Booking_Guest1
FOREIGN KEY (Guest_guest_id)
REFERENCES Guest (guest_id),
CONSTRAINT fk_Booking_Hotel1
FOREIGN KEY (Hotel_hotel_name)
REFERENCES Hotel (hotel_name)
);
CREATE TABLE HotelFacilities (
Hotel_hotel_name CHAR(20) NOT NULL ,
internet_access BOOLEAN NOT NULL ,
hotel_restaurant BOOLEAN NOT NULL ,
games_room BOOLEAN NOT NULL ,
bar BOOLEAN NOT NULL ,
evening_shows BOOLEAN NOT NULL ,
massage_parlour BOOLEAN NOT NULL ,
misc_details CHAR(100) NULL ,
PRIMARY KEY (Hotel_hotel_name) ,
CONSTRAINT fk_HotelFacilities_Hotel1
FOREIGN KEY (Hotel_hotel_name)
REFERENCES Hotel (hotel_name)
);
CREATE TABLE Attractions (
Region_region_name VARCHAR(45) NOT NULL ,
dirt_mountains BOOLEAN NOT NULL ,
beaches BOOLEAN NOT NULL ,
casinos BOOLEAN NOT NULL ,
safari BOOLEAN NOT NULL ,
snow_mountains BOOLEAN NOT NULL ,
misc_details CHAR(100) NULL ,
PRIMARY KEY (Region_region_name) ,
CONSTRAINT fk_Attractions_Region1
FOREIGN KEY (Region_region_name )
REFERENCES Region (region_name )
);
CREATE TABLE Rate (
Hotel_hotel_name CHAR(20) NOT NULL ,
Room_room_num INT NOT NULL ,
first_quarter DECIMAL(10,2) NOT NULL ,
second_quarter DECIMAL(10,2) NOT NULL ,
third_quarter DECIMAL(10,2) NOT NULL ,
fourth_quarter DECIMAL(10,2) NOT NULL ,
discount_information CHAR(100) NULL ,
PRIMARY KEY (Hotel_hotel_name, Room_room_num) ,
CONSTRAINT fk_Rate_Hotel1
FOREIGN KEY (Hotel_hotel_name )
REFERENCES Hotel (hotel_name ),
CONSTRAINT fk_Rate_Room1
FOREIGN KEY (Room_room_num)
REFERENCES Room (room_num)
);