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;
Related
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;
I have been using MySQL to design a database, when compiled into SQL code and executed it produces two errors. One that states Variable 'unique checks' can't be set to the value of 'NULL' and another that produces message "Duplicate key on write and update" I have a feeling that it is my foreign keys that are causing this issue but still cannot fix the problem. I have attached the query code below.
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `mydb` ;
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`User`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`User` ;
CREATE TABLE IF NOT EXISTS `mydb`.`User` (
`idUser` INT NOT NULL,
`first_name` VARCHAR(45) NULL,
`last_name` VARCHAR(45) NULL,
`username` VARCHAR(45) NULL,
`password` VARCHAR(45) NULL,
`company_pin` VARCHAR(45) NULL,
`student_pin` VARCHAR(45) NULL,
`isAdmin` TINYINT(1) NULL DEFAULT 0,
`isCandidate` TINYINT(1) NULL,
`isVoter` TINYINT(1) NULL,
`votes_left` INT NULL,
`votes_achieved` INT NULL,
PRIMARY KEY (`idUser`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Issue`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Issue` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Issue` (
`idIssue` INT NOT NULL,
`issueTitle` VARCHAR(45) NULL,
`issueDescription` VARCHAR(255) NULL,
`userID` INT NULL,
`isResolved` TINYINT(1) NULL DEFAULT 0,
`upVotes` INT NULL,
PRIMARY KEY (`idIssue`),
INDEX `FK_USERID_idx` (`userID` ASC),
CONSTRAINT `FK_USERID`
FOREIGN KEY (`userID`)
REFERENCES `mydb`.`User` (`idUser`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Forum`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Forum` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Forum` (
`idForum` INT NOT NULL,
`userID` INT NULL,
`postTitle` VARCHAR(45) NULL,
`postText` VARCHAR(255) NULL,
`upVotes` INT NULL,
PRIMARY KEY (`idForum`),
INDEX `FK_USERID_idx` (`userID` ASC),
CONSTRAINT `FK_USERID`
FOREIGN KEY (`userID`)
REFERENCES `mydb`.`User` (`idUser`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Vote`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Vote` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Vote` (
`idVote` INT NOT NULL,
`voteFor` ENUM('TOPIC', 'USER', 'ISSUE') NULL,
`topicID` INT NULL,
`userID` INT NULL,
`issueID` INT NULL,
PRIMARY KEY (`idVote`),
INDEX `FK_USER_VOTED_FOR_idx` (`userID` ASC),
INDEX `FK_ISSUE_VOTED_FOR_idx` (`issueID` ASC),
INDEX `FK_FORUM_VOTED_FOR_idx` (`topicID` ASC),
CONSTRAINT `FK_USER_VOTED_FOR`
FOREIGN KEY (`userID`)
REFERENCES `mydb`.`User` (`idUser`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `FK_ISSUE_VOTED_FOR`
FOREIGN KEY (`issueID`)
REFERENCES `mydb`.`Issue` (`idIssue`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `FK_FORUM_VOTED_FOR`
FOREIGN KEY (`topicID`)
REFERENCES `mydb`.`Forum` (`idForum`)
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 already have a Constraint with the name FK_USERID on table mydb.issue. Give the constraint on mydb.Forum another name. e.g.
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
-- -----------------------------------------------------
DROP SCHEMA IF EXISTS `mydb` ;
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`User`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`User` ;
CREATE TABLE IF NOT EXISTS `mydb`.`User` (
`idUser` INT NOT NULL,
`first_name` VARCHAR(45) NULL,
`last_name` VARCHAR(45) NULL,
`username` VARCHAR(45) NULL,
`password` VARCHAR(45) NULL,
`company_pin` VARCHAR(45) NULL,
`student_pin` VARCHAR(45) NULL,
`isAdmin` TINYINT(1) NULL DEFAULT 0,
`isCandidate` TINYINT(1) NULL,
`isVoter` TINYINT(1) NULL,
`votes_left` INT NULL,
`votes_achieved` INT NULL,
PRIMARY KEY (`idUser`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Issue`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Issue` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Issue` (
`idIssue` INT NOT NULL,
`issueTitle` VARCHAR(45) NULL,
`issueDescription` VARCHAR(255) NULL,
`userID` INT NULL,
`isResolved` TINYINT(1) NULL DEFAULT 0,
`upVotes` INT NULL,
PRIMARY KEY (`idIssue`),
INDEX `FK_USERID_idx` (`userID` ASC),
CONSTRAINT `FK_USERID`
FOREIGN KEY (`userID`)
REFERENCES `mydb`.`User` (`idUser`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Forum`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Forum` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Forum` (
`idForum` INT NOT NULL,
`userID` INT NULL,
`postTitle` VARCHAR(45) NULL,
`postText` VARCHAR(255) NULL,
`upVotes` INT NULL,
PRIMARY KEY (`idForum`),
INDEX `FK_USERID_idx` (`userID` ASC),
CONSTRAINT `FK_Forum_USERID`
FOREIGN KEY (`userID`)
REFERENCES `mydb`.`User` (`idUser`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Vote`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Vote` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Vote` (
`idVote` INT NOT NULL,
`voteFor` ENUM('TOPIC', 'USER', 'ISSUE') NULL,
`topicID` INT NULL,
`userID` INT NULL,
`issueID` INT NULL,
PRIMARY KEY (`idVote`),
INDEX `FK_USER_VOTED_FOR_idx` (`userID` ASC),
INDEX `FK_ISSUE_VOTED_FOR_idx` (`issueID` ASC),
INDEX `FK_FORUM_VOTED_FOR_idx` (`topicID` ASC),
CONSTRAINT `FK_USER_VOTED_FOR`
FOREIGN KEY (`userID`)
REFERENCES `mydb`.`User` (`idUser`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `FK_ISSUE_VOTED_FOR`
FOREIGN KEY (`issueID`)
REFERENCES `mydb`.`Issue` (`idIssue`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `FK_FORUM_VOTED_FOR`
FOREIGN KEY (`topicID`)
REFERENCES `mydb`.`Forum` (`idForum`)
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 got this error at a time of restoring of database & I found below details :
This issue comes into picture when your table/database names are not same like small/capital letters used in same table/database name on both side (needs to maintain same name on both side as it's case_sensitive).
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.
I have this example bellow where customers_b cannot be created.
Error Code 1005 / errno: 121.
However, if I create customers_b first than customers_a, then customers_a is the one which won't be created.
What is wrong? Why I cant link more than one FK to the PK 'id_state'?
Thanks!
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';
DROP SCHEMA IF EXISTS `testdb` ;
CREATE SCHEMA IF NOT EXISTS `testdb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `testdb` ;
-- -----------------------------------------------------
-- Table `testdb`.`state`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `testdb`.`state` ;
CREATE TABLE IF NOT EXISTS `testdb`.`state` (
`id_state` INT NOT NULL,
`abbr` CHAR(2) NOT NULL,
PRIMARY KEY (`id_state`),
UNIQUE INDEX `id_state_UNIQUE` (`id_state` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `testdb`.`customers_a`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `testdb`.`customers_a` ;
CREATE TABLE IF NOT EXISTS `testdb`.`customers_a` (
`id_customer` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`addr_state` INT NOT NULL,
PRIMARY KEY (`id_customer`),
CONSTRAINT `fk_state`
FOREIGN KEY (`addr_state`)
REFERENCES `testdb`.`state` (`id_state`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `testdb`.`customers_b`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `testdb`.`customers_b` ;
CREATE TABLE IF NOT EXISTS `testdb`.`customers_b` (
`id_customer` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`addr_state` INT NOT NULL,
PRIMARY KEY (`id_customer`),
CONSTRAINT `fk_state`
FOREIGN KEY (`addr_state`)
REFERENCES `testdb`.`state` (`id_state`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
You can't use the same name fk_state for both constraints. Give one of them a different name.
CREATE TABLE IF NOT EXISTS `customers_b` (
`id_customer` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`addr_state` INT NOT NULL,
PRIMARY KEY (`id_customer`),
CONSTRAINT `fk_state_b`
FOREIGN KEY (`addr_state`)
REFERENCES `state` (`id_state`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Your both tables constraint name is same CONSTRAINT fk_state. for customer_b change that name like below
CREATE TABLE IF NOT EXISTS `customers_a` (
`id_customer` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`addr_state` INT NOT NULL,
PRIMARY KEY (`id_customer`),
CONSTRAINT `fk_state`
FOREIGN KEY (`addr_state`)
REFERENCES `state` (`id_state`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `customers_b` (
`id_customer` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NOT NULL,
`addr_state` INT NOT NULL,
PRIMARY KEY (`id_customer`),
CONSTRAINT `fk_state1` <-- Here
FOREIGN KEY (`addr_state`)
REFERENCES `state` (`id_state`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
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