Spring boot application - SQLException: Cannot add foreign key constraint - mysql

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;

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;

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.

Out of range decimal for column 'Price' at row 1

I am trying to populate the fields of a database and I keep getting errors no matter how I change my decimal value. I had my decimal first set to (2,2) and then I changed it to (4,2)-- this was after I got some feedback and did some research. Any help would be appreciate it. Thank you.
My insert statement:
INSERT INTO Ingredient(Name, Price)
values ('Chocolate', '10.50');
And the database:
-- 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 BrendaStore
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `BrendaStore` ;
-- -----------------------------------------------------
-- Schema BrendaStore
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `BrendaStore` DEFAULT CHARACTER SET utf8 ;
SHOW WARNINGS;
USE `BrendaStore` ;
-- -----------------------------------------------------
-- Table `Vendor`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Vendor` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `Vendor` (
`idVendor` INT NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(45) NOT NULL,
`Address` VARCHAR(45) NOT NULL,
PRIMARY KEY (`idVendor`))
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `Shop`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Shop` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `Shop` (
`idShop` INT NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(45) NOT NULL,
`Address` VARCHAR(45) NOT NULL,
`PhoneNumber` VARCHAR(45) NOT NULL,
PRIMARY KEY (`idShop`))
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `Product`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Product` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `Product` (
`idProduct` INT NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(45) NOT NULL,
`ProductPrice` DECIMAL(4,2) NOT NULL,
`Shop_idShop` INT NOT NULL,
PRIMARY KEY (`idProduct`),
INDEX `fk_Product_Shop1_idx` (`Shop_idShop` ASC),
CONSTRAINT `fk_Product_Shop1`
FOREIGN KEY (`Shop_idShop`)
REFERENCES `Shop` (`idShop`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `Recipe`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Recipe` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `Recipe` (
`idRecipe` INT NOT NULL AUTO_INCREMENT,
`IngredientAmount` VARCHAR(45) NOT NULL,
`Product_idProduct` INT NOT NULL,
PRIMARY KEY (`idRecipe`),
INDEX `fk_Recipe_Product1_idx` (`Product_idProduct` ASC),
CONSTRAINT `fk_Recipe_Product1`
FOREIGN KEY (`Product_idProduct`)
REFERENCES `Product` (`idProduct`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `Ingredient`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Ingredient` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `Ingredient` (
`idIngredient` INT NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(45) NOT NULL,
`Price` DECIMAL(2,2) NOT NULL,
`Vendor_idVendor` INT NOT NULL,
`Recipe_idRecipe` INT NOT NULL,
`Product_idProduct` INT NOT NULL,
PRIMARY KEY (`idIngredient`),
INDEX `fk_Ingredient_Vendor_idx` (`Vendor_idVendor` ASC),
INDEX `fk_Ingredient_Recipe1_idx` (`Recipe_idRecipe` ASC),
INDEX `fk_Ingredient_Product1_idx` (`Product_idProduct` ASC),
CONSTRAINT `fk_Ingredient_Vendor`
FOREIGN KEY (`Vendor_idVendor`)
REFERENCES `Vendor` (`idVendor`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Ingredient_Recipe1`
FOREIGN KEY (`Recipe_idRecipe`)
REFERENCES `Recipe` (`idRecipe`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Ingredient_Product1`
FOREIGN KEY (`Product_idProduct`)
REFERENCES `Product` (`idProduct`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `Client`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Client` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `Client` (
`idClient` INT NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(45) NOT NULL,
`PhoneNumber` VARCHAR(45) NOT NULL,
`Email` VARCHAR(45) NOT NULL,
PRIMARY KEY (`idClient`))
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `Purchase`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Purchase` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `Purchase` (
`idPurchase` INT NOT NULL AUTO_INCREMENT,
`PurchaseDate` DATETIME NOT NULL,
`PurchaseAmount` DECIMAL(2,2) NOT NULL,
`Client_idClient` INT NOT NULL,
PRIMARY KEY (`idPurchase`),
INDEX `fk_Purchase_Client1_idx` (`Client_idClient` ASC),
CONSTRAINT `fk_Purchase_Client1`
FOREIGN KEY (`Client_idClient`)
REFERENCES `Client` (`idClient`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `Orders`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Orders` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `Orders` (
`idOrders` INT NOT NULL AUTO_INCREMENT,
`Date` DATETIME NOT NULL,
`Quantity` VARCHAR(45) NOT NULL,
`Price` VARCHAR(45) NOT NULL,
PRIMARY KEY (`idOrders`))
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `Employee`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Employee` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `Employee` (
`idEmployee` INT NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(45) NOT NULL,
`Address` VARCHAR(45) NOT NULL,
`PhoneNumber` VARCHAR(45) NOT NULL,
PRIMARY KEY (`idEmployee`))
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `Shop_has_Employee`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Shop_has_Employee` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `Shop_has_Employee` (
`Shop_idShop` INT NOT NULL,
`Employee_idEmployee` INT NOT NULL,
PRIMARY KEY (`Shop_idShop`, `Employee_idEmployee`),
INDEX `fk_Shop_has_Employee_Employee1_idx` (`Employee_idEmployee` ASC),
INDEX `fk_Shop_has_Employee_Shop1_idx` (`Shop_idShop` ASC),
CONSTRAINT `fk_Shop_has_Employee_Shop1`
FOREIGN KEY (`Shop_idShop`)
REFERENCES `Shop` (`idShop`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Shop_has_Employee_Employee1`
FOREIGN KEY (`Employee_idEmployee`)
REFERENCES `Employee` (`idEmployee`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `Ingredient_has_Orders`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Ingredient_has_Orders` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `Ingredient_has_Orders` (
`Ingredient_idIngredient` INT NOT NULL,
`Orders_idOrders` INT NOT NULL,
PRIMARY KEY (`Ingredient_idIngredient`, `Orders_idOrders`),
INDEX `fk_Ingredient_has_Orders_Orders1_idx` (`Orders_idOrders` ASC),
INDEX `fk_Ingredient_has_Orders_Ingredient1_idx` (`Ingredient_idIngredient` ASC),
CONSTRAINT `fk_Ingredient_has_Orders_Ingredient1`
FOREIGN KEY (`Ingredient_idIngredient`)
REFERENCES `Ingredient` (`idIngredient`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Ingredient_has_Orders_Orders1`
FOREIGN KEY (`Orders_idOrders`)
REFERENCES `Orders` (`idOrders`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `Product_has_Purchase`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Product_has_Purchase` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `Product_has_Purchase` (
`Product_idProduct` INT NOT NULL,
`Purchase_idPurchase` INT NOT NULL,
PRIMARY KEY (`Product_idProduct`, `Purchase_idPurchase`),
INDEX `fk_Product_has_Purchase_Purchase1_idx` (`Purchase_idPurchase` ASC),
INDEX `fk_Product_has_Purchase_Product1_idx` (`Product_idProduct` ASC),
CONSTRAINT `fk_Product_has_Purchase_Product1`
FOREIGN KEY (`Product_idProduct`)
REFERENCES `Product` (`idProduct`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Product_has_Purchase_Purchase1`
FOREIGN KEY (`Purchase_idPurchase`)
REFERENCES `Purchase` (`idPurchase`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SHOW WARNINGS;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;

SQL: insert into 3 tables with a foreign key

I am trying to add data to 3 tables. The table "Gebruiker" is the parent table.
Wen i use the following code it gives me the follow error:
My SQL code:
SELECT *
FROM Gebruiker
INNER JOIN Inlog
ON Gebruiker.idGebruiker=Inlog.Gebruiker_idGebruiker
INNER JOIN GGevens
ON Gebruiker.idGebruiker=GGevens.Gebruiker_idGebruiker
INNER JOIN Domein
ON Gebruiker.Domein_idDomeint=Domein.idDomeint;
BEGIN;
INSERT INTO Gebruiker (Domein_idDomeint,idGebruiker)
VALUES (1,NULL);
INSERT INTO Inlog (Gebruiker_idGebruiker,UserName,UserPass)
VALUES (LAST_INSERT_ID(),'profile','drie');
INSERT INTO GGevens (Gebruiker_idGebruiker,Email,Voornaam,Tussenvoeg,Achternaam,Geslacht,Opleiding,GebDatum)
VALUES (LAST_INSERT_ID(),'aapje#peer.nl','Aapje','van','Drie',1,'Zeerslim','2014-11-11');
COMMIT;
Error:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`zolstm001`.`GGevens`, CONSTRAINT `fk_GGevens_Gebruiker1` FOREIGN KEY (`Gebruiker_idGebruiker`) REFERENCES `Gebruiker` (`idGebruiker`) ON DELETE NO ACTION ON UPDATE NO ACTION)
My database structure:
http://i.imgur.com/bxeQbhQ.png
I think its because the table "GGevens" needs a foreign key from the table "Gebruiker"
Does someone know how i can add this?
(LAST_INSERT_ID() does not work...
SQL code to create the tables:
-- 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 zolstm001
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema zolstm001
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `zolstm001` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `zolstm001` ;
-- -----------------------------------------------------
-- Table `zolstm001`.`Domein`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`Domein` (
`idDomeint` INT NOT NULL AUTO_INCREMENT,
`Naam` VARCHAR(45) NOT NULL,
`OverigeGegevens` VARCHAR(45) NULL,
PRIMARY KEY (`idDomeint`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `zolstm001`.`Gebruiker`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`Gebruiker` (
`idGebruiker` INT NOT NULL AUTO_INCREMENT,
`Domein_idDomeint` INT NOT NULL,
PRIMARY KEY (`idGebruiker`),
INDEX `fk_Gebruiker_Domein1_idx` (`Domein_idDomeint` ASC),
CONSTRAINT `fk_Gebruiker_Domein1`
FOREIGN KEY (`Domein_idDomeint`)
REFERENCES `zolstm001`.`Domein` (`idDomeint`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `zolstm001`.`GGevens`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`GGevens` (
`idGGevens` INT NOT NULL AUTO_INCREMENT,
`Gebruiker_idGebruiker` INT NOT NULL,
`Email` VARCHAR(45) NOT NULL,
`GebDatum` DATE NOT NULL,
PRIMARY KEY (`idGGevens`),
INDEX `fk_GGevens_Gebruiker1_idx` (`Gebruiker_idGebruiker` ASC),
CONSTRAINT `fk_GGevens_Gebruiker1`
FOREIGN KEY (`Gebruiker_idGebruiker`)
REFERENCES `zolstm001`.`Gebruiker` (`idGebruiker`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `zolstm001`.`Inlog`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`Inlog` (
`idInlog` INT NOT NULL AUTO_INCREMENT,
`UserName` VARCHAR(45) NOT NULL,
`UserPass` VARCHAR(45) NOT NULL,
`Gebruiker_idGebruiker` INT NOT NULL,
PRIMARY KEY (`idInlog`),
INDEX `fk_Inlog_Gebruiker1_idx` (`Gebruiker_idGebruiker` ASC),
CONSTRAINT `fk_Inlog_Gebruiker1`
FOREIGN KEY (`Gebruiker_idGebruiker`)
REFERENCES `zolstm001`.`Gebruiker` (`idGebruiker`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `zolstm001`.`Startup`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`Startup` (
`idStartup` INT NOT NULL AUTO_INCREMENT,
`Naam` VARCHAR(45) NOT NULL,
`Gebruiker_idGebruiker` INT NOT NULL,
PRIMARY KEY (`idStartup`),
INDEX `fk_Startup_Gebruiker1_idx` (`Gebruiker_idGebruiker` ASC),
CONSTRAINT `fk_Startup_Gebruiker1`
FOREIGN KEY (`Gebruiker_idGebruiker`)
REFERENCES `zolstm001`.`Gebruiker` (`idGebruiker`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `zolstm001`.`Reactie`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`Reactie` (
`idReactie` INT NOT NULL AUTO_INCREMENT,
`Gebruiker_idGebruiker` INT NOT NULL,
`Bericht` VARCHAR(45) NOT NULL,
`Startup_idStartup` INT NOT NULL,
`Reactiecol` VARCHAR(45) NULL,
PRIMARY KEY (`idReactie`),
INDEX `fk_Reactie_Gebruiker1_idx` (`Gebruiker_idGebruiker` ASC),
INDEX `fk_Reactie_Startup1_idx` (`Startup_idStartup` ASC),
CONSTRAINT `fk_Reactie_Gebruiker1`
FOREIGN KEY (`Gebruiker_idGebruiker`)
REFERENCES `zolstm001`.`Gebruiker` (`idGebruiker`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Reactie_Startup1`
FOREIGN KEY (`Startup_idStartup`)
REFERENCES `zolstm001`.`Startup` (`idStartup`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `zolstm001`.`Category`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`Category` (
`idCategory` INT NOT NULL AUTO_INCREMENT,
`Startup_idStartup` INT NOT NULL,
PRIMARY KEY (`idCategory`),
INDEX `fk_Category_Startup1_idx` (`Startup_idStartup` ASC),
CONSTRAINT `fk_Category_Startup1`
FOREIGN KEY (`Startup_idStartup`)
REFERENCES `zolstm001`.`Startup` (`idStartup`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `zolstm001`.`Unit`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`Unit` (
`idUnit` INT NOT NULL AUTO_INCREMENT,
`Startup_idStartup` INT NOT NULL,
PRIMARY KEY (`idUnit`),
INDEX `fk_Unit_Startup1_idx` (`Startup_idStartup` ASC),
CONSTRAINT `fk_Unit_Startup1`
FOREIGN KEY (`Startup_idStartup`)
REFERENCES `zolstm001`.`Startup` (`idStartup`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `zolstm001`.`SGegevens`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `zolstm001`.`SGegevens` (
`idSGegevens` INT NOT NULL AUTO_INCREMENT,
`Startup_idStartup` INT NOT NULL,
PRIMARY KEY (`idSGegevens`),
INDEX `fk_SGegevens_Startup1_idx` (`Startup_idStartup` ASC),
CONSTRAINT `fk_SGegevens_Startup1`
FOREIGN KEY (`Startup_idStartup`)
REFERENCES `zolstm001`.`Startup` (`idStartup`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
I would change the INSERT INTO gebruiker to
INSERT INTO Gebruiker (Domein_idDomeint) VALUES (1);
Furthermore to get this working, you will need to use some programming code to help you. This can be done in the shape of a stored procedure.
CREATE PROCEDURE gebruikerProc (IN {your_params})
BEGIN
DECLARE lastId INT;
INSERT INTO Gebruiker (Domein_idDomeint) VALUES (1);
SET lastId=LAST_INSERT_ID();
INSERT INTO Inlog (Gebruiker_idGebruiker,UserName,UserPass)
VALUES (lastId,'profile','drie');
INSERT INTO GGevens (Gebruiker_idGebruiker,Email,Voornaam,Tussenvoeg,Achternaam,Geslacht,Opleiding,GebDatum)
VALUES (lastId,'aapje#peer.nl','Aapje','van','Drie',1,'Zeerslim','2014-11-11');
END;
One warning: Do not use TAB to indent your code in a stored procedure/function/trigger etc in MySQL. You will get an error. It just can not handle TAB.

#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