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;
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 a table called reservation which stores the reservation details. When I try to create the table it gives me the error;
Foreign key constraint is incorrectly formed
This is my query;
CREATE TABLE IF NOT EXISTS `smartbusarrival_sbaDB`.`reservation` (
`res_id` INT NOT NULL,
`user_id` INT NOT NULL,
`bus_id` INT NOT NULL,
`trip_id` INT NOT NULL,
`date` DATE NOT NULL,
`start` INT NOT NULL,
`end` INT NOT NULL,
PRIMARY KEY (`res_id`),
INDEX `fk_user_id` (`user_id` ASC),
INDEX `fk_bus_id` (`bus_id` ASC),
INDEX `fk_trip_id` (`trip_id` ASC),
INDEX `fk_start` (`start` ASC),
INDEX `fk_end` (`end` ASC),
CONSTRAINT `fk_reservation_user`
FOREIGN KEY (`user_id`)
REFERENCES `smartbusarrival_sbaDB`.`users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_reservation_bustrip`
FOREIGN KEY (`bus_id`,`trip_id`)
REFERENCES `smartbusarrival_sbaDB`.`bustrip` (`bus_id`,`trip_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_reservation_start`
FOREIGN KEY (`start`)
REFERENCES `smartbusarrival_sbaDB`.`trip_halt` (`tag`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_reservation_end`
FOREIGN KEY (`end`)
REFERENCES `smartbusarrival_sbaDB`.`trip_halt` (`tag`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
user_id is is a foreign from users table(primary key)
bus_id + trip_id is a composite key from bustrip table (primary key)
start and end are foreign key from trip_halt table.
I looked into similar questions but nothing solve my problem. Can anyone suggest something to solve this.
Thank You!!!
the field you are referencing as a foreign key should be a unique. I think 'tag' field is not a unique one.
Every time I'm trying to insert a foreign key to the table I got that message:
Error Code: 1215. Cannot add foreign key constraint 0.281 sec
My create table code:
CREATE TABLE `test`.`buy`(
`id` INT NOT NULL AUTO_INCREMENT,
`id_customer` INT UNSIGNED NOT NULL,
`code` VARCHAR(45) NOT NULL,
PRIMARY KEY(`id`),
UNIQUE INDEX `code_UNIQUE`(`code`),
CONSTRAINT `id_customer` FOREIGN KEY(`id_customer`) REFERENCES `test`.`customer`(`id_customer`) ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT `code` FOREIGN KEY(`code`) REFERENCES `test`.`product`(`code`) ON DELETE RESTRICT ON UPDATE CASCADE
)
What should I do ?
Please format your code next time so it's easier to read.
You don't need the CONSTRAINT xxx bit, try this instead:
CREATE TABLE test.buy (
id INT NOT NULL AUTO_INCREMENT,
id_customer INT UNSIGNED NOT NULL,
code VARCHAR(45) NOT NULL,
PRIMARY KEY (id),
UNIQUE INDEX code_UNIQUE (code),
FOREIGN KEY (id_customer)
REFERENCES test.customer (id_customer)
ON DELETE RESTRICT ON UPDATE CASCADE,
FOREIGN KEY (code)
REFERENCES test.product (code)
ON DELETE RESTRICT ON UPDATE CASCADE
);
If that still doesn't work then make sure the other tables you reference (test.customer and test.product) already exist and that they have matching fields of the same data type.
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.
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