Mysql Workbench - Error processing forward engineering - mysql

I want to raise a small problem MySQL Workbench, but first of all I want you to know that this question previously made, but without much explanation and a lot of code that leaves many questions, and as I have not been to find the problem, the question will easy and explanatory possible.
1) I made a simple model in MySQL Workbench, a relationship between two tables.
If I make the execution "Forward engineering" of this model is created without problem.
But the problem it is following...
The code that generates me MySQL Workbench:
-- MySQL Workbench Forward Engineering
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';
-- -----------------------------------------------------
-- Schema example
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema example
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `example` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `example` ;
-- -----------------------------------------------------
-- Table `example`.`status`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `example`.`status` (
`id` INT UNSIGNED NOT NULL,
`name` VARCHAR(45) NULL,
`create_at` DATETIME NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `example`.`users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `example`.`users` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NULL,
`email` VARCHAR(45) NULL,
`password` VARCHAR(45) NULL,
`create_at` DATETIME NULL,
`status_id` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_users_status_idx` (`status_id` ASC),
CONSTRAINT `fk_users_status`
FOREIGN KEY (`status_id`)
REFERENCES `example`.`status` (`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;
2) I need to make an optimization to the table status, and I will make a simple change because the status.id only have a maximum of 10 records, and the data type INT id field think it's too big for this then what I would do is change id INT to id TINYINT
I made this simple change and stuck:
The code that generates me MySQL Workbench:
-- MySQL Workbench Forward Engineering
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';
-- -----------------------------------------------------
-- Schema example
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema example
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `example` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `example` ;
-- -----------------------------------------------------
-- Table `example`.`status`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `example`.`status` (
`id` TINYINT UNSIGNED NOT NULL,
`name` VARCHAR(45) NULL,
`create_at` DATETIME NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `example`.`users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `example`.`users` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NULL,
`email` VARCHAR(45) NULL,
`password` VARCHAR(45) NULL,
`create_at` DATETIME NULL,
`status_id` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_users_status_idx` (`status_id` ASC),
CONSTRAINT `fk_users_status`
FOREIGN KEY (`status_id`)
REFERENCES `example`.`status` (`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;
Error: If I make the execution "Forward engineering" of this model it shows me the following error:
I would greatly appreciate your help, thank you very much.

Related

I'm new to and and ran into a little trouble with a learning project I'm working on

ERROR: Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right
syntax to use near 'NOT NULL,
-- MySQL Workbench Forward Engineering
I don't understand what I did wrong?
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='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
-- -----------------------------------------------------
-- Schema filmKategory
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema filmKategory
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `filmKategory` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci ;
USE `filmKategory` ;
-- -----------------------------------------------------
-- Table `filmKategory`.`category`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `filmKategory`.`category` (
`category_id` INT NOT NULL,
`name` VARCHAR(255) NOT NULL,
PRIMARY KEY (`category_id`));
-- -----------------------------------------------------
-- Table `filmKategory`.`new_table`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `filmKategory`.`new_table` (
`ıd` INT NOT NULL,
`name` VARCHAR(45) NOT NULL,
`price` (10,0) INT NOT NULL,
`images` VARCHAR(45) NULL DEFAULT NULL,
`Stock` VARCHAR(45) NULL DEFAULT '5',
`Category` VARCHAR(2000) NULL DEFAULT NULL,
`category_category_id` INT NULL DEFAULT NULL,
PRIMARY KEY (`ıd`),
UNIQUE INDEX `ıd_UNIQUE` (`ıd` ASC) VISIBLE,
INDEX `fk_new_table_category_idx` (`category_category_id` ASC) VISIBLE,
CONSTRAINT `fk_new_table_category`
FOREIGN KEY (`category_category_id`)
REFERENCES `filmKategory`.`category` (`category_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
AUTO_INCREMENT = 33
DEFAULT CHARACTER SET = utf8mb4
COLLATE = utf8mb4_0900_ai_ci;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;

Why am I not able to inspect my database's tables?

I'll start by saying that my experience in mySQL is limited. I've only been using workbench for a few months.
Me and a couple of buddies are working on a database for a pet shop scenario, and I've been tasked with writing some queries to test it out. At first my pal's script for the database wasn't running, because I was getting errors with the "VISIBLE" keyword. Since indexes are visible by default I chucked them all out. Then I got an error that some of his prim keys were set to null. I assumed that was by accident and set them all to "NOT NULL".
I proceeded then to run the script, and got no errors, so I assumed everything was fine.
He had told me he recently populated the tables, but it doesn't look like he did. I mean, I'm not certain that he did but he seems to have the columns set up nicely at the very least.
When I ran the code, I inspected the tables but there were no tables listed.
I am not sure where to go from here. I'll attach the code in question. If anyone can point me in the right direction, I'd be grateful.
-- Tue Nov 24 20:45:34 2020
-- Model: New Model Version: 1.0
-- MySQL Workbench Forward Engineering
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='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`species`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`species` (
`species_id` INT NOT NULL,
`species_name` VARCHAR(45) NOT NULL,
PRIMARY KEY (`species_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`animals`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`animals` (
`animal_id` INT NOT NULL,
`species_id` INT NOT NULL,
`animal_name` VARCHAR(45) NOT NULL,
`price` DECIMAL(8,2) NOT NULL,
`date_arived` DATETIME not NULL,
`quantity` INT NOT NULL,
PRIMARY KEY (`animal_id`),
INDEX `species_id_fk_idx` (`species_id` ASC),
CONSTRAINT `species_id_fk`
FOREIGN KEY (`species_id`)
REFERENCES `mydb`.`species` (`species_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`distributors`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`distributors` (
`distributor_id` INT NOT NULL,
`distributor_email` VARCHAR(255) NOT NULL,
`distributor_name` VARCHAR(45) NOT NULL,
`distributor_address` VARCHAR(45) NOT NULL,
`distributor_phone` VARCHAR(15) NOT NULL,
PRIMARY KEY (`distributor_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`orders`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`orders` (
`order_number` INT not NULL,
`distributor_id` INT NOT NULL,
`order_date` DATETIME not NULL,
`arival_date` DATETIME not NULL,
PRIMARY KEY (`order_number`),
INDEX `distributor_id_fk_idx` (`distributor_id` ASC),
CONSTRAINT `distributor_id_fk`
FOREIGN KEY (`distributor_id`)
REFERENCES `mydb`.`distributors` (`distributor_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`invoice`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`invoice` (
`invoice_id` INT NOT NULL,
`order_number` INT NOT NULL,
`animal_id` INT NOT NULL,
`animal_price` DECIMAL(8,2) NOT NULL,
`quantity` INT NOT NULL,
PRIMARY KEY (`invoice_id`),
INDEX `order_number_fk_idx` (`order_number` ASC),
INDEX `animal_id_fk_idx` (`animal_id` ASC),
CONSTRAINT `order_number_fk`
FOREIGN KEY (`order_number`)
REFERENCES `mydb`.`orders` (`order_number`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `animal_id_fk`
FOREIGN KEY (`animal_id`)
REFERENCES `mydb`.`animals` (`animal_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;

How to model multiple members of groups?

How should one model multiple members of groups? Let me give an example.
I have people, houses, and household items.
All houses are owned by a single person and a person can own more than one house.
All household items are located in a single house, and are owned by the owner of the house. For my particular situation, I would also like a incremental counter for all household items owned by a given person.
In addition, while all household items share some properties, they also have some properties based on the type of household item they are. Assume there are only a few types of household items such as waterbeds, televisions, and refrigerators. Note that it must also be possible to link a household item to another table (for instance, otherPeopleInterestedInBuyingAHouseholdItem must have a foreign key to household items regardless of the type of household item).
So, houses are the group, and householdItems are members of that group.
How should this be modeled? Two possibilities are below. Is one better than the other given the above rules? I recognize that the first option with houses composite incrementing primary key doesn't play nice with InnoDB, however, can be implemented with triggers/etc which is acceptable. Or maybe a third option which is better?
-- MySQL Script generated by MySQL Workbench
-- 02/16/17 16:01:38
-- Model: New Model Version: 1.0
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';
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`people`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`people` (
`idpeople` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`age` INT NOT NULL,
PRIMARY KEY (`idpeople`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`houses`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`houses` (
`idhouses` INT NOT NULL AUTO_INCREMENT,
`people_idpeople` INT NOT NULL,
`address` VARCHAR(45) NOT NULL,
`square_feet` INT NOT NULL,
PRIMARY KEY (`idhouses`),
INDEX `fk_houses_people1_idx` (`people_idpeople` ASC),
CONSTRAINT `fk_houses_people1`
FOREIGN KEY (`people_idpeople`)
REFERENCES `mydb`.`people` (`idpeople`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`houseHoldItems`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`houseHoldItems` (
`idhouseHoldItems` INT NOT NULL AUTO_INCREMENT,
`houses_idhouses` INT NOT NULL,
`value` DECIMAL(6,2) NOT NULL,
`dateBought` DATETIME NOT NULL,
PRIMARY KEY (`idhouseHoldItems`, `houses_idhouses`),
INDEX `fk_houseHoldItems_houses_idx` (`houses_idhouses` ASC),
CONSTRAINT `fk_houseHoldItems_houses`
FOREIGN KEY (`houses_idhouses`)
REFERENCES `mydb`.`houses` (`idhouses`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`waterBeds`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`waterBeds` (
`houseHoldItems_idhouseHoldItems` INT NOT NULL,
`houseHoldItems_houses_idhouses` INT NOT NULL,
`gallonsWaterSize` INT NOT NULL,
PRIMARY KEY (`houseHoldItems_idhouseHoldItems`, `houseHoldItems_houses_idhouses`),
CONSTRAINT `fk_waterBeds_houseHoldItems1`
FOREIGN KEY (`houseHoldItems_idhouseHoldItems` , `houseHoldItems_houses_idhouses`)
REFERENCES `mydb`.`houseHoldItems` (`idhouseHoldItems` , `houses_idhouses`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`televisions`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`televisions` (
`houseHoldItems_idhouseHoldItems` INT NOT NULL,
`houseHoldItems_houses_idhouses` INT NOT NULL,
`screenSize` INT NOT NULL,
`brandName` VARCHAR(45) NOT NULL,
PRIMARY KEY (`houseHoldItems_idhouseHoldItems`, `houseHoldItems_houses_idhouses`),
CONSTRAINT `fk_televisions_houseHoldItems1`
FOREIGN KEY (`houseHoldItems_idhouseHoldItems` , `houseHoldItems_houses_idhouses`)
REFERENCES `mydb`.`houseHoldItems` (`idhouseHoldItems` , `houses_idhouses`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`refrigerators`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`refrigerators` (
`houseHoldItems_idhouseHoldItems` INT NOT NULL,
`houseHoldItems_houses_idhouses` INT NOT NULL,
`icecubeCapacity` INT NOT NULL,
`color` VARCHAR(45) NOT NULL,
PRIMARY KEY (`houseHoldItems_idhouseHoldItems`, `houseHoldItems_houses_idhouses`),
CONSTRAINT `fk_refrigerators_houseHoldItems1`
FOREIGN KEY (`houseHoldItems_idhouseHoldItems` , `houseHoldItems_houses_idhouses`)
REFERENCES `mydb`.`houseHoldItems` (`idhouseHoldItems` , `houses_idhouses`)
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;
-- MySQL Script generated by MySQL Workbench
-- 02/16/17 16:00:37
-- Model: New Model Version: 1.0
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';
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`people`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`people` (
`idpeople` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`age` INT NOT NULL,
PRIMARY KEY (`idpeople`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`houses`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`houses` (
`idhouses` INT NOT NULL AUTO_INCREMENT,
`people_idpeople` INT NOT NULL,
`address` VARCHAR(45) NOT NULL,
`square_feet` INT NOT NULL,
PRIMARY KEY (`idhouses`),
INDEX `fk_houses_people1_idx` (`people_idpeople` ASC),
CONSTRAINT `fk_houses_people1`
FOREIGN KEY (`people_idpeople`)
REFERENCES `mydb`.`people` (`idpeople`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`houseHoldItems`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`houseHoldItems` (
`idhouseHoldItems` INT NOT NULL AUTO_INCREMENT,
`houses_idhouses` INT NOT NULL,
`uniqueKey` INT NOT NULL,
`value` DECIMAL(6,2) NOT NULL,
`dateBought` DATETIME NOT NULL,
PRIMARY KEY (`idhouseHoldItems`),
INDEX `fk_houseHoldItems_houses1_idx` (`houses_idhouses` ASC),
UNIQUE INDEX `unique_key` (`houses_idhouses` ASC, `uniqueKey` ASC),
CONSTRAINT `fk_houseHoldItems_houses1`
FOREIGN KEY (`houses_idhouses`)
REFERENCES `mydb`.`houses` (`idhouses`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`waterBeds`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`waterBeds` (
`gallonsWaterSize` INT NOT NULL,
`houseHoldItems_idhouseHoldItems` INT NOT NULL,
PRIMARY KEY (`houseHoldItems_idhouseHoldItems`),
CONSTRAINT `fk_waterBeds_houseHoldItems1`
FOREIGN KEY (`houseHoldItems_idhouseHoldItems`)
REFERENCES `mydb`.`houseHoldItems` (`idhouseHoldItems`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`televisions`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`televisions` (
`screenSize` INT NOT NULL,
`brandName` VARCHAR(45) NOT NULL,
`houseHoldItems_idhouseHoldItems` INT NOT NULL,
PRIMARY KEY (`houseHoldItems_idhouseHoldItems`),
CONSTRAINT `fk_televisions_houseHoldItems1`
FOREIGN KEY (`houseHoldItems_idhouseHoldItems`)
REFERENCES `mydb`.`houseHoldItems` (`idhouseHoldItems`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`refrigerators`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`refrigerators` (
`icecubeCapacity` INT NOT NULL,
`color` VARCHAR(45) NOT NULL,
`houseHoldItems_idhouseHoldItems` INT NOT NULL,
PRIMARY KEY (`houseHoldItems_idhouseHoldItems`),
CONSTRAINT `fk_refrigerators_houseHoldItems1`
FOREIGN KEY (`houseHoldItems_idhouseHoldItems`)
REFERENCES `mydb`.`houseHoldItems` (`idhouseHoldItems`)
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;
Prefixing column names with the tablename gets tedious and unnecessary.
EAV is a bad way to go, but it is better than having separate tables for refrigerators and televisions. Follow the tag I added. The bottom line: have columns for columns you need to manipulate in MySQL throw the rest into a single JSON column.
For "many:many" tables, see my tips.

ERROR: Error 1215: Cannot add foreign key constraint even when both fields are of the same type

When trying to build a mysql database consisting of two tables, some reason I am getting Error 1215 and I'm unable to find the cause. Both fields are of the same type. I am trying to do this with MySQL workbench. Anyone knows the cause of this error?
-- MySQL Workbench Forward Engineering
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';
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`Classified_tweets`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Classified_tweets` (
`QueryID` INT NULL,
`TweetID` BIGINT NOT NULL,
`TweetKeyword` VARCHAR(45) NULL,
`TweetUsername` VARCHAR(45) NULL,
`TweetDate` VARCHAR(45) NULL,
`TweetLocation` VARCHAR(45) NULL,
`TweetContent` VARCHAR(150) NULL,
`TweetLabel` TINYINT(1) NULL,
PRIMARY KEY (`TweetID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Words_frequency`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Words_frequency` (
`QueryID` INT NOT NULL,
`Word` VARCHAR(45) NOT NULL,
`Label` INT NULL,
`Frequency` VARCHAR(45) NULL,
PRIMARY KEY (`QueryID`, `Word`),
INDEX `fk_Words_frequency_Classified_tweets_idx` (`QueryID` ASC),
CONSTRAINT `fk_Words_frequency_Classified_tweets`
FOREIGN KEY (`QueryID`)
REFERENCES `mydb`.`Classified_tweets` (`QueryID`)
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;
You have defined the primary key on Classified_tweets as TweetID.
You should be using this column for the foreign key relationship. Perhaps:
CREATE TABLE IF NOT EXISTS `mydb`.`Words_frequency` (
`TweetID` BIGINT NOT NULL,
`Word` VARCHAR(45) NOT NULL,
`Label` INT NULL,
`Frequency` VARCHAR(45) NULL,
PRIMARY KEY (`QueryID`, `Word`),
INDEX `fk_Words_frequency_Classified_tweets_idx` (`QueryID` ASC),
CONSTRAINT `fk_Words_frequency_Classified_tweets`
FOREIGN KEY (`TweetID`)
REFERENCES `mydb`.`Classified_tweets` (`TweetID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
);
Note: I changed the first column from QueryId to TweetId. This should fix the problem, although your intention might be something else.
You can only refer to a column which is a primary key or a unique attribute. If you want to refer to query ID itself, then make it as the primary key or a unique attribute. or change the Words_frequency table to refer to tweetID.

MySQL foreign key creation with alter table command

I created some tables using MySQL Workbench, and then did forward ‘forward engineer’ to create scripts to create these tables. BUT, the scripts lead me to a number of problems. One of which involves the foreign keys. So I tried creating separate foreign key additions using alter table and I am still getting problems. The code is below (the set statements, drop/create statements I left in … though I don’t think they should matter for this):
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 `mydb` ;
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
-- -----------------------------------------------------
-- Table `mydb`.`User`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`User` ;
CREATE TABLE IF NOT EXISTS `mydb`.`User` (
`UserName` VARCHAR(35) NOT NULL ,
`Num_Accts` INT NOT NULL ,
`Password` VARCHAR(45) NULL ,
`Email` VARCHAR(45) NULL ,
`User_ID` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`User_ID`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`User_Space`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`User_Space` ;
CREATE TABLE IF NOT EXISTS `mydb`.`User_Space` (
`User_UserName` VARCHAR(35) NOT NULL ,
`User_Space_ID` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`User_Space_ID`),
FOREIGN KEY (`User_UserName`)
REFERENCES `mydb`.`User` (`UserName`)
ON UPDATE CASCADE ON DELETE CASCADE)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
The error this produces is:
Error Code: 1005 Can't create table 'mydb.user_space' (errno: 150)
Anybody know what the heck I’m doing wrong?? And anybody else have problems with the script generation done by mysql workbench? It’s a nice tool, but annoying that it pumps out scripts that don’t work for me.
[As an fyi here’s the script it auto-generates:
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 `mydb` ;
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
-- -----------------------------------------------------
-- Table `mydb`.`User`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`User` ;
CREATE TABLE IF NOT EXISTS `mydb`.`User` (
`UserName` VARCHAR(35) NOT NULL ,
`Num_Accts` INT NOT NULL ,
`Password` VARCHAR(45) NULL ,
`Email` VARCHAR(45) NULL ,
`User_ID` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`User_ID`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`User_Space`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`User_Space` ;
CREATE TABLE IF NOT EXISTS `mydb`.`User_Space` (
`User_Space_ID` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`User_Space_ID`) ,
INDEX `User_ID` () ,
CONSTRAINT `User_ID`
FOREIGN KEY ()
REFERENCES `mydb`.`User` ()
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;
**
Thanks!]
I haven't used MySQL Workbench to generate a lot of schemas, but I found the problem in the script. The foreign key definition in the User_Space table is attempting to create a foreign key on an unindexed column in the User table. If you alter the User definition to have an index on UserName, like this:
CREATE TABLE IF NOT EXISTS `mydb`.`User` (
`UserName` VARCHAR(35) NOT NULL ,
`Num_Accts` INT NOT NULL ,
`Password` VARCHAR(45) NULL ,
`Email` VARCHAR(45) NULL ,
`User_ID` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`User_ID`),
INDEX(`UserName`)
)
ENGINE = InnoDB;
... the script will succeed. It sounds like MySQL Workbench probably isn't taking indexes into account when it generates the foreign key definitions. I'm not sure if you can fix this in your schema diagrams or if it's a bigger bug in the program, but I'd see if you could add index definitions in the right places and determine if that fixes the script generation.