Mysql forward engineered sql duplicate foreign key constraints - mysql

i don't know much SQLs, i generated this using modelling via MySQL Workbench. I always get a Error Code: 1826. Duplicate foreign key constraint name 'post_id' when i try to import the SQL generated via Forward Engineering of workbench. If anybody could me out. I am not coding this from scratch. I just made a ERD and generated an SQL.
Additional info, the error fires in this line
CREATE TABLE IF NOT EXISTS testing.post_likes ...
Also, MySQL Workbench and Server are both version 8.0.19
Here is the generated SQL
-- MySQL Script generated by MySQL Workbench
-- Tue Mar 31 21:51:19 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 testing
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema testing
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `testing` DEFAULT CHARACTER SET utf8 ;
USE `testing` ;
-- -----------------------------------------------------
-- Table `testing`.`users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `testing`.`users` (
`userid` DOUBLE NOT NULL,
`username` VARCHAR(100) NULL,
`email` VARCHAR(100) NULL,
`name` VARCHAR(100) NULL,
`handle` VARCHAR(45) NULL,
`quote` LONGTEXT NULL,
`last_updated` DATE NULL,
PRIMARY KEY (`userid`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `testing`.`posts`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `testing`.`posts` (
`post_id` DOUBLE NOT NULL,
`user_id` DOUBLE NOT NULL,
`description` LONGTEXT NULL,
`images` LONGTEXT NULL,
`tags` LONGTEXT NULL,
`last_updated` DATE NULL,
PRIMARY KEY (`post_id`),
INDEX `user_id_idx` (`user_id` ASC) VISIBLE,
CONSTRAINT `user_id`
FOREIGN KEY (`user_id`)
REFERENCES `testing`.`users` (`userid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `testing`.`comments`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `testing`.`comments` (
`comment_id` DOUBLE NOT NULL,
`post_id` DOUBLE NOT NULL,
`description` LONGTEXT NULL,
`images` LONGTEXT NULL,
`last_updated` DATE NULL,
PRIMARY KEY (`comment_id`),
INDEX `post_id_idx` (`post_id` ASC) VISIBLE,
CONSTRAINT `post_id`
FOREIGN KEY (`post_id`)
REFERENCES `testing`.`posts` (`post_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `testing`.`post_likes`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `testing`.`post_likes` (
`plike_id` DOUBLE NOT NULL,
`post_id` DOUBLE NOT NULL,
`user_id` DOUBLE NOT NULL,
`type` INT NULL,
PRIMARY KEY (`plike_id`),
INDEX `post_id_idx` (`post_id` ASC) VISIBLE,
INDEX `user_id_idx` (`user_id` ASC) VISIBLE,
CONSTRAINT `post_id`
FOREIGN KEY (`post_id`)
REFERENCES `testing`.`posts` (`post_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `user_id`
FOREIGN KEY (`user_id`)
REFERENCES `testing`.`users` (`userid`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `testing`.`comment_likes`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `testing`.`comment_likes` (
`clike_id` DOUBLE NOT NULL,
`comment_id` DOUBLE NOT NULL,
`user_id` DOUBLE NOT NULL,
`type` INT NULL,
PRIMARY KEY (`clike_id`),
INDEX `comment_id_idx` (`comment_id` ASC) VISIBLE,
INDEX `user_id_idx` (`user_id` ASC) VISIBLE,
CONSTRAINT `comment_id`
FOREIGN KEY (`comment_id`)
REFERENCES `testing`.`comments` (`comment_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `user_id`
FOREIGN KEY (`user_id`)
REFERENCES `testing`.`users` (`userid`)
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;

As per discussion with #P.Salmon, it was indicated that the FK name was duplicated. So i adjusted the FK name via the ERD ang generated a new SQL file. So basically, i adjusted avoid using the same FK name and voila, it works.
See below

Related

SQL Script can't create some tables

My SQL script firstly says that it is not connected, when i open it, so i connect it by going to Query -> Reconnect to server, which works. Then i try to execute the SQL script which only creates the first two tables are met with an error code 1064, and the errors are at the VISIBLE Index lines. I have no idea what that means as i am completely new to SQL, can anyone help?
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' INDEX userId_idx (userId ASC) VISIBLE, CONSTRAINT gameId FOREIGN ' at line 9
-- MySQL Script generated by MySQL Workbench
-- Wed Jan 2 02:21:44 2019
-- 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 game_review
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema game_review
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `game_review` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ;
USE `game_review` ;
-- -----------------------------------------------------
-- Table `game_review`.`games`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `game_review`.`games` (
`game_Id` INT NOT NULL AUTO_INCREMENT,
`title` VARCHAR(255) NOT NULL,
`tags` VARCHAR(60) NOT NULL,
`systemReq` VARCHAR(255) NOT NULL,
`developer` VARCHAR(45) NOT NULL,
`publisher` VARCHAR(45) NOT NULL,
`trailer` VARCHAR(45) NULL,
`about` VARCHAR(255) NULL,
`platform` VARCHAR(45) NOT NULL,
`categories` VARCHAR(45) NOT NULL,
`description` VARCHAR(45) NOT NULL,
`releaseDate` DATETIME NOT NULL,
PRIMARY KEY (`game_Id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `game_review`.`users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `game_review`.`users` (
`user_Id` INT NOT NULL AUTO_INCREMENT,
`username` VARCHAR(45) NOT NULL,
`password` VARCHAR(45) NOT NULL,
`email` VARCHAR(45) NOT NULL,
PRIMARY KEY (`user_Id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `game_review`.`reviews`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `game_review`.`reviews` (
`review_Id` INT NOT NULL AUTO_INCREMENT,
`gameId` INT NOT NULL,
`userId` INT NOT NULL,
`review` TEXT(1024) NOT NULL,
`rating` INT NULL,
`dateposted` DATETIME NULL,
PRIMARY KEY (`review_Id`),
INDEX `gameId_idx` (`gameId` ASC) VISIBLE,
INDEX `userId_idx` (`userId` ASC) VISIBLE,
CONSTRAINT `gameId`
FOREIGN KEY (`gameId`)
REFERENCES `game_review`.`games` (`game_Id`)
ON DELETE CASCADE
ON UPDATE NO ACTION,
CONSTRAINT `userId`
FOREIGN KEY (`userId`)
REFERENCES `game_review`.`users` (`user_Id`)
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `game_review`.`favourites`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `game_review`.`favourites` (
`favourites_Id` INT NOT NULL AUTO_INCREMENT,
`gameId` INT NOT NULL,
`userId` INT NOT NULL,
PRIMARY KEY (`favourites_Id`),
INDEX `gameId_idx` (`gameId` ASC) VISIBLE,
INDEX `userId_idx` (`userId` ASC) VISIBLE,
CONSTRAINT `gameId`
FOREIGN KEY (`gameId`)
REFERENCES `game_review`.`games` (`game_Id`)
ON DELETE CASCADE
ON UPDATE NO ACTION,
CONSTRAINT `userId`
FOREIGN KEY (`userId`)
REFERENCES `game_review`.`users` (`user_Id`)
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `game_review`.`forum`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `game_review`.`forum` (
`forum_Id` INT NOT NULL AUTO_INCREMENT,
`gameId` INT NOT NULL,
`userId` INT NOT NULL,
`topic` VARCHAR(45) NOT NULL,
`views` INT NOT NULL,
`likes` INT NOT NULL,
`comment` TEXT(300) NULL,
`totalComments` INT NULL,
`dateposted` DATETIME NOT NULL,
PRIMARY KEY (`forum_Id`),
INDEX `gameId_idx` (`gameId` ASC) VISIBLE,
INDEX `userId_idx` (`userId` ASC) VISIBLE,
CONSTRAINT `gameId`
FOREIGN KEY (`gameId`)
REFERENCES `game_review`.`games` (`game_Id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `userId`
FOREIGN KEY (`userId`)
REFERENCES `game_review`.`users` (`user_Id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `game_review`.`game cart`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `game_review`.`game cart` (
`cart_Id` INT NOT NULL AUTO_INCREMENT,
`gameId` INT NOT NULL,
`quantity` INT NULL,
PRIMARY KEY (`cart_Id`),
INDEX `gameId_idx` (`gameId` ASC) VISIBLE,
CONSTRAINT `gameId`
FOREIGN KEY (`gameId`)
REFERENCES `game_review`.`games` (`game_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;
Your declaration of indexes is not correct : VISIBLE is not recognized syntax. Also, the ASC mention is superfluous as it corrresponds to the default.
Replace :
...
INDEX `gameId_idx` (`gameId` ASC) VISIBLE,
INDEX `userId_idx` (`userId` ASC) VISIBLE,
...
With :
...
INDEX `reviews_gameId_idx` (`gameId`),
INDEX `reviews_userId_idx` (`userId`),
...
For more information see the mysql CREATE INDEX docs.
Also please bear in mind that, in most RDBMS others than mysql, index names are global across the database schema, hence make sure not to use the same index name twice (I can see that at least index gameId_idx is declared twice. One solution is to prefix the index name with the table it belongs to, as shown above.
Foreign key names between different tables also must be unique (you have duplicates like game_Id).
Finally I also notice that one of your table name has a space in it (game cart) : this is not a good practice, should be avoided.

MySQL Workbench "errno 150 foreign key"

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 `mydb` DEFAULT CHARACTER SET utf8 COLLATE
utf8_general_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`Diploma`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Diploma` (
`diploma_id` VARCHAR(5) NOT NULL,
`diploma_name` VARCHAR(90) NULL,
PRIMARY KEY (`diploma_id`)
) ENGINE=InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`School`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`School` (
`school_id` INT(1) NOT NULL,
`school_name` VARCHAR(45) NULL,
PRIMARY KEY (`school_id`)
) ENGINE=InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Student`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Student` (
`student_number` INT(8) NOT NULL,
`student_id` INT(8) NULL,
`student_name` VARCHAR(45) NULL,
`student_password` VARCHAR(45) NULL,
`student_mobile` INT(8) NULL,
`student_email` VARCHAR(45) NULL,
`diploma_id` VARCHAR(5) NOT NULL,
`school_id` INT(1) NOT NULL,
PRIMARY KEY (`student_number`),
INDEX `fk_Student_Diploma1_idx` (`diploma_id` ASC),
INDEX `fk_Student_School1_idx` (`school_id` ASC),
CONSTRAINT `fk_Student_Diploma1` FOREIGN KEY (`diploma_id`)
REFERENCES `mydb`.`Diploma` (`diploma_id`)
ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_Student_School1` FOREIGN KEY (`school_id`)
REFERENCES `mydb`.`School` (`school_id`)
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`OFN`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`OFN` (
`ofn_id` INT(8) NOT NULL,
`ofn_username` VARCHAR(45) NULL,
`ofn_password` VARCHAR(45) NULL,
`ofn_email` VARCHAR(45) NULL,
PRIMARY KEY (`ofn_id`)
) ENGINE=InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Appointment`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Appointment` (
`appointment_id` INT(8) NOT NULL,
`appointment_date` DATE NULL,
`appointment_time` TIME NULL,
`ofn_id` INT(8) NOT NULL,
PRIMARY KEY (`appointment_id`),
INDEX `fk_Appointment_OFN1_idx` (`ofn_id` ASC),
CONSTRAINT `fk_Appointment_OFN1` FOREIGN KEY (`ofn_id`)
REFERENCES `mydb`.`OFN` (`ofn_id`)
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Booking`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Booking` (
`student_number` INT(8) NOT NULL,
`appointment_id` INT(8) NOT NULL,
`booking_date` DATE NULL,
`booking_time` TIME NULL,
PRIMARY KEY (`student_number` , `appointment_id`),
INDEX `fk_Student_has_Appointment_Appointment1_idx` (`appointment_id` ASC),
INDEX `fk_Student_has_Appointment_Student1_idx` (`student_number` ASC),
CONSTRAINT `fk_Student_has_Appointment_Student1` FOREIGN KEY
(`student_number`)
REFERENCES `mydb`.`Student` (`student_number`)
ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_Student_has_Appointment_Appointment1` FOREIGN KEY (`appointment_id`)
REFERENCES `mydb`.`Appointment` (`appointment_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;
This is the code i was building but it gave an error
Error Code: 1005: Can't create table 'mydb'.'booking'
I tried looking through some of the previous question by people encountering the same problem but i am still stuck at this point.
Any help will be greatly appreciated, thank you.
Somehow it's the foreign key student_number.
I am thinking in these possibilities:
The foreign key name is a duplicate of an already existing key. Check that the name of your foreign key is unique within your database. Just add a few random characters to the end of your key name to test for this.
One or both of your tables is a MyISAM table. In order to use foreign keys, the tables must both be InnoDB. (Actually, if both tables are MyISAM then you won’t get an error message - it just won’t create the key.) In Query Browser, you can specify the table type.
One of the key field that you are trying to reference does not have an index and/or is not a primary key. If one of the fields in the relationship is not a primary key, you must create an index for that field.
Also check:
SET FOREIGN_KEY_CHECKS=0;

MySQL Server Error 1005

When I try to run SQL script I get the log error in Workbench Forward Engineer:
Executing SQL script in server
ERROR: Error 1005: Can't create table 'regional_budget.budget' (errno: 121)
SQL Code:
-- -----------------------------------------------------
-- Table `regional_budget`.`budget`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `regional_budget`.`budget` (
`id` INT(3) NOT NULL AUTO_INCREMENT,
`region_id` INT(3) NOT NULL,
`balance` DECIMAL(11,2) NULL,
`income_id` INT(3) NOT NULL,
`income` DECIMAL(11,2) NULL,
`outcome_id` INT(3) NOT NULL,
`outcome` DECIMAL(11,2) NULL,
PRIMARY KEY (`id`),
INDEX `region_id_idx` (`region_id` ASC),
INDEX `outcome_id_idx` (`outcome_id` ASC),
INDEX `income_id_idx` (`income_id` ASC),
CONSTRAINT `region_id`
FOREIGN KEY (`region_id`)
REFERENCES `regional_budget`.`regions` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `income_id`
FOREIGN KEY (`income_id`)
REFERENCES `regional_budget`.`income` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `outcome_id`
FOREIGN KEY (`outcome_id`)
REFERENCES `regional_budget`.`outcome` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 13 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
SQL script:
-- 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 regional_budget
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema regional_budget
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `regional_budget` DEFAULT CHARACTER SET utf8 ;
USE `regional_budget` ;
-- -----------------------------------------------------
-- Table `regional_budget`.`regions`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `regional_budget`.`regions` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(20) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `regional_budget`.`subjects`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `regional_budget`.`subjects` (
`id` INT NOT NULL,
`name` VARCHAR(20) NULL,
`region_id` INT NULL,
PRIMARY KEY (`id`),
INDEX `region_id_idx` (`region_id` ASC),
CONSTRAINT `region_id`
FOREIGN KEY (`region_id`)
REFERENCES `regional_budget`.`regions` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `regional_budget`.`income`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `regional_budget`.`income` (
`id` INT NOT NULL AUTO_INCREMENT,
`tax_and_non_tax` DECIMAL(11,2) NULL,
`receipts` DECIMAL(11,2) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `regional_budget`.`outcome`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `regional_budget`.`outcome` (
`id` INT NOT NULL AUTO_INCREMENT,
`transfers` DECIMAL(11,2) NULL,
`subventions` DECIMAL(11,2) NULL,
`subsidies` DECIMAL(11,2) NULL,
`dotations` DECIMAL(11,2) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `regional_budget`.`population`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `regional_budget`.`population` (
`subject_id` INT NOT NULL,
`count` DECIMAL(11,2) NULL,
PRIMARY KEY (`subject_id`),
CONSTRAINT `subject_id`
FOREIGN KEY (`subject_id`)
REFERENCES `regional_budget`.`subjects` (`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;
-- -----------------------------------------------------
-- Table `regional_budget`.`budget`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `regional_budget`.`budget` (
`id` INT(3) NOT NULL AUTO_INCREMENT,
`region_id` INT(3) NOT NULL,
`balance` DECIMAL(11,2) NULL,
`income_id` INT(3) NOT NULL,
`income` DECIMAL(11,2) NULL,
`outcome_id` INT(3) NOT NULL,
`outcome` DECIMAL(11,2) NULL,
PRIMARY KEY (`id`),
INDEX `region_id_idx` (`region_id` ASC),
INDEX `outcome_id_idx` (`outcome_id` ASC),
INDEX `income_id_idx` (`income_id` ASC),
CONSTRAINT `region_id`
FOREIGN KEY (`region_id`)
REFERENCES `regional_budget`.`regions` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `income_id`
FOREIGN KEY (`income_id`)
REFERENCES `regional_budget`.`income` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `outcome_id`
FOREIGN KEY (`outcome_id`)
REFERENCES `regional_budget`.`outcome` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
P.S. I tried to create 'regional_budget'.'budget' with foreign keys after other reference tables. Also I tried to use foreign_key_checks, but nothing helps.
I suggest you try changing the identifiers of the foreign key constraints to be unique, and not conflict with identifiers already used for columns (or other constraints.
For example:
CONSTRAINT `FK_budget_regions` FOREIGN KEY ...
CONSTRAINT `FK_budget_income` FOREIGN KEY ...
CONSTRAINT `FK_budget_outcome` FOREIGN KEY ...

Error #1215 - Cannot add foreign key constraint

I have generated a sql script with MySql Workbench.
When I try to create my database I have "Error 1215 1215 - Cannot add foreign key constraint".
The problem seems to be with my last foreign key in the last create table :
CONSTRAINT `fk_tb_animation_option_col_debut`
FOREIGN KEY (`DT_ANO_DEBUT`)
REFERENCES `brest2016`.`tb_animation_option` (`DT_ANO_DEBUT`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
This is my entire script:
-- MySQL Script generated by MySQL Workbench
-- 01/16/16 00:46:22
-- 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='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Schema brest2016
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `brest2016` DEFAULT CHARACTER SET utf8 ;
USE `brest2016` ;
-- -----------------------------------------------------
-- Table `brest2016`.`tb_visiteur`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brest2016`.`tb_visiteur` (
`D_VI_CODE` INT(3) NOT NULL AUTO_INCREMENT,
`V_VI_NOM` VARCHAR(45) NOT NULL,
`V_VI_PRENOM` VARCHAR(45) NOT NULL,
`V_VI_EMAIL` VARCHAR(45) NOT NULL,
`V_VI_TELEPHONE` VARCHAR(45) NULL,
`DT_VI_DEBUT` DATETIME NOT NULL,
`DT_VI_FIN` DATETIME NOT NULL,
PRIMARY KEY (`D_VI_CODE`),
UNIQUE INDEX `idVisiteur_UNIQUE` (`D_VI_CODE` ASC),
UNIQUE INDEX `email_UNIQUE` (`V_VI_EMAIL` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brest2016`.`tb_type_animation`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brest2016`.`tb_type_animation` (
`D_TY_CODE` INT(3) NOT NULL AUTO_INCREMENT,
`V_TY_LIBELLE` VARCHAR(45) NULL,
PRIMARY KEY (`D_TY_CODE`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brest2016`.`tb_animation`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brest2016`.`tb_animation` (
`D_AN_CODE` INT(3) NOT NULL AUTO_INCREMENT,
`V_AN_LIBELLE` VARCHAR(45) NOT NULL,
`V_AN_LIEU` VARCHAR(45) NOT NULL,
`V_AN_IMAGE` VARCHAR(45) NULL,
`V_AN_DESCRIPTIF` VARCHAR(45) NULL,
`D_AN_PRIORITE` VARCHAR(45) NOT NULL,
`D_TY_CODE` INT(3) NULL,
PRIMARY KEY (`D_AN_CODE`),
UNIQUE INDEX `idAnimation_UNIQUE` (`D_AN_CODE` ASC),
INDEX `fk_tb_type_animation_col_code_1_idx` (`D_TY_CODE` ASC),
CONSTRAINT `fk_tb_type_animation_col_code`
FOREIGN KEY (`D_TY_CODE`)
REFERENCES `brest2016`.`tb_type_animation` (`D_TY_CODE`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brest2016`.`tb_option`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brest2016`.`tb_option` (
`D_OP_CODE` INT(3) NOT NULL AUTO_INCREMENT,
`V_OP_LIBELLE` VARCHAR(45) NOT NULL,
PRIMARY KEY (`D_OP_CODE`),
UNIQUE INDEX `idOption_UNIQUE` (`D_OP_CODE` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brest2016`.`tb_animation_option`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brest2016`.`tb_animation_option` (
`D_AN_CODE` INT(3) NOT NULL,
`D_OP_CODE` INT(3) NOT NULL,
`DT_ANO_DEBUT` DATETIME NOT NULL,
`DT_ANO_FIN` DATETIME NOT NULL,
`D_ANO_PLACES_MAX` INT(3) NOT NULL,
`D_ANO_DUREE` INT(3) NOT NULL,
PRIMARY KEY (`D_AN_CODE`, `D_OP_CODE`, `DT_ANO_DEBUT`),
INDEX `fk_tb_option_idx` (`D_OP_CODE` ASC),
CONSTRAINT `fk_tb_animation_col_code`
FOREIGN KEY (`D_AN_CODE`)
REFERENCES `brest2016`.`tb_animation` (`D_AN_CODE`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tb_option_col_code`
FOREIGN KEY (`D_OP_CODE`)
REFERENCES `brest2016`.`tb_option` (`D_OP_CODE`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
COMMENT = 'association entre une animation et une option';
-- -----------------------------------------------------
-- Table `brest2016`.`tb_administrateur`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brest2016`.`tb_administrateur` (
`D_AD_CODE` INT(3) NOT NULL,
`V_AD_ID` VARCHAR(12) NOT NULL,
`V_AD_MDP` VARCHAR(12) NOT NULL,
PRIMARY KEY (`D_AD_CODE`),
UNIQUE INDEX `idAdministrateur_UNIQUE` (`D_AD_CODE` ASC),
UNIQUE INDEX `V_UT_LOGIN_UNIQUE` (`V_AD_ID` ASC))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `brest2016`.`tb_reservation`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `brest2016`.`tb_reservation` (
`D_VI_CODE` INT(3) NOT NULL,
`D_AN_CODE` INT(3) NOT NULL,
`D_OP_CODE` INT(3) NOT NULL,
`DT_ANO_DEBUT` DATETIME NOT NULL,
PRIMARY KEY (`D_VI_CODE`, `D_AN_CODE`, `D_OP_CODE`, `DT_ANO_DEBUT`),
INDEX `fk_tb_animation_option_col_code_animation_idx` (`D_AN_CODE` ASC),
INDEX `fk_tb_animation_option_col_code_option_idx` (`D_OP_CODE` ASC),
INDEX `fk_tb_animation_option_col_debut_idx` (`DT_ANO_DEBUT` ASC),
CONSTRAINT `fk_tb_visiteur_col_code`
FOREIGN KEY (`D_VI_CODE`)
REFERENCES `brest2016`.`tb_visiteur` (`D_VI_CODE`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tb_animation_option_col_code_animation`
FOREIGN KEY (`D_AN_CODE`)
REFERENCES `brest2016`.`tb_animation_option` (`D_AN_CODE`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tb_animation_option_col_code_option`
FOREIGN KEY (`D_OP_CODE`)
REFERENCES `brest2016`.`tb_animation_option` (`D_OP_CODE`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tb_animation_option_col_debut`
FOREIGN KEY (`DT_ANO_DEBUT`)
REFERENCES `brest2016`.`tb_animation_option` (`DT_ANO_DEBUT`)
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;
The column named in a foreign key must be indexed.
CREATE TABLE IF NOT EXISTS `brest2016`.`tb_animation_option` (
`D_AN_CODE` INT(3) NOT NULL,
`D_OP_CODE` INT(3) NOT NULL,
`DT_ANO_DEBUT` DATETIME NOT NULL,
`DT_ANO_FIN` DATETIME NOT NULL,
`D_ANO_PLACES_MAX` INT(3) NOT NULL,
`D_ANO_DUREE` INT(3) NOT NULL,
PRIMARY KEY (`D_AN_CODE`, `D_OP_CODE`, `DT_ANO_DEBUT`),
INDEX `fk_tb_option_idx` (`D_OP_CODE` ASC),
INDEX `dt_ano_debut_idx` (`T_ANO_DEBUT`), -- Add this line
CONSTRAINT `fk_tb_animation_col_code`
FOREIGN KEY (`D_AN_CODE`)
REFERENCES `brest2016`.`tb_animation` (`D_AN_CODE`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tb_option_col_code`
FOREIGN KEY (`D_OP_CODE`)
REFERENCES `brest2016`.`tb_option` (`D_OP_CODE`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
COMMENT = 'association entre une animation et une option';

#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