This is the table I get "Cannot add foreign key constraint" error.
-- -----------------------------------------------------
-- Table `mydb`.`Supervise1`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Supervise1` (
`S1_Date` VARCHAR(45) NOT NULL,
`S1_Contracter` VARCHAR(45) NOT NULL,
`S1_Contractee` VARCHAR(45) NOT NULL,
`S1_ID` VARCHAR(45) NOT NULL,
PRIMARY KEY (`S1_Contracter`, `S1_Contractee`, `S1_Date`, `S1_ID`),
INDEX `Contracter_idx` (`S1_Contracter` ASC),
INDEX `Contractee_idx` (`S1_Contractee` ASC),
INDEX `S1_ID_idx` (`S1_ID` ASC),
CONSTRAINT `S1_Date`
FOREIGN KEY (`S1_Date`)
REFERENCES `mydb`.`Contract` (`Date`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `S1_Contracter`
FOREIGN KEY (`S1_Contracter`)
REFERENCES `mydb`.`Contract` (`Contracter`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `S1_Contractee`
FOREIGN KEY (`S1_Contractee`)
REFERENCES `mydb`.`Contract` (`Contractee`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `S1_ID`
FOREIGN KEY (`S1_ID`)
REFERENCES `mydb`.`Lawfirm` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Contract and Lawfirm table
-- -----------------------------------------------------
-- Table `mydb`.`Contract`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Contract` (
`Date` VARCHAR(45) NOT NULL,
`Contracter` VARCHAR(45) NOT NULL,
`Contractee` VARCHAR(45) NOT NULL,
INDEX `Contracter_idx` (`Contracter` ASC),
INDEX `Contractee_idx` (`Contractee` ASC),
PRIMARY KEY (`Contracter`, `Contractee`, `Date`),
CONSTRAINT `Contracter`
FOREIGN KEY (`Contracter`)
REFERENCES `mydb`.`Agency` (`AgencyID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `Contractee`
FOREIGN KEY (`Contractee`)
REFERENCES `mydb`.`Agency` (`AgencyID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Lawfirm`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Lawfirm` (
`ID` VARCHAR(45) NOT NULL,
PRIMARY KEY (`ID`))
ENGINE = InnoDB;
I looked through some answers for similar problem but mine columns are same type. I don't understand why it gives me error. I can provide more information if needed.
Solved: Check the sql if there is any constraint with the same name. Even it says foreign key error was about constraint names.
Related
Hey so I'm on my first project with a complexe database :)
I have created all of my tables and I am now updating them to add the Foreign Keys (I had an issue so I decided to add Foreign Keys after creating all the tables).
Here the ERR Diagram that describe the project : click to open
The exact error i get : ER_FK_NO_INDEX_PARENT: Failed to add the foreign key constaint. Missing index for constraint 'fk_user_in_org__org_roles1' in the referenced table 'org_roles'
I've searched my error but i didn't found a solution to my problem here some of the best I found :
Link 1
Link 2
The tables concerned (simplified) and the update command :
------------------
-- Tables setup --
------------------
-- A role is unique for an org but differents orgs can have a role that have the same label
CREATE TABLE IF NOT EXISTS students.org_roles (
`org_id` INT NOT NULL,
`label` VARCHAR(32) NOT NULL,
PRIMARY KEY (`org_id`, `label`))
ENGINE = InnoDB;
-- The users are unique
CREATE TABLE IF NOT EXISTS students.users (
`id` INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`),
UNIQUE INDEX id_UNIQUE (`id` ASC));
-- The orgs are unique
CREATE TABLE IF NOT EXISTS students.organizations (
`id` INT NOT NULL AUTO_INCREMENT,
`name` TEXT(64) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX id_UNIQUE (`id` ASC))
ENGINE = InnoDB;
-- The table that match a user in an org with a specified role (= label in the org_role table)
CREATE TABLE IF NOT EXISTS students.user_in_org (
`user_id` INT NOT NULL,
`org_id` INT NOT NULL,
`role` TEXT(32) NOT NULL,
PRIMARY KEY (`user_id`, `org_id`),
UNIQUE INDEX `user_id_UNIQUE` (`user_id` ASC));
------------------------
-- The update command --
------------------------
ALTER TABLE students.user_in_org ADD
(CONSTRAINT fk_user_in_org__users1
FOREIGN KEY (`user_id`) REFERENCES students.users(`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT fk_user_in_org__organizations1
FOREIGN KEY (`org_id`) REFERENCES students.organizations(`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT fk_user_in_org__org_roles1
FOREIGN KEY (`role`) REFERENCES students.org_roles(`label`)
ON DELETE RESTRICT
ON UPDATE CASCADE);
Thank you in advance for your help :)
And please forgive me if my english isn't that fluent :/
This can be because the table students.org_roles doesn't have Unique index in the field org_id, it is what error says
For
CONSTRAINT fk_user_in_org__org_roles1
FOREIGN KEY (`role`) REFERENCES students.org_roles(`label`)
You need a index on the table org_roles for the column label
CREATE TABLE IF NOT EXISTS students.org_roles (
`org_id` INT NOT NULL,
`label` VARCHAR(32) NOT NULL,
KEy (`label`),
PRIMARY KEY (`org_id`, `label`))
ENGINE = InnoDB;
In your origi8nal design you have a combined primary key so you can only make a reference to the combined primary key or define a new one for label
CREATE TABLE IF NOT EXISTS org_roles (
`org_id` INT NOT NULL,
`label` VARCHAR(32) NOT NULL,
PRIMARY KEY (`org_id`, `label`))
ENGINE = InnoDB;
-- The users are unique
CREATE TABLE IF NOT EXISTS users (
`id` INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`),
UNIQUE INDEX id_UNIQUE (`id` ASC));
-- The orgs are unique
CREATE TABLE IF NOT EXISTS organizations (
`id` INT NOT NULL AUTO_INCREMENT,
`name` TEXT(64) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX id_UNIQUE (`id` ASC))
ENGINE = InnoDB;
-- The table that match a user in an org with a specified role (= label in the org_role table)
CREATE TABLE IF NOT EXISTS user_in_org (
`user_id` INT NOT NULL,
`org_id` INT NOT NULL,
`role` TEXT(32) NOT NULL,
PRIMARY KEY (`user_id`, `org_id`),
UNIQUE INDEX `user_id_UNIQUE` (`user_id` ASC));
ALTER TABLE user_in_org ADD
(CONSTRAINT fk_user_in_org__users1
FOREIGN KEY (`user_id`) REFERENCES users(`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT fk_user_in_org__organizations1
FOREIGN KEY (`org_id`) REFERENCES organizations(`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT fk_user_in_org__org_roles1
FOREIGN KEY (`org_id`,`role`) REFERENCES org_roles(`org_id`,`label`)
ON DELETE RESTRICT
ON UPDATE CASCADE);
db<>fiddle here
I have to create a double foreign key in the "Sceglie" table, but MySQL shows me always the same error: "#1215 - Cannot add foreign key constraint ". It only happens with "Cognome_candidato" and not with "Nome_candidato".I've also tried to write FOREIGN KEY (Nome_candidato, Cognome_candidato) REFERENCES Candidato(Nome_candidato, Cognome_candidato) ON DELETE CASCADE ON UPDATE CASCADE, but it didn't create the double foreign key. How can i solve it?
create table Candidato (
Nome_candidato varchar(255),
Cognome_candidato varchar(255),
PRIMARY KEY (Nome_candidato, Cognome_candidato));
create table Sceglie (
Email varchar(255),
Nome_candidato varchar(255),
Cognome_candidato varchar(255),
PRIMARY KEY (Email, Nome_candidato, Cognome_candidato),
FOREIGN KEY (Email) REFERENCES Utente(Email)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (Nome_candidato) REFERENCES Candidato(Nome_candidato)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (Cognome_candidato) REFERENCES Candidato(Cognome_candidato)
ON DELETE CASCADE
ON UPDATE CASCADE );
You need an index for cognome_candidato, before a foreign key can reference that column.
This should solve it.
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`Candidato`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Candidato` (
`Nome_candidato` VARCHAR(255) NOT NULL,
`Cognome_candidato` VARCHAR(255) NOT NULL,
PRIMARY KEY (`Nome_candidato`, `Cognome_candidato`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Utente`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Utente` (
`Email` VARCHAR(255) NOT NULL,
PRIMARY KEY (`Email`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Sceglie`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Sceglie` (
`Nome_candidato` VARCHAR(255) NOT NULL,
`Cognome_candidato` VARCHAR(255) NOT NULL,
`Email` VARCHAR(255) NOT NULL,
PRIMARY KEY (`Nome_candidato`, `Cognome_candidato`, `Email`),
INDEX `fk_Candidato_has_Utente_Utente1_idx` (`Email` ASC),
INDEX `fk_Candidato_has_Utente_Candidato_idx` (`Nome_candidato` ASC, `Cognome_candidato` ASC),
CONSTRAINT `fk_Candidato_has_Utente_Candidato`
FOREIGN KEY (`Nome_candidato` , `Cognome_candidato`)
REFERENCES `mydb`.`Candidato` (`Nome_candidato` , `Cognome_candidato`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `fk_Candidato_has_Utente_Utente1`
FOREIGN KEY (`Email`)
REFERENCES `mydb`.`Utente` (`Email`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
I Created a Whole Database in MySQL Workbench and now im trying to import it to my DB on my Webhost through phpMyAdmin. The Workbench created following code:
CREATE TABLE IF NOT EXISTS `usr_web375_4`.`Sandwich` (
`Sandwich_ID` INT NOT NULL,
`Style_ID` INT NOT NULL,
`Bread_ID` INT NOT NULL,
`Cheese_ID` INT NULL,
`Size_ID` INT NOT NULL,
`Sandwich_Toasted` TINYINT(1) NOT NULL,
`Sandwich_Prize` FLOAT NOT NULL,
PRIMARY KEY (`Sandwich_ID`),
INDEX `Style_ID_idx` (`Style_ID` ASC),
INDEX `Bread_ID_idx` (`Bread_ID` ASC),
INDEX `Cheese_ID_idx` (`Cheese_ID` ASC),
INDEX `Size_ID_idx` (`Size_ID` ASC),
CONSTRAINT `Style_ID`
FOREIGN KEY (`Style_ID`)
REFERENCES `usr_web375_4`.`Style` (`Style_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `Bread_ID`
FOREIGN KEY (`Bread_ID`)
REFERENCES `usr_web375_4`.`Bread` (`Bread_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `Cheese_ID`
FOREIGN KEY (`Cheese_ID`)
REFERENCES `usr_web375_4`.`Cheese` (`Cheese_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `Size_ID`
FOREIGN KEY (`Size_ID`)
REFERENCES `usr_web375_4`.`Size` (`Size_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `usr_web375_4`.`Sauce` (
`Sauce_ID` INT NOT NULL,
`Sauce_Name` VARCHAR(45) NULL,
`Sauce_IMG` VARCHAR(45) NULL,
`Sauce_Nutrition` INT NULL,
PRIMARY KEY (`Sauce_ID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `usr_web375_4`.`SandwichSauce`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `usr_web375_4`.`SandwichSauce` (
`SandwichSauce_ID` INT NOT NULL,
`Sandwich_ID` INT NOT NULL,
`Sauce_ID` INT NOT NULL,
PRIMARY KEY (`SandwichSauce_ID`),
INDEX `Sandwich_ID_idx` (`Sandwich_ID` ASC),
INDEX `Sauce_ID_idx` (`Sauce_ID` ASC),
CONSTRAINT `Sandwich_ID`
FOREIGN KEY (`Sandwich_ID`)
REFERENCES `usr_web375_4`.`Sandwich` (`Sandwich_ID`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `Sauce_ID`
FOREIGN KEY (`Sauce_ID`)
REFERENCES `usr_web375_4`.`Sauce` (`Sauce_ID`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
i tried it either with the import function and the code section.
It gives me following error:
CREATE TABLE IF NOT EXISTS `usr_web375_4`.`SandwichSauce` (
SandwichSauce_ID INT NOT NULL,
Sandwich_ID INT NOT NULL,
Sauce_ID INT NOT NULL,
PRIMARY KEY (SandwichSauce_ID),
INDEX Sandwich_ID_idx (Sandwich_ID ASC),
INDEX Sauce_ID_idx (Sauce_ID ASC),
CONSTRAINT Sandwich_ID
FOREIGN KEY (Sandwich_ID)
#1005 - Can't create table 'usr_web375_4.SandwichSauce' (errno: 121) (Details…)
I don't get it. there was no error in workbench
I'm not sure if it has maybe something to do with this table i successfully created:
CREATE TABLE IF NOT EXISTS `usr_web375_4`.`SandwichVegtable` (
`SandwichVegtable_ID` INT NOT NULL,
`Sandwich_ID` INT NOT NULL,
`Vegtable_ID` INT NOT NULL,
PRIMARY KEY (`SandwichVegtable_ID`),
INDEX `Sandwich_ID_idx` (`Sandwich_ID` ASC),
INDEX `Vegtable_ID_idx` (`Vegtable_ID` ASC),
CONSTRAINT `Sandwich_ID`
FOREIGN KEY (`Sandwich_ID`)
REFERENCES `usr_web375_4`.`Sandwich` (`Sandwich_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `Vegtable_ID`
FOREIGN KEY (`Vegtable_ID`)
REFERENCES `usr_web375_4`.`Vegtable` (`Vegtable_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
You must add a type for Sandwich_ID and Sauce_ID in your CREATE TABLE IF NOT EXISTSusr_web375_4.SandwichSauce ...
and you must use a unique names for all your indexes and constraints.
Could you try this code :
CREATE TABLE IF NOT EXISTS `usr_web375_4`.`SandwichSauce` (
`SandwichSauce_ID` INT NOT NULL,
`Sandwich_ID` INT NOT NULL,
`Sauce_ID` INT NOT NULL,
PRIMARY KEY (`SandwichSauce_ID`),
INDEX `Sandwich_ID_idx` (`Sandwich_ID` ASC),
INDEX `Sauce_ID_idx` (`Sauce_ID` ASC),
CONSTRAINT `Sandwich_ID_1`
FOREIGN KEY (`Sandwich_ID`)
REFERENCES `usr_web375_4`.`Sandwich` (`Sandwich_ID`)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `Sauce_ID`
FOREIGN KEY (`Sauce_ID`)
REFERENCES `usr_web375_4`.`Sauce` (`Sauce_ID`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB
Or in workbench you can run this query and export the DB again :
ALTER TABLE `SandwichSauce` DROP FOREIGN KEY `Sandwich_ID`;
ALTER TABLE `SandwichSauce` ADD CONSTRAINT `Sandwich_ID_1` FOREIGN KEY (`Sandwich_ID`) REFERENCES `usr_web375_4`.`Sandwich`(`Sandwich_ID`) ON DELETE CASCADE ON UPDATE CASCADE;
When I am creating a database, I'm getting a little bit of a difficulty with creating the "main" table for this database.
I thought there was a type difference between the foreign key and the primary key, but there wasn't.
So can anyone help me out? There must be something that I'm overlooking, but I can't find the problem.
[code]
-- -----------------------------------------------------
-- Table `boat`.`terms_and_conditions`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `boat`.`terms_and_conditions` (
`Terms_And_Conditions_Id` INT NOT NULL,
`Terms_And_Conditions_Doc` VARCHAR(255) NULL DEFAULT NULL,
`Terms_And_Conditions_Date` DATE NULL DEFAULT NULL,
PRIMARY KEY (`Terms_And_Conditions_Id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `boat`.`player`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `boat`.`player` (
`Player_Id` INT NOT NULL,
`Player_Firstname` VARCHAR(45) NULL DEFAULT NULL,
`Player_Lastname` VARCHAR(45) NULL DEFAULT NULL,
`Player_Password` VARCHAR(45) NULL,
`Player_Budget` DOUBLE NULL,
`Terms_Id` INT NOT NULL,
PRIMARY KEY (`Player_Id`),
INDEX `fk_player_terms_and_conditions1_idx` (`Terms_Id` ASC),
CONSTRAINT `fk_player_player_result1`
FOREIGN KEY (`Player_Id`)
REFERENCES `boat`.`player_result` (`Player_Id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_player_supervisor_player1`
FOREIGN KEY (`Player_Id`)
REFERENCES `boat`.`supervisor_player` (`Player_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_player_terms_and_conditions1`
FOREIGN KEY (`Terms_Id`)
REFERENCES `boat`.`terms_and_conditions` (`Terms_And_Conditions_Id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
Problem was in the Primary Key in the Player Table. It was configured to act like a foreign key for the other tables.
I am trying to create a table but I keep getting Error 1005. ): Please help!
Executing SQL script in server
ERROR: Error 1005: Can't create table 'czhen_hockey_db.hockey_db' (errno: 150)
SQL Code:
-- -----------------------------------------------------
-- Table `czhen_hockey_db`.`hockey_db`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `czhen_hockey_db`.`hockey_db` (
`id` INT NOT NULL,
`Date` VARCHAR(45) NOT NULL,
`Time` VARCHAR(45) NOT NULL,
`rink_id` INT NOT NULL,
`division/team_id` INT NOT NULL,
`opponent_id` INT NOT NULL,
INDEX `fk_hockey_db_rink_idx` (`rink_id` ASC),
INDEX `fk_hockey_db_division/team1_idx` (`division/team_id` ASC),
INDEX `fk_hockey_db_opponent1_idx` (`opponent_id` ASC),
PRIMARY KEY (`id`),
CONSTRAINT `fk_hockey_db_rink`
FOREIGN KEY (`rink_id`)
REFERENCES `czhen_hockey_db`.`rink` (`rink_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_hockey_db_division/team1`
FOREIGN KEY (`division/team_id`)
REFERENCES `czhen_hockey_db`.`division/team` (`division/team_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_hockey_db_opponent1`
FOREIGN KEY (`opponent_id`)
REFERENCES `czhen_hockey_db`.`opponent` (`opponent_name`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 8 succeeded, 1 failed
SQL 1005 is related to Foreign key problem. So please check whether "RINK", "OPPONENT" and "division/team" tables exist with specified primary keys.
I think for Opponent table you have to specify opponent_id as foreign key in place of opponent_name.
Please check
CREATE TABLE IF NOT EXISTS `czhen_hockey_db`.`hockey_db` (
`id` INT NOT NULL,
`Date` VARCHAR(45) NOT NULL,
`Time` VARCHAR(45) NOT NULL,
`rink_id` INT NOT NULL,
`division/team_id` INT NOT NULL,
`opponent_id` INT NOT NULL,
INDEX `fk_hockey_db_rink_idx` (`rink_id` ASC),
INDEX `fk_hockey_db_division/team1_idx` (`division/team_id` ASC),
INDEX `fk_hockey_db_opponent1_idx` (`opponent_id` ASC),
PRIMARY KEY (`id`),
CONSTRAINT `fk_hockey_db_rink`
FOREIGN KEY (`rink_id`)
REFERENCES `czhen_hockey_db`.`rink` (`rink_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_hockey_db_division/team1`
FOREIGN KEY (`division/team_id`)
REFERENCES `czhen_hockey_db`.`division/team` (`division/team_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_hockey_db_opponent1`
FOREIGN KEY (`opponent_id`)
REFERENCES `czhen_hockey_db`.`opponent` (`opponent_name`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB