SQL: insert into 3 tables with a foreign key - mysql

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.

Related

How to excute SQL Queries from '.sql' file into MySQL database using NodeJS

Stack and Enviroment
I am using NodeJS for back-end server and MySQL for storing the database.
I am using MySQL Workbench to design the database model.
with MySQL Workbench I can generate a '.sql' script that contains all the queries of my database model.
How to execute the generated '.sql' script into MySQL database using NodeJS server ?
I have already tried using the SQLite package following this online tutorial Running SQL Queries from a ‘.sql’ file in NodeJS (SQLite)
, but unfortunately no luck and it keeps throwing errors.
Would really appreciate any suggestion, thanks for your help :)
generated-script.sql
-- MySQL Workbench Forward Engineering
-- MySQL Script generated by MySQL Workbench
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 db_test
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema db_test
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `db_test` DEFAULT CHARACTER SET utf8 ;
USE `db_test` ;
-- -----------------------------------------------------
-- Table `assessment`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `assessment` (
`id` INT NOT NULL AUTO_INCREMENT,
`patient_id` INT NOT NULL,
`assessment_type` INT NOT NULL,
`timestamp` DATE NOT NULL,
PRIMARY KEY (`id`),
INDEX `assessment_patient_idx` (`patient_id` ASC) VISIBLE,
INDEX `assessment_type_idx` (`assessment_type` ASC) VISIBLE,
CONSTRAINT `assessment_patient`
FOREIGN KEY (`patient_id`)
REFERENCES `patient` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `assessment_type`
FOREIGN KEY (`assessment_type`)
REFERENCES `assessment_type` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `assessment_type`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `assessment_type` (
`id` INT NOT NULL,
`label` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `images`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `images` (
`id` INT NOT NULL,
`url` VARCHAR(250) NOT NULL,
`assessment_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `assessment_image_idx` (`assessment_id` ASC) VISIBLE,
CONSTRAINT `assessment_image`
FOREIGN KEY (`assessment_id`)
REFERENCES `assessment` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `patient`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `patient` (
`id` INT NOT NULL,
`username` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `username_UNIQUE` (`username` ASC) VISIBLE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `user` (
`id` INT NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `user_notes`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `user_notes` (
`id` INT NOT NULL,
`user_id` INT NOT NULL,
`assessment_id` INT NOT NULL,
`content` VARCHAR(600) NOT NULL,
PRIMARY KEY (`id`),
INDEX `user_note_assessment_idx` (`assessment_id` ASC) VISIBLE,
INDEX `user_note_user_idx` (`user_id` ASC) VISIBLE,
CONSTRAINT `user_note_user`
FOREIGN KEY (`user_id`)
REFERENCES `assessment_type` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `user_note_assessment`
FOREIGN KEY (`assessment_id`)
REFERENCES `assessment` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `users_patients`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `users_patients` (
`id` INT NOT NULL,
`patient_id` INT NOT NULL,
`user_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `users_patients_patient_idx` (`patient_id` ASC) VISIBLE,
INDEX `users_patients_user_idx` (`user_id` ASC) VISIBLE,
CONSTRAINT `users_patients_patient`
FOREIGN KEY (`patient_id`)
REFERENCES `patient` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `users_patients_user`
FOREIGN KEY (`user_id`)
REFERENCES `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;
What i would suggest is : since you have multiple table creation scripts, best would be if you separate every table creation script into files, in this way you have full modularity over the things you want to achieve. However ...
You can read from a file and execute a query aginst it:
const generatedScript = fs.readFileSync(path.join(__dirname, '../generated-script.sql')).toString();
const query = await db.query(generatedScript, (err, result) => {
if (err){
throw err;
}else{
res.send("Query run successfully");
}
The db config file could look something like this:
const db = mysql.createConnection({
host:"-your host here -",
user:"-your user here-",
password:"-your password here-",
database:"-your database here-"
});

EER diagram forward engineering error: mysql ERROR: Error 1215: Cannot add foreign key constraint

I am using the MySQL workbench to create a database structure using the EER diagram. When it was complete I used the forward engineering function to create a sql file of my model:EER Diagram
When I try to deploy this query on MySQL server it keeps giving me mysql ERROR: Error 1215: Cannot add foreign key constraint.
Can somebody help me?
-- MySQL Workbench Forward Engineering
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`TblPersoon`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`TblPersoon` ;
CREATE TABLE IF NOT EXISTS `mydb`.`TblPersoon` (
`PersoonID` INT NOT NULL,
`GSM` VARCHAR(45) NULL,
`Functie` VARCHAR(45) NULL,
`Voornaam` VARCHAR(45) NULL,
`Achternaam` VARCHAR(45) NULL,
PRIMARY KEY (`PersoonID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`TblDevice`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`TblDevice` ;
CREATE TABLE IF NOT EXISTS `mydb`.`TblDevice` (
`DeviceID` INT NOT NULL,
`Van` VARCHAR(45) NULL,
`Tot` VARCHAR(45) NULL,
PRIMARY KEY (`DeviceID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`TblEvents`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`TblEvents` ;
CREATE TABLE IF NOT EXISTS `mydb`.`TblEvents` (
`EventID` INT NOT NULL AUTO_INCREMENT,
`Naam` VARCHAR(45) NULL,
`Locatie` VARCHAR(45) NULL,
`ContactpersoonID` INT NOT NULL,
`VerantwoordelijkeID` INT NOT NULL,
`EventNaam` VARCHAR(45) NULL,
PRIMARY KEY (`EventID`),
INDEX `PersoonID_idx` (`ContactpersoonID` ASC),
INDEX `PersoonID_idx1` (`VerantwoordelijkeID` ASC),
CONSTRAINT `PersoonID`
FOREIGN KEY (`ContactpersoonID`)
REFERENCES `mydb`.`TblPersoon` (`PersoonID`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `PersoonID`
FOREIGN KEY (`VerantwoordelijkeID`)
REFERENCES `mydb`.`TblPersoon` (`PersoonID`)
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`TblContainer`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`TblContainer` ;
CREATE TABLE IF NOT EXISTS `mydb`.`TblContainer` (
`ContainerID` INT NULL,
`Plaats` INT NULL,
PRIMARY KEY (`ContainerID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`TblInstallatie`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`TblInstallatie` ;
CREATE TABLE IF NOT EXISTS `mydb`.`TblInstallatie` (
`InstallatieID` INT NULL,
`ContainerID` INT NULL,
`DeviceID` INT NULL,
`Van` VARCHAR(45) NULL,
`Tot` VARCHAR(45) NULL,
`EventID` INT NULL,
`Omschrijving` VARCHAR(45) NULL,
`VerantwoordelijkeID` INT NULL,
PRIMARY KEY (`InstallatieID`),
INDEX `PersoonID_idx` (`VerantwoordelijkeID` ASC),
INDEX `DeviceID_idx` (`DeviceID` ASC),
INDEX `EventID_idx` (`EventID` ASC),
INDEX `ContainerID_idx` (`ContainerID` ASC),
CONSTRAINT `PersoonID`
FOREIGN KEY (`VerantwoordelijkeID`)
REFERENCES `mydb`.`TblPersoon` (`PersoonID`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `DeviceID`
FOREIGN KEY (`DeviceID`)
REFERENCES `mydb`.`TblDevice` (`DeviceID`)
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `EventID`
FOREIGN KEY (`EventID`)
REFERENCES `mydb`.`TblEvents` (`EventID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `ContainerID`
FOREIGN KEY (`ContainerID`)
REFERENCES `mydb`.`TblContainer` (`ContainerID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`TblLog`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`TblLog` ;
CREATE TABLE IF NOT EXISTS `mydb`.`TblLog` (
`LogID` INT NOT NULL,
`InstallatieID` INT NULL,
PRIMARY KEY (`LogID`),
INDEX `InstallatieID_idx` (`InstallatieID` ASC),
CONSTRAINT `InstallatieID`
FOREIGN KEY (`InstallatieID`)
REFERENCES `mydb`.`TblInstallatie` (`InstallatieID`)
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 noticed both foreign key constraints in TblEvents have the same name, PersoonID. Constraint names must be unique, and in fact they must be unique within the whole schema, not only within the table in which they are defined.
Also TblInstallatie has its own constraint named PersoonID. So if you just change one of the constraint names in TblEvents, it still results in an error when you get to CREATE TABLE TblInstallatie.
I tested this with MySQL 5.6 and I got it to work by giving distinct names to both constraints in TblEvents.
CREATE TABLE IF NOT EXISTS `mydb`.`TblEvents` (
...
CONSTRAINT `PersoonID1`
FOREIGN KEY (`ContactpersoonID`)
...
CONSTRAINT `PersoonID2`
FOREIGN KEY (`VerantwoordelijkeID`)
...
The later creation of TblInstallatie's CONSTRAINT PersoonID can then succeed.

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.

SQL Schema Level Constraint

I've got a constraint between some tables I'm trying to implement, I imagine I should be doing it on the db level, but I'm struggling to come up with the right terms to google, maybe you guys can help!
If I have the tables:
Product [coke, banana, chocolate etc]
ProductType [food, drink, cosmetics etc]
Location [fridge, shelf, cupboard etc]
If -> is many-to-one, and >-< is many-to-many
Product -> ProductType
Product -> Location
ProductType >-< Location
Given a fridge, we know a fridge can contain [food,drink], so on the application level, we present the user with only the food and drink products to allocate to fridge. Is there a way on the db level to ensure that fridge only contains products from its permitted types?
Below is the SQL code I came up with for the above, in the last part I insert 'coke', with ProductType 'drink'. How to I make sure it therefore can't be put in a 'cupboard'
CREATE SCHEMA IF NOT EXISTS `test` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `test` ;
-- -----------------------------------------------------
-- Table `test`.`Location`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test`.`Location` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test`.`ProductType`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test`.`ProductType` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test`.`Product`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test`.`Product` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NULL,
`Location_id` INT NULL,
`ProductType_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_Product_Location_idx` (`Location_id` ASC),
INDEX `fk_Product_ProductType1_idx` (`ProductType_id` ASC),
CONSTRAINT `fk_Product_Location`
FOREIGN KEY (`Location_id`)
REFERENCES `test`.`Location` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Product_ProductType1`
FOREIGN KEY (`ProductType_id`)
REFERENCES `test`.`ProductType` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `test`.`ProductType_has_Location`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `test`.`ProductType_has_Location` (
`ProductType_id` INT NOT NULL,
`Location_id` INT NOT NULL,
PRIMARY KEY (`ProductType_id`, `Location_id`),
INDEX `fk_ProductType_has_Location_Location1_idx` (`Location_id` ASC),
INDEX `fk_ProductType_has_Location_ProductType1_idx` (`ProductType_id` ASC),
CONSTRAINT `fk_ProductType_has_Location_ProductType1`
FOREIGN KEY (`ProductType_id`)
REFERENCES `test`.`ProductType` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_ProductType_has_Location_Location1`
FOREIGN KEY (`Location_id`)
REFERENCES `test`.`Location` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
INSERT INTO `test`.`Location`
(`name`)
VALUES
('fridge'),('shelf'),('cupboard');
INSERT INTO `test`.`ProductType`
(`name`)
VALUES
('food'),('drink'),('cosmetics');
INSERT INTO `test`.`ProductType_has_Location`
(`ProductType_id`,`Location_id`)
VALUES
(1,1),(2,1),(2,2),(3,3);
INSERT INTO `test`.`Product`
(`name`,`ProductType_id`,`Location_id`)
VALUES
('coke',2,NULL);
Another constraint in Product:
CONSTRAINT `fk_Product_Location_XRef`
FOREIGN KEY (`ProductType_id`,`Location_id`)
REFERENCES `test`.`ProductType_has_Location` (`ProductType_id`,`Location_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
This then checks that the Type and Location agree with what's in the ProductType_has_Location table.

#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