DB Schema For Chats? - mysql

I need to store chat conversations in a database schema. The way I would use this database is I would post chats on a website. Each chat would not be more than about 20 responses. Can someone please suggest a schema for this?

Here's a start using MySQL Workbench
and the create 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 `chats` DEFAULT CHARACTER SET utf8 COLLATE default collation ;
-- -----------------------------------------------------
-- Table `chats`.`chat`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `chats`.`chat` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `chats`.`chat_user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `chats`.`chat_user` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`handle` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `chats`.`chat_line`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `chats`.`chat_line` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT ,
`chat_id` INT UNSIGNED NOT NULL ,
`user_id` INT UNSIGNED NOT NULL ,
`line_text` TEXT NOT NULL ,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY (`id`) ,
INDEX `fk_chat_line_chat` (`chat_id` ASC) ,
INDEX `fk_chat_line_chat_user1` (`user_id` ASC) ,
CONSTRAINT `fk_chat_line_chat`
FOREIGN KEY (`chat_id` )
REFERENCES `chats`.`chat` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_chat_line_chat_user1`
FOREIGN KEY (`user_id` )
REFERENCES `chats`.`chat_user` (`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;
And you are welcome to download the MWB file from my dropbox.

Conversation has_may Lines
Line belongs_to User, has content & time

Related

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;

Spring boot application - SQLException: Cannot add foreign key constraint

After some research on this problem, I found out that something is probably not right in my database structure, so I have tried the "SHOW ENGINE INNODB STATUS" command to find out what is causing the foreign key SQLException.
Here is the result on "lastest foreign key error"
If I'm right, the table that causes the problem is a temporary table, that's why I don't know what to do.
The other questions here are usually like having an unsigned int in one table, and signed int at foreign key, so that causes error, but I can't see anything like that in my script and the script itself works fine in mysql workbench, but the spring application throws exception when using this database.
The strange thing is that I don't have origin_id anywhere in my code, be it the java code or the sql.
Here is the database script:
-- MySQL Script generated by MySQL Workbench
-- Tue Oct 9 20:16:01 2018
-- 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';
-- -----------------------------------------------------
-- DATABASE flights
-- -----------------------------------------------------
DROP DATABASE IF EXISTS `flights` ;
-- -----------------------------------------------------
-- DATABASE flights
-- -----------------------------------------------------
CREATE DATABASE `flights` ;
-- -----------------------------------------------------
-- Schema flights
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `flights` ;
-- -----------------------------------------------------
-- Schema flights
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `flights` DEFAULT CHARACTER SET utf8 ;
USE `flights` ;
-- -----------------------------------------------------
-- Table `flights`.`user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `flights`.`user` (
`id` REAL NOT NULL,
`username` VARCHAR(50) NULL,
`password` VARCHAR(50) NULL,
`email` TEXT NULL,
`role` VARCHAR(10) NULL DEFAULT "USER",
`created_at` TIMESTAMP NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `flights`.`airline`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `flights`.`airline` (
`id` REAL NOT NULL auto_increment,
`name` VARCHAR(100) NULL,
`created_at` TIMESTAMP NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `flights`.`plane`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `flights`.`plane` (
`id` REAL NOT NULL auto_increment,
`seats` INT NULL,
`airline_id` REAL NULL,
`created_at` TIMESTAMP NULL,
PRIMARY KEY (`id`),
CONSTRAINT `airline_id`
FOREIGN KEY (`airline_id`)
REFERENCES `flights`.`airline` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
CREATE INDEX `airline_id_idx` ON `flights`.`plane` (`airline_id` ASC) VISIBLE;
-- -----------------------------------------------------
-- Table `flights`.`airport`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `flights`.`airport` (
`id` REAL NOT NULL auto_increment,
`name` VARCHAR(100) NULL,
`city` VARCHAR(50) NULL,
`country` VARCHAR(50) NULL,
`created_at` TIMESTAMP NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `flights`.`flight`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `flights`.`flight` (
`id` REAL NOT NULL auto_increment,
`departure_time` TIMESTAMP NULL,
`arrival_time` TIMESTAMP NULL,
`adult_prise` REAL NULL,
`reduced_prise` REAL NULL,
`plane_id` REAL NULL,
`departure_airport_id` REAL NULL,
`arrival_airport_id` REAL NULL,
`created_at` TIMESTAMP NULL,
`airline_id` REAL NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `airline_id_2`
FOREIGN KEY (`airline_id`)
REFERENCES `flights`.`airline` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `plane_id`
FOREIGN KEY (`plane_id`)
REFERENCES `flights`.`plane` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `departure_airport_id`
FOREIGN KEY (`departure_airport_id`)
REFERENCES `flights`.`airport` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `arrival_airport_id`
FOREIGN KEY (`arrival_airport_id`)
REFERENCES `flights`.`airport` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
CREATE INDEX `plane_id_idx` ON `flights`.`flight` (`plane_id` ASC) VISIBLE;
CREATE INDEX `departure_airport_id_idx` ON `flights`.`flight` (`departure_airport_id` ASC) VISIBLE;
CREATE INDEX `arrival_airport_id_idx` ON `flights`.`flight` (`arrival_airport_id` ASC) VISIBLE;
-- -----------------------------------------------------
-- Table `flights`.`reservation`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `flights`.`reservation` (
`id` REAL NOT NULL,
`reservation_time` TIMESTAMP NULL,
`adult_ticket` SMALLINT NULL,
`reduced_ticket` SMALLINT NULL,
`user_id` REAL NOT NULL,
`flight_id` REAL NOT NULL,
`created_at` TIMESTAMP NULL,
PRIMARY KEY (`id`),
CONSTRAINT `user_id`
FOREIGN KEY (`user_id`)
REFERENCES `flights`.`user` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `flight_id`
FOREIGN KEY (`flight_id`)
REFERENCES `flights`.`flight` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
CREATE INDEX `user_id_idx` ON `flights`.`reservation` (`user_id` ASC) VISIBLE;
CREATE INDEX `flight_id_idx` ON `flights`.`reservation` (`flight_id` ASC) VISIBLE;
USE `flights` ;
-- -----------------------------------------------------
-- Placeholder table for view `flights`.`v_airline`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `flights`.`v_airline` (`id` REAL, `name` VARCHAR(30), `created_at` TIMESTAMP);
-- -----------------------------------------------------
-- Placeholder table for view `flights`.`v_airport`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `flights`.`v_airport` (`id` REAL, `name` VARCHAR(100), `city` VARCHAR(50), `country` VARCHAR(50), `created_at` TIMESTAMP);
-- -----------------------------------------------------
-- Placeholder table for view `flights`.`v_flight`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `flights`.`v_flight` (`id` REAL, `departure_time` TIMESTAMP, `arrival_time` TIMESTAMP, `adult_prise` REAL, `reduced_prise` REAL, `plane_id` REAL, `departure_airport_id` REAL, `arrival_airport_id` REAL, `created_at` TIMESTAMP);
-- -----------------------------------------------------
-- Placeholder table for view `flights`.`v_plane`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `flights`.`v_plane` (`id` REAL, `seats` INT, `airline_id` REAL, `created_at` TIMESTAMP);
-- -----------------------------------------------------
-- Placeholder table for view `flights`.`v_reservation`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `flights`.`v_reservation` (`id` REAL, `reservation_time` TIMESTAMP, `adult_ticket` SMALLINT, `reduced_ticket` SMALLINT, `user_id` REAL, `flight_id` REAL, `created_at` TIMESTAMP);
-- -----------------------------------------------------
-- Placeholder table for view `flights`.`v_user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `flights`.`v_user` (`id` REAL, `username` VARCHAR(50), `password` VARCHAR(50), `email` TEXT, `role` VARCHAR(50), `created_at` TIMESTAMP);
-- -----------------------------------------------------
-- View `flights`.`v_airline`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `flights`.`v_airline`;
USE `flights`;
CREATE OR REPLACE VIEW `v_airline` AS select * from airline;
-- -----------------------------------------------------
-- View `flights`.`v_airport`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `flights`.`v_airport`;
USE `flights`;
CREATE OR REPLACE VIEW `v_airport` AS select * from airport;
-- -----------------------------------------------------
-- View `flights`.`v_flight`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `flights`.`v_flight`;
USE `flights`;
CREATE OR REPLACE VIEW `v_flight` AS select * from flight;
-- -----------------------------------------------------
-- View `flights`.`v_plane`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `flights`.`v_plane`;
USE `flights`;
CREATE OR REPLACE VIEW `v_plane` AS select * from plane;
-- -----------------------------------------------------
-- View `flights`.`v_reservation`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `flights`.`v_reservation`;
USE `flights`;
CREATE OR REPLACE VIEW `v_reservation` AS select * from reservation;
-- -----------------------------------------------------
-- View `flights`.`v_user`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `flights`.`v_user`;
USE `flights`;
CREATE OR REPLACE VIEW `v_user` AS select * from user;
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.

#1005 - Can't create table 'feedback.answer' (errno: 150)

I am getting an error with mysql and I do not understand why:
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 `feedback` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `feedback` ;
-- -----------------------------------------------------
-- Table `feedback`.`application`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `feedback`.`application` (
`application_id` INT NOT NULL AUTO_INCREMENT,
`app_name` VARCHAR(45) NULL,
PRIMARY KEY (`application_id`),
UNIQUE INDEX `app_name_UNIQUE` (`app_name` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `feedback`.`user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `feedback`.`user` (
`user_id` INT NOT NULL AUTO_INCREMENT,
`firstname` VARCHAR(45) NOT NULL,
`lastname` VARCHAR(45) NULL,
`email` VARCHAR(45) NOT NULL,
`customer_length` VARCHAR(45) NULL,
PRIMARY KEY (`user_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `feedback`.`users_has_application`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `feedback`.`users_has_application` (
`user_id` INT NOT NULL,
`application_id` INT NOT NULL,
PRIMARY KEY (`user_id`, `application_id`),
INDEX `fk_users_has_application_application1_idx` (`application_id` ASC),
INDEX `fk_users_has_application_users_idx` (`user_id` ASC),
CONSTRAINT `fk_users_has_application_users`
FOREIGN KEY (`user_id`)
REFERENCES `feedback`.`user` (`user_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_users_has_application_application1`
FOREIGN KEY (`application_id`)
REFERENCES `feedback`.`application` (`application_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `feedback`.`survey`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `feedback`.`survey` (
`survey_id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NULL,
`description` VARCHAR(255) NULL,
`is_active` TINYINT(1) NULL,
PRIMARY KEY (`survey_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `feedback`.`question`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `feedback`.`question` (
`question_id` INT NOT NULL,
`question_text` VARCHAR(255) NULL,
PRIMARY KEY (`question_id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `feedback`.`option`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `feedback`.`option` (
`option_id` INT NOT NULL AUTO_INCREMENT,
`question_id` INT NOT NULL,
`option_number` INT NOT NULL,
`option_text` TEXT NULL,
INDEX `fk_option_question1_idx` (`question_id` ASC),
PRIMARY KEY (`option_id`),
UNIQUE INDEX `uk_question_option_number_key` (`question_id` ASC, `option_number` ASC),
CONSTRAINT `fk_option_question1`
FOREIGN KEY (`question_id`)
REFERENCES `feedback`.`question` (`question_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `feedback`.`answer`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `feedback`.`answer` (
`answer_id` INT NOT NULL AUTO_INCREMENT,
`user_id` INT NOT NULL,
`option_id` INT NOT NULL,
`date_submitted` DATETIME NOT NULL,
PRIMARY KEY (`answer_id`),
INDEX `fk_answer_user1_idx` (`user_id` ASC),
INDEX `fk_answer_option1_idx` (`option_id` ASC),
CONSTRAINT `fk_answer_user1`
FOREIGN KEY (`user_id`)
REFERENCES `feedback`.`user` (`user_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_answer_option1`
FOREIGN KEY (`option_id`)
REFERENCES `feedback`.`option` (`option_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `feedback`.`survey_has_question`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `feedback`.`survey_has_question` (
`survey_id` INT NOT NULL,
`question_id` INT NOT NULL,
`question_number` INT NULL,
PRIMARY KEY (`survey_id`, `question_id`),
INDEX `fk_survey_has_question_question1_idx` (`question_id` ASC),
INDEX `fk_survey_has_question_survey1_idx` (`survey_id` ASC),
UNIQUE INDEX `unique_order_key` (`survey_id` ASC, `question_number` ASC),
CONSTRAINT `fk_survey_has_question_survey1`
FOREIGN KEY (`survey_id`)
REFERENCES `feedback`.`survey` (`survey_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_survey_has_question_question1`
FOREIGN KEY (`question_id`)
REFERENCES `feedback`.`question` (`question_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:
#1005 - Can't create table 'feedback.answer' (errno: 150)
I am creating my table from this as a template:
https://dba.stackexchange.com/questions/16002/survey-database-design-associate-an-answer-to-a-user/16047#16047
My thought process for adding answer_id to the answer table is that I want users to be able to fill out the same survey multiple times.
Why is the answer table throwing an error?
EDIT:
Server version: 5.5.29-0ubuntu0.12.04.2
I am importing this using phpmyadmin
Your code worked on MYSQL server 5.1 without errors.
A common cause of errno: 150 is when you create a FK constraint that references a PK that does not yet exist. Make sure both your "user" and "option" tables are created first before you create the "answer" table.
To help debug you can remove the FK constraints one at a time to see which one is triggering the problem.
If you execute the code in the order shown, I do not see any FK problems that would occur.
try 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,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Table `feedback`.`application`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `application` (
`application_id` INT NOT NULL AUTO_INCREMENT,
`app_name` VARCHAR(45) NULL,
PRIMARY KEY (`application_id`),
UNIQUE INDEX `app_name_UNIQUE` (`app_name` ASC))
;
your working code in fiddle

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.