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
Related
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
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.
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 ...
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 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;