MySQL code schema generating code that does nothing? - mysql

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)
);

Related

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

why do I get the “Error Code 1005 Cant create Table” in MYSQL with no typos in FK

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.

Mysql Forward engineer errno 150

I'm modeling my db shema using MySQL Workbench CE EER modeler and now I'm stuck with mysql errno 150.
My sql code:
CREATE TABLE `myschema`.`Clients` (
`phone` VARCHAR(15) NOT NULL ,
`surname` VARCHAR(30) NOT NULL ,
`name` VARCHAR(30) NOT NULL ,
`middleName` VARCHAR(30) NULL ,
`discountCardNumber` BIGINT NULL ,
PRIMARY KEY (`phone`) ,
UNIQUE INDEX `discountCardNumber_UNIQUE` (`discountCardNumber` ASC) ,
CONSTRAINT `fk_Clients_DiscountCards1`
FOREIGN KEY (`discountCardNumber` )
REFERENCES `myschema`.`DiscountCards` (`cardNumber` )
ON DELETE SET NULL
ON UPDATE CASCADE)
ENGINE = InnoDB;
CREATE TABLE `myschema`.`OrderStatuses` (
`statusID` INT NOT NULL AUTO_INCREMENT ,
`statusTitle` VARCHAR(45) NOT NULL ,
`statusDescription` VARCHAR(150) NULL ,
PRIMARY KEY (`statusID`) )
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `myschema`.`WorkstationUsers` (
`userID` INT NOT NULL AUTO_INCREMENT ,
`login` VARCHAR(45) NOT NULL ,
`pass` VARCHAR(45) NOT NULL ,
`name` VARCHAR(45) NOT NULL ,
`surname` VARCHAR(45) NOT NULL ,
`middleName` VARCHAR(45) NULL ,
PRIMARY KEY (`userID`) )
ENGINE = InnoDB;
CREATE TABLE `myschema`.`Orders` (
`orderID` BIGINT NOT NULL AUTO_INCREMENT ,
`registerDate` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`clientPhone` VARCHAR(15) NOT NULL ,
`shipmentAddress` VARCHAR(150) NOT NULL ,
`orderStatus` INT NOT NULL ,
`registrator` INT NOT NULL ,
PRIMARY KEY (`orderID`) ,
INDEX `fk_Orders_Clients` (`clientPhone` ASC) ,
INDEX `fk_Orders_OrderStatuses1` (`orderStatus` ASC) ,
INDEX `fk_Orders_WorkstationUsers1` (`registrator` ASC) ,
CONSTRAINT `fk_Orders_Clients`
FOREIGN KEY (`clientPhone` )
REFERENCES `myschema`.`Clients` (`phone` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `fk_Orders_OrderStatuses1`
FOREIGN KEY (`orderStatus` )
REFERENCES `myschema`.`OrderStatuses` (`statusID` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `fk_Orders_WorkstationUsers1`
FOREIGN KEY (`registrator` )
REFERENCES `myschema`.`WorkstationUsers` (`userID` )
ON DELETE SET NULL
ON UPDATE CASCADE)
ENGINE = InnoDB;
It fails in last statement (CREATE TABLE Orders).
I already checked:
Types of refferencing tables are equals
Indexes on all columns exists
Engine is InnoDB
Thanks for any help! Have a good day!
P.S. sorry for possible duplicate. I really can't find any problem in my code.
Some of fields are defined as NOT NULL, but you defined 'ON DELETE' action as 'SET NULL'.
Make these fields nullabe -
CREATE TABLE `myschema`.`Orders` (
`orderID` BIGINT NOT NULL AUTO_INCREMENT ,
`registerDate` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
`clientPhone` VARCHAR(15) NULL, -- NOT NULL ,
`shipmentAddress` VARCHAR(150) NOT NULL ,
`orderStatus` INT NULL, -- NOT NULL ,
`registrator` INT NULL, -- NOT NULL ,
PRIMARY KEY (`orderID`) ,
INDEX `fk_Orders_Clients` (`clientPhone` ASC) ,
INDEX `fk_Orders_OrderStatuses1` (`orderStatus` ASC) ,
INDEX `fk_Orders_WorkstationUsers1` (`registrator` ASC) ,
CONSTRAINT `fk_Orders_Clients`
FOREIGN KEY (`clientPhone` )
REFERENCES `myschema`.`Clients` (`phone` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `fk_Orders_OrderStatuses1`
FOREIGN KEY (`orderStatus` )
REFERENCES `myschema`.`OrderStatuses` (`statusID` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `fk_Orders_WorkstationUsers1`
FOREIGN KEY (`registrator` )
REFERENCES `myschema`.`WorkstationUsers` (`userID` )
ON DELETE SET NULL
ON UPDATE CASCADE)
ENGINE = InnoDB;
In my case, the problem was solved by adding
DEFAULT CHARACTER SET = utf8
at the end of each create table
regards!

Compatibility issue between the MySQL workbench and MySQL GUI tools

I've used MySQL workbench 5.0.30 OSS for creating a visual design of my database with the entities and relationships between tables.I exported the SQL create script from it.
I'm using the SQL GUI tools 5.0 r17 on windows 7 (32-bit) machine to restore the database and I see a couple of tables missing.It throws a 150 error table cant be created.
I don't understand if its the compatibility issue or is it something wrong with the err diagram.
It would be great if someone can help.The following is the SQL script.
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 `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `mydb`;
-- -----------------------------------------------------
-- Table `mydb`.`Question_type`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Question_type` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Question_type` (
`idQuestion_type` VARCHAR(255) NULL ,
`Question_type_description` VARCHAR(255) NULL ,
`Part_Marks` BOOLEAN NULL ,
`Minimum_Marks` INT NULL ,
`Maximum_Marks` INT NULL ,
`Close_ended` BOOLEAN NULL ,
PRIMARY KEY (`idQuestion_type`) )
ENGINE = InnoDB
COMMENT = 'The type of the question';
-- -----------------------------------------------------
-- Table `mydb`.`Question`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Question` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Question` (
`idQuestion` VARCHAR(255) NOT NULL ,
`idQuestion_type` VARCHAR(255) NULL ,
PRIMARY KEY (`idQuestion`) ,
CONSTRAINT `fk_Question_Question_type`
FOREIGN KEY (`idQuestion_type` )
REFERENCES `mydb`.`Question_type` (`idQuestion_type` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
COMMENT = 'The main questionID table that links to everyother';
CREATE INDEX `fk_Question_Question_type` ON `mydb`.`Question` (`idQuestion_type` ASC) ;
-- -----------------------------------------------------
-- Table `mydb`.`Answer_form`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Answer_form` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Answer_form` (
`idAnswer_form` VARCHAR(255) NOT NULL ,
`Form_description` MEDIUMTEXT NULL ,
`Form_shortname` VARCHAR(45) NULL ,
PRIMARY KEY (`idAnswer_form`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Question_form`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Question_form` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Question_form` (
`idQuestion_form` VARCHAR(255) NOT NULL ,
`Form_description` MEDIUMTEXT NULL ,
`Form_shortname` VARCHAR(45) NULL ,
PRIMARY KEY (`idQuestion_form`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`QShort_Answer`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`QShort_Answer` ;
CREATE TABLE IF NOT EXISTS `mydb`.`QShort_Answer` (
`idQShort_Answer` VARCHAR(255) NOT NULL ,
`idQuestion` VARCHAR(255) NULL ,
`idQuestion_form` VARCHAR(255) NULL ,
`Question_content` LONGTEXT NULL ,
`Upload_content` LONGBLOB NULL ,
`One_word_answer` BOOLEAN NULL ,
`One_answer1` VARCHAR(45) NULL ,
`One_answer2` VARCHAR(45) NULL ,
`idAnswer_form` VARCHAR(255) NULL ,
`Answer_key1` VARCHAR(255) NULL ,
`Answer_key2` VARCHAR(255) NULL ,
`Answer_key3` VARCHAR(255) NULL ,
`Answer_key4` VARCHAR(255) NULL ,
`Answer_key5` VARCHAR(255) NULL ,
`Answer_key6` VARCHAR(255) NULL ,
`No_of_words` INT NULL ,
PRIMARY KEY (`idQShort_Answer`) ,
CONSTRAINT `fk_QShort_Answer_Answer_form`
FOREIGN KEY (`idAnswer_form` )
REFERENCES `mydb`.`Answer_form` (`idAnswer_form` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_QShort_Answer_Question_form`
FOREIGN KEY (`idQuestion_form` )
REFERENCES `mydb`.`Question_form` (`idQuestion_form` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_QShort_Answer_Question`
FOREIGN KEY (`idQuestion` )
REFERENCES `mydb`.`Question` (`idQuestion` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
COMMENT = 'The short answer question table';
CREATE INDEX `fk_QShort_Answer_Answer_form` ON `mydb`.`QShort_Answer` (`idAnswer_form` ASC) ;
CREATE INDEX `fk_QShort_Answer_Question_form` ON `mydb`.`QShort_Answer` (`idQuestion_form` ASC) ;
CREATE INDEX `fk_QShort_Answer_Question` ON `mydb`.`QShort_Answer` (`idQuestion` ASC) ;
-- -----------------------------------------------------
-- Table `mydb`.`QLong_Answer`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`QLong_Answer` ;
CREATE TABLE IF NOT EXISTS `mydb`.`QLong_Answer` (
`idQLong_Answer` VARCHAR(255) NOT NULL ,
`idQuestion` VARCHAR(255) NULL ,
`idQuestion_form` VARCHAR(255) NULL ,
`Question_content` LONGTEXT NULL ,
`Upload_content` LONGBLOB NULL ,
`idAnswer_form` VARCHAR(255) NULL ,
`Answer_key1` VARCHAR(255) NULL ,
`Answer_key2` VARCHAR(255) NULL ,
`Answer_key3` VARCHAR(255) NULL ,
`Answer_key4` VARCHAR(255) NULL ,
`Answer_key5` VARCHAR(255) NULL ,
`Answer_key6` VARCHAR(255) NULL ,
`No_of_words` INT NULL ,
`Rubric_used` BOOLEAN NULL ,
PRIMARY KEY (`idQLong_Answer`) ,
CONSTRAINT `fk_QLong_Answer_Question_form`
FOREIGN KEY (`idQuestion_form` )
REFERENCES `mydb`.`Question_form` (`idQuestion_form` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_QLong_Answer_Answer_form`
FOREIGN KEY (`idAnswer_form` )
REFERENCES `mydb`.`Answer_form` (`idAnswer_form` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_QLong_Answer_Question`
FOREIGN KEY (`idQuestion` )
REFERENCES `mydb`.`Question` (`idQuestion` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE INDEX `fk_QLong_Answer_Question_form` ON `mydb`.`QLong_Answer` (`idQuestion_form` ASC) ;
CREATE INDEX `fk_QLong_Answer_Answer_form` ON `mydb`.`QLong_Answer` (`idAnswer_form` ASC) ;
CREATE INDEX `fk_QLong_Answer_Question` ON `mydb`.`QLong_Answer` (`idQuestion` ASC) ;
-- -----------------------------------------------------
-- Table `mydb`.`Multichoice_question`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Multichoice_question` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Multichoice_question` (
`idMultichoice_question` VARCHAR(255) NOT NULL ,
`idQuestion` VARCHAR(255) NULL ,
`idQuestion_form` VARCHAR(255) NULL ,
`Question_content` LONGTEXT NULL ,
`Upload_content` LONGBLOB NULL ,
`Option1` MEDIUMTEXT NULL ,
`Option2` MEDIUMTEXT NULL ,
`Option3` MEDIUMTEXT NULL ,
`Option4` MEDIUMTEXT NULL ,
`Option5` MEDIUMTEXT NULL ,
`Option6` MEDIUMTEXT NULL ,
PRIMARY KEY (`idMultichoice_question`) ,
CONSTRAINT `fk_Multichoice_question_Question_form`
FOREIGN KEY (`idQuestion_form` )
REFERENCES `mydb`.`Question_form` (`idQuestion_form` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Multichoice_question_Question`
FOREIGN KEY (`idQuestion` )
REFERENCES `mydb`.`Question` (`idQuestion` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
COMMENT = 'Multiple choice Questions';
CREATE INDEX `fk_Multichoice_question_Question_form` ON `mydb`.`Multichoice_question` (`idQuestion_form` ASC) ;
CREATE INDEX `fk_Multichoice_question_Question` ON `mydb`.`Multichoice_question` (`idQuestion` ASC) ;
-- -----------------------------------------------------
-- Table `mydb`.`QCalculated`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`QCalculated` ;
CREATE TABLE IF NOT EXISTS `mydb`.`QCalculated` (
`idQCalculated` VARCHAR(255) NOT NULL ,
`idQuestion` VARCHAR(255) NULL ,
`idQuestion_form` VARCHAR(255) NULL ,
`Question_content` LONGTEXT NULL ,
`Upload_content` LONGBLOB NULL ,
`Number_of_wildcards` INT NULL ,
`Wildcard1` VARCHAR(45) NULL ,
`Wvalue1` INT NULL ,
`Wmax1` INT NULL ,
`Wmin1` INT NULL ,
`Wdecimal1` BOOLEAN NULL ,
`Wildcard2` VARCHAR(45) NULL ,
`Wvalue2` INT NULL ,
`Wmax2` INT NULL ,
`Wmin2` INT NULL ,
`Wdecimal2` BOOLEAN NULL ,
`Wildcard3` VARCHAR(45) NULL ,
`Wvalue3` INT NULL ,
`Wmax3` INT NULL ,
`Wmin3` INT NULL ,
`Wdecimal3` BOOLEAN NULL ,
`Wildcard4` VARCHAR(45) NULL ,
`Wvalue4` INT NULL ,
`Wmax4` INT NULL ,
`Wmin4` INT NULL ,
`Wdecimal4` BOOLEAN NULL ,
`Wildcard5` VARCHAR(45) NULL ,
`Wvalue5` INT NULL ,
`Wmax5` INT NULL ,
`Wmin5` INT NULL ,
`Wdecimal5` BOOLEAN NULL ,
`Wildcard6` VARCHAR(45) NULL ,
`Wvalue6` INT NULL ,
`Wmax6` INT NULL ,
`Wmin6` INT NULL ,
`Wdecimal6` BOOLEAN NULL ,
`Wildcard7` VARCHAR(45) NULL ,
`Wvalue7` INT NULL ,
`Wmax7` INT NULL ,
`Wmin7` INT NULL ,
`Wdecimal7` BOOLEAN NULL ,
`Formula` VARCHAR(255) NULL ,
`Formula_unit1` VARCHAR(45) NULL ,
`Formula_unit2` VARCHAR(45) NULL ,
`Formula_unit3` VARCHAR(45) NULL ,
`Formula_unit4` VARCHAR(45) NULL ,
`Formula_unit5` VARCHAR(45) NULL ,
`Tolerance` INT NULL ,
PRIMARY KEY (`idQCalculated`) ,
CONSTRAINT `fk_QCalculated_Question_form`
FOREIGN KEY (`idQuestion_form` )
REFERENCES `mydb`.`Question_form` (`idQuestion_form` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_QCalculated_Question`
FOREIGN KEY (`idQuestion` )
REFERENCES `mydb`.`Question` (`idQuestion` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
COMMENT = 'Generalized calculation type question';
CREATE INDEX `fk_QCalculated_Question_form` ON `mydb`.`QCalculated` (`idQuestion_form` ASC) ;
CREATE INDEX `fk_QCalculated_Question` ON `mydb`.`QCalculated` (`idQuestion` ASC) ;
-- -----------------------------------------------------
-- Table `mydb`.`Board`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Board` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Board` (
`idBoard` VARCHAR(255) NOT NULL ,
`Board_description` MEDIUMTEXT NULL ,
`Board_shortname` VARCHAR(45) NULL ,
`Class` VARCHAR(45) NULL ,
`Year` INT NULL ,
PRIMARY KEY (`idBoard`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Class_subject_term`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Class_subject_term` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Class_subject_term` (
`idCST` VARCHAR(255) NOT NULL ,
`idBoard` VARCHAR(255) NULL ,
`Class_shortname` VARCHAR(45) NULL ,
`Class_description` MEDIUMTEXT NULL ,
`Subject_description` MEDIUMTEXT NULL ,
`Subject_shortname` MEDIUMTEXT NULL ,
`Subject_group` VARCHAR(255) NULL ,
PRIMARY KEY (`idCST`) ,
CONSTRAINT `fk_Class_subject_term_Board`
FOREIGN KEY (`idBoard` )
REFERENCES `mydb`.`Board` (`idBoard` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE INDEX `fk_Class_subject_term_Board` ON `mydb`.`Class_subject_term` (`idBoard` ASC) ;
-- -----------------------------------------------------
-- Table `mydb`.`Educational_objectives`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Educational_objectives` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Educational_objectives` (
`idEO` VARCHAR(255) NOT NULL ,
`EO_shortname` VARCHAR(45) NULL ,
`EO_description` MEDIUMTEXT NULL ,
PRIMARY KEY (`idEO`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Chapter_topic`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Chapter_topic` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Chapter_topic` (
`idChaptop` VARCHAR(255) NOT NULL ,
`idCST` VARCHAR(255) NULL ,
`Chapter_shortname` VARCHAR(45) NULL ,
`Chapter_description` MEDIUMTEXT NULL ,
`Chapter_group` VARCHAR(255) NULL ,
`idTopic` VARCHAR(255) NULL ,
`Topic_description` MEDIUMTEXT NULL ,
`Term_description` MEDIUMTEXT NULL ,
`idEO` VARCHAR(255) NULL ,
PRIMARY KEY (`idChaptop`) ,
CONSTRAINT `fk_Chapter_topic_Educational_objectives`
FOREIGN KEY (`idEO` )
REFERENCES `mydb`.`Educational_objectives` (`idEO` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Chapter_topic_Class_subject_term`
FOREIGN KEY (`idCST` )
REFERENCES `mydb`.`Class_subject_term` (`idCST` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE INDEX `fk_Chapter_topic_Educational_objectives` ON `mydb`.`Chapter_topic` (`idEO` ASC) ;
CREATE INDEX `fk_Chapter_topic_Class_subject_term` ON `mydb`.`Chapter_topic` (`idCST` ASC) ;
-- -----------------------------------------------------
-- Table `mydb`.`Topic_concept`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Topic_concept` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Topic_concept` (
`idTopcon` VARCHAR(255) NOT NULL ,
`idTopic` VARCHAR(255) NULL ,
`Concept_shortname` VARCHAR(45) NULL ,
`Concept_description` MEDIUMTEXT NULL ,
PRIMARY KEY (`idTopcon`) ,
CONSTRAINT `fk_Topic_concept_Chapter_topic`
FOREIGN KEY (`idTopic` )
REFERENCES `mydb`.`Chapter_topic` (`idTopic` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE INDEX `fk_Topic_concept_Chapter_topic` ON `mydb`.`Topic_concept` (`idTopic` ASC) ;
-- -----------------------------------------------------
-- Table `mydb`.`Question_properties`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Question_properties` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Question_properties` (
`idQuestion_properties` VARCHAR(255) NOT NULL ,
`idQuestion` VARCHAR(255) NULL ,
`idBoard` VARCHAR(255) NULL ,
`Board_shortname` VARCHAR(45) NULL ,
`idCST` VARCHAR(255) NULL ,
`Class_shortname` VARCHAR(45) NULL ,
`idChapTop` VARCHAR(255) NULL ,
`Chapter_shortname` VARCHAR(45) NULL ,
`idTopcon` VARCHAR(255) NULL ,
`Marks` INT NULL ,
`Negative Marks` BOOLEAN NULL ,
`Repeat` BIGINT NULL ,
`From_textbook` BOOLEAN NULL ,
`FA_only` BOOLEAN NULL ,
PRIMARY KEY (`idQuestion_properties`) ,
CONSTRAINT `fk_Question_properties_Board`
FOREIGN KEY (`idBoard` )
REFERENCES `mydb`.`Board` (`idBoard` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Question_properties_Class_subject_term`
FOREIGN KEY (`idCST` )
REFERENCES `mydb`.`Class_subject_term` (`idCST` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Question_properties_Chapter_topic`
FOREIGN KEY (`idChapTop` )
REFERENCES `mydb`.`Chapter_topic` (`idChaptop` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Question_properties_Topic_concept`
FOREIGN KEY (`idTopcon` )
REFERENCES `mydb`.`Topic_concept` (`idTopcon` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Question_properties_Question`
FOREIGN KEY (`idQuestion` )
REFERENCES `mydb`.`Question` (`idQuestion` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
COMMENT = 'Properties of the Question';
CREATE INDEX `fk_Question_properties_Board` ON `mydb`.`Question_properties` (`idBoard` ASC) ;
CREATE INDEX `fk_Question_properties_Class_subject_term` ON `mydb`.`Question_properties` (`idCST` ASC) ;
CREATE INDEX `fk_Question_properties_Chapter_topic` ON `mydb`.`Question_properties` (`idChapTop` ASC) ;
CREATE INDEX `fk_Question_properties_Topic_concept` ON `mydb`.`Question_properties` (`idTopcon` ASC) ;
CREATE INDEX `fk_Question_properties_Question` ON `mydb`.`Question_properties` (`idQuestion` ASC) ;
-- -----------------------------------------------------
-- Table `mydb`.`Rubric`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Rubric` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Rubric` (
`idRubdim` VARCHAR(255) NOT NULL ,
`Rubric_shortname` VARCHAR(45) NULL ,
`Rubric_dimension` VARCHAR(45) NULL ,
`Rubric_dimension_description` MEDIUMTEXT NULL ,
`Scale1` VARCHAR(45) NULL ,
`Scale1_description` MEDIUMTEXT NULL ,
`Scale1_detail_description` LONGTEXT NULL ,
`Scale2` VARCHAR(45) NULL ,
`Scale2_description` MEDIUMTEXT NULL ,
`Scale2_detail_description` LONGTEXT NULL ,
`Scale3` VARCHAR(45) NULL ,
`Scale3_description` MEDIUMTEXT NULL ,
`Scale3_detail_description` LONGTEXT NULL ,
`Scale4` VARCHAR(45) NULL ,
`Scale4_description` MEDIUMTEXT NULL ,
`Scale4_detail_description` LONGTEXT NULL ,
`Rubric_category` VARCHAR(45) NULL ,
`Group_rubric` BOOLEAN NULL ,
PRIMARY KEY (`idRubdim`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Question_Rubric`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Question_Rubric` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Question_Rubric` (
`idRubric` VARCHAR(255) NOT NULL ,
`idQRubdim1` VARCHAR(255) NULL ,
`QRWeightage1` INT NULL ,
`idQRubdim2` VARCHAR(255) NULL ,
`QRWeightage2` INT NULL ,
`idQRubdim3` VARCHAR(255) NULL ,
`QRWeightage3` INT NULL ,
`idQRubdim4` VARCHAR(255) NULL ,
`QRWeightage4` INT NULL ,
`idQRubdim5` VARCHAR(255) NULL ,
`QRWeightage5` INT NULL ,
`idQRubdim6` VARCHAR(255) NULL ,
`QRWeightage6` INT NULL ,
`idQRubdim7` VARCHAR(255) NULL ,
`QRWeightage7` INT NULL ,
`idQRubdim8` VARCHAR(255) NULL ,
`QRWeightage8` INT NULL ,
PRIMARY KEY (`idRubric`) ,
CONSTRAINT `fk_Question_Rubric_Rubric`
FOREIGN KEY (`idQRubdim1` )
REFERENCES `mydb`.`Rubric` (`idRubdim` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Question_Rubric_Rubric1`
FOREIGN KEY (`idQRubdim2` )
REFERENCES `mydb`.`Rubric` (`idRubdim` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Question_Rubric_Rubric2`
FOREIGN KEY (`idQRubdim3` )
REFERENCES `mydb`.`Rubric` (`idRubdim` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Question_Rubric_Rubric3`
FOREIGN KEY (`idQRubdim4` )
REFERENCES `mydb`.`Rubric` (`idRubdim` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Question_Rubric_Rubric4`
FOREIGN KEY (`idQRubdim5` )
REFERENCES `mydb`.`Rubric` (`idRubdim` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Question_Rubric_Rubric5`
FOREIGN KEY (`idQRubdim6` )
REFERENCES `mydb`.`Rubric` (`idRubdim` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Question_Rubric_Rubric6`
FOREIGN KEY (`idQRubdim7` )
REFERENCES `mydb`.`Rubric` (`idRubdim` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Question_Rubric_Rubric7`
FOREIGN KEY (`idQRubdim8` )
REFERENCES `mydb`.`Rubric` (`idRubdim` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE INDEX `fk_Question_Rubric_Rubric` ON `mydb`.`Question_Rubric` (`idQRubdim1` ASC) ;
CREATE INDEX `fk_Question_Rubric_Rubric1` ON `mydb`.`Question_Rubric` (`idQRubdim2` ASC) ;
CREATE INDEX `fk_Question_Rubric_Rubric2` ON `mydb`.`Question_Rubric` (`idQRubdim3` ASC) ;
CREATE INDEX `fk_Question_Rubric_Rubric3` ON `mydb`.`Question_Rubric` (`idQRubdim4` ASC) ;
CREATE INDEX `fk_Question_Rubric_Rubric4` ON `mydb`.`Question_Rubric` (`idQRubdim5` ASC) ;
CREATE INDEX `fk_Question_Rubric_Rubric5` ON `mydb`.`Question_Rubric` (`idQRubdim6` ASC) ;
CREATE INDEX `fk_Question_Rubric_Rubric6` ON `mydb`.`Question_Rubric` (`idQRubdim7` ASC) ;
CREATE INDEX `fk_Question_Rubric_Rubric7` ON `mydb`.`Question_Rubric` (`idQRubdim8` ASC) ;
-- -----------------------------------------------------
-- Table `mydb`.`Learning_objectives`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Learning_objectives` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Learning_objectives` (
`idLO` VARCHAR(255) NOT NULL ,
`LO_shortname` VARCHAR(45) NULL ,
`LO_description` MEDIUMTEXT NULL ,
`idCS` VARCHAR(255) NULL ,
`CS_shortname` VARCHAR(45) NULL ,
`CS_description` MEDIUMTEXT NULL ,
PRIMARY KEY (`idLO`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`QDifficulty`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`QDifficulty` ;
CREATE TABLE IF NOT EXISTS `mydb`.`QDifficulty` (
`idQDifficulty` VARCHAR(255) NOT NULL ,
`Difficulty_description` MEDIUMTEXT NULL ,
PRIMARY KEY (`idQDifficulty`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`QNR_Tags`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`QNR_Tags` ;
CREATE TABLE IF NOT EXISTS `mydb`.`QNR_Tags` (
`idQNR_Tags` VARCHAR(255) NOT NULL ,
`idQuestion` VARCHAR(255) NULL ,
`idRubric1` VARCHAR(255) NULL ,
`idRubric2` VARCHAR(255) NULL ,
`idQCS1` VARCHAR(255) NULL ,
`idQLO1` VARCHAR(255) NULL ,
`idQCS2` VARCHAR(255) NULL ,
`idQLO2` VARCHAR(255) NULL ,
`idQDifficulty` VARCHAR(255) NULL ,
`idQuestion_form` VARCHAR(255) NULL ,
`idComment_set1` VARCHAR(255) NULL ,
`idComment_set2` VARCHAR(255) NULL ,
PRIMARY KEY (`idQNR_Tags`) ,
CONSTRAINT `fk_QNR_Tags_Question_Rubric`
FOREIGN KEY (`idRubric1` )
REFERENCES `mydb`.`Question_Rubric` (`idRubric` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_QNR_Tags_Question_Rubric1`
FOREIGN KEY (`idRubric2` )
REFERENCES `mydb`.`Question_Rubric` (`idRubric` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_QNR_Tags_Learning_objectives`
FOREIGN KEY (`idQLO1` )
REFERENCES `mydb`.`Learning_objectives` (`idLO` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_QNR_Tags_Learning_objectives2`
FOREIGN KEY (`idQLO2` )
REFERENCES `mydb`.`Learning_objectives` (`idLO` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_QNR_Tags_Learning_objectives1`
FOREIGN KEY (`idQCS1` )
REFERENCES `mydb`.`Learning_objectives` (`idCS` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_QNR_Tags_Learning_objectives3`
FOREIGN KEY (`idQCS2` )
REFERENCES `mydb`.`Learning_objectives` (`idCS` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_QNR_Tags_QDifficulty`
FOREIGN KEY (`idQDifficulty` )
REFERENCES `mydb`.`QDifficulty` (`idQDifficulty` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_QNR_Tags_Question`
FOREIGN KEY (`idQuestion` )
REFERENCES `mydb`.`Question` (`idQuestion` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE INDEX `fk_QNR_Tags_Question_Rubric` ON `mydb`.`QNR_Tags` (`idRubric1` ASC) ;
CREATE INDEX `fk_QNR_Tags_Question_Rubric1` ON `mydb`.`QNR_Tags` (`idRubric2` ASC) ;
CREATE INDEX `fk_QNR_Tags_Learning_objectives` ON `mydb`.`QNR_Tags` (`idQLO1` ASC) ;
CREATE INDEX `fk_QNR_Tags_Learning_objectives2` ON `mydb`.`QNR_Tags` (`idQLO2` ASC) ;
CREATE INDEX `fk_QNR_Tags_Learning_objectives1` ON `mydb`.`QNR_Tags` (`idQCS1` ASC) ;
CREATE INDEX `fk_QNR_Tags_Learning_objectives3` ON `mydb`.`QNR_Tags` (`idQCS2` ASC) ;
CREATE INDEX `fk_QNR_Tags_QDifficulty` ON `mydb`.`QNR_Tags` (`idQDifficulty` ASC) ;
CREATE INDEX `fk_QNR_Tags_Question` ON `mydb`.`QNR_Tags` (`idQuestion` ASC) ;
-- -----------------------------------------------------
-- Table `mydb`.`Comments`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Comments` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Comments` (
`idComment` VARCHAR(255) NOT NULL ,
`Comment_description` MEDIUMTEXT NULL ,
`Comment_shortname` VARCHAR(45) NULL ,
PRIMARY KEY (`idComment`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Comment_set`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Comment_set` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Comment_set` (
`idComment_set` VARCHAR(255) NOT NULL ,
`Comment_set_shortname` VARCHAR(45) NULL ,
`Comment_set_description` MEDIUMTEXT NULL ,
`idComment1` VARCHAR(255) NULL ,
`idComment2` VARCHAR(255) NULL ,
`idComment3` VARCHAR(255) NULL ,
`idComment4` VARCHAR(255) NULL ,
`idComment5` VARCHAR(255) NULL ,
`idComment6` VARCHAR(255) NULL ,
PRIMARY KEY (`idComment_set`) ,
CONSTRAINT `fk_Comment_set_Comments`
FOREIGN KEY (`idComment1` )
REFERENCES `mydb`.`Comments` (`idComment` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Comment_set_Comments1`
FOREIGN KEY (`idComment2` )
REFERENCES `mydb`.`Comments` (`idComment` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Comment_set_Comments2`
FOREIGN KEY (`idComment3` )
REFERENCES `mydb`.`Comments` (`idComment` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Comment_set_Comments3`
FOREIGN KEY (`idComment4` )
REFERENCES `mydb`.`Comments` (`idComment` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Comment_set_Comments4`
FOREIGN KEY (`idComment5` )
REFERENCES `mydb`.`Comments` (`idComment` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Comment_set_Comments5`
FOREIGN KEY (`idComment6` )
REFERENCES `mydb`.`Comments` (`idComment` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE INDEX `fk_Comment_set_Comments` ON `mydb`.`Comment_set` (`idComment1` ASC) ;
CREATE INDEX `fk_Comment_set_Comments1` ON `mydb`.`Comment_set` (`idComment2` ASC) ;
CREATE INDEX `fk_Comment_set_Comments2` ON `mydb`.`Comment_set` (`idComment3` ASC) ;
CREATE INDEX `fk_Comment_set_Comments3` ON `mydb`.`Comment_set` (`idComment4` ASC) ;
CREATE INDEX `fk_Comment_set_Comments4` ON `mydb`.`Comment_set` (`idComment5` ASC) ;
CREATE INDEX `fk_Comment_set_Comments5` ON `mydb`.`Comment_set` (`idComment6` ASC) ;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
Please use MySQL Workbench 5.2 (5.0 is very old):
http://forums.mysql.com/index.php?151

MySQL 1005 Error - What's wrong with the query?

I'm unable to see the issue with this CREATE TABLE sentences. I've double checked but can't find the error.
-- -----------------------------------------------------
-- Table `agentes`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `agentes` (
`id` INT(11) NOT NULL AUTO_INCREMENT ,
`codAgente` VARCHAR(8) NOT NULL ,
`clave` VARCHAR(8) NOT NULL ,
`nombre` VARCHAR(50) NOT NULL ,
`apellido1` VARCHAR(50) NOT NULL ,
`apellido2` VARCHAR(50) NOT NULL ,
`email` VARCHAR(100) NOT NULL ,
`usuarioDA` VARCHAR(45) NULL ,
`passDA` VARCHAR(100) NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB
COLLATE = latin1_spanish_ci;
-- -----------------------------------------------------
-- Table `cola`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `colas` (
`idCola` INT NOT NULL AUTO_INCREMENT ,
`cliente` INT NOT NULL ,
`nombre` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`idCola`) ,
INDEX `fk_colas_clientes1` (`cliente` ASC) ,
CONSTRAINT `fk_colas_clientes1`
FOREIGN KEY (`cliente` )
REFERENCES `clientes` (`idCliente` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `relColaExt`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `relColaExt` (
`id` INT NOT NULL AUTO_INCREMENT ,
`codAgente` VARCHAR(8) NOT NULL ,
`cola` VARCHAR(45) NOT NULL ,
`prioridad` INT NOT NULL ,
PRIMARY KEY (`id`) ,
UNIQUE INDEX `id` (`id` ASC) ,
INDEX `fk_relColaExt_agentes1` (`codAgente` ASC) ,
INDEX `fk_relColaExt_colas1` (`cola` ASC) ,
CONSTRAINT `fk_relColaExt_agentes1`
FOREIGN KEY (`codAgente` )
REFERENCES `agentes` (`codAgente` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_relColaExt_colas1`
FOREIGN KEY (`cola` )
REFERENCES `colas` (`nombre` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
It throws #1005 - Can't create table './centralita/relColaExt.frm' (errno: 150) creating the relColaExt table.
It's a Foreign Key issue but I'm not able to see what's wrong. Can someone help me, please?
According to this comment on MySQL manual page on Foreign keys, it may be due to different encoding of the VARCHAR columns. In table agentes you defined a collation but in relColaExt you did not.
Try using the same collation on every table (by defining it or by letting the default kick in).