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.
Related
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.
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.
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