errors adding foreign key constraints in MySQL Workbench - mysql

Executing:
ALTER TABLE `sales`.`sales`
ADD INDEX `constraint_1_idx` (`customer_id` ASC) VISIBLE;
ALTER TABLE `sales`.`sales`
ADD CONSTRAINT `constraint_1`
FOREIGN KEY (`customer_id`)
REFERENCES `sales`.`customers` (`customer_id`)
ON DELETE CASCADE
ON UPDATE NO ACTION;
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1822: Failed to add the foreign key constraint. Missing index for constraint 'constraint_1' in the referenced table 'customers'
SQL Statement:
ALTER TABLE `sales`.`sales`
ADD CONSTRAINT `constraint_1`
FOREIGN KEY (`customer_id`)
REFERENCES `sales`.`customers` (`customer_id`)
ON DELETE CASCADE ON UPDATE NO ACTION`
This code was written by me in Workbench:
drop table sales;
create table sales
(
purchase_number INT auto_increment,
date_of_purchase date,
customer_id int,
item_code varchar(10),
primary key (purchase_number)
);
alter table sales
add foreign key(customer_id)
references customers(customer_id) on delete cascade;
Error on the last line:
Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'sales_ibfk_1' in the referenced table 'customers'
Where have I gone wrong?

Related

MySQL Workbench. ERROR 1452: Cannot add or update a child row: a foreign key constraint fails. Operation failed

I can not bind a subordinate (Table administrator) and the main table (the department) by a foreign key.
It displays the following message:
Executing:
ALTER TABLE `grocery_supermarket_manager`.`administrator`
ADD CONSTRAINT `AdministratorDepartment_FK`
FOREIGN KEY (`id_department`)
REFERENCES `grocery_supermarket_manager`.`department` (`id_department`)
ON DELETE CASCADE
ON UPDATE CASCADE;
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1452: Cannot add or update a child row: a foreign key constraint fails (`grocery_supermarket_manager`.`#sql-9ac_8`, CONSTRAINT `AdministratorDepartment_FK` FOREIGN KEY (`id_department`) REFERENCES `department` (`id_department`) ON DELETE CASCADE ON UPDATE CASCADE)
SQL Statement:
ALTER TABLE `grocery_supermarket_manager`.`administrator`
ADD CONSTRAINT `AdministratorDepartment_FK`
FOREIGN KEY (`id_department`)
REFERENCES `grocery_supermarket_manager`.`department` (`id_department`)
ON DELETE CASCADE
ON UPDATE CASCADE
Column name: "id_department" table "Department" have:
Datatype - INT(10)
Storage: Primary Key, Not Null, Unique, Unsigned, Auto increment.
Column name: "id_department" table "Administrator" have:
Datatype - INT(10)
Storage: Not Null, Unsigned.
Make sure the current data in your table fullfills the constraint you are adding. Looks like you have administrators with invalid department ids.

MySQL: Foreign key constraint is incorrectly formed

I am using a MySQL database, have the following 3 tables:
map_range
map_location (join table)
location
I am trying to add some foreign keys.
ALTER TABLE `www`.`map_location`
ALTER TABLE `www`.`map_location`
ADD INDEX `fk_map_location_indx` (`LOC_ID` ASC);
ALTER TABLE `www`.`map_location`
ADD CONSTRAINT `fk_map_loc_map`
FOREIGN KEY (`MAP_ID`)
REFERENCES `www`.`map_location` (`MAP_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
ADD CONSTRAINT `fk_map_loc_location`
FOREIGN KEY (`LOC_ID`)
REFERENCES `www`.`location` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
But I get the following error:
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1025: Error on rename of '.\www\#sql-ea0_2b8' to '.\www\map_location' (errno: 150 - Foreign key constraint is incorrectly formed)
SQL Statement:
ALTER TABLE `www`.`map_location`
ADD CONSTRAINT `fk_map_loc_map`
FOREIGN KEY (`MAP_ID`)
REFERENCES `www`.`map_location` (`MAP_ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
ADD CONSTRAINT `fk_map_loc_location`
FOREIGN KEY (`LOC_ID`)
REFERENCES `www`.`location` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
Any ideas appreciated.
You are self-refferencing the same table and colum.
did you mean to location and not map_location?
ALTER TABLE `www`.`map_location`
ADD CONSTRAINT... FOREIGN KEY (`MAP_ID`)
REFERENCES `www`.`map_location` (`MAP_ID`)

Mysql Issue Related to foreign key and on delete cascade

I have 4 sql tables.
create table general(regno int NOT NULL primary key);
create table company_information(cregno int NOT NULL primary key);
create table company_jobs (jcode int NOT NULL primary key, cregno int , foreign key(cregno) references company_information(cregno));
create table applied(cregno int ,jcode int, regno int, foreign key(regno) references general(regno), foreign key(jcode) references company_jobs(jcode));
All i need to do is delete from table company_jobs when table applied has some value. Actually, all the tables must have some value for table applied to have some value as you can see from the structure of tables.
I used these commands to add ON DELETE CASCADE Constraint:
alter table company_jobs add constraint fk_cregno13 foreign key(cregno) references company_information (cregno) on delete cascade;
alter table applied add constraint fk_jcode16 foreign key(jcode) references company_jobs(jcode) on delete cascade;
alter table applied add constraint fk_regno14 foreign key(regno) references general(regno) on delete cascade;
But it is not working unfortunately and i am getting this error when i am giving the following command.
mysql> delete from company_jobs;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign
key constrai nt fails (test.applied, CONSTRAINT applied_ibfk_2
FOREIGN KEY (jcode) RE FERENCES company_jobs (jcode))
Help me out please if anyone can. Thank you
When you created the applied table you created 2 foreign keys.
After that, you have added 2 other foreign keys to this table.
As you can see, the error references a foreign key named applied_ibfk_2 that's not the foreign key you added after the creation.
Thus, ad the moment you have 4 foreign key constraint on that table.
So, you have to drop the 2 foreign keys created on table creation (that have a predefined name) and everything will work
The 1st foreign key pointing from table applied to company_job doesn't have any cascade rule, so it simple blocks the delete from company_job;
See mysql dump bellow
ALTER TABLE `applied`
ADD CONSTRAINT `applied_ibfk_1` FOREIGN KEY (`regno`) REFERENCES `general` (`regno`),
ADD CONSTRAINT `applied_ibfk_2` FOREIGN KEY (`jcode`) REFERENCES `company_jobs` (`jcode`),
ADD CONSTRAINT `fk_jcode16` FOREIGN KEY (`jcode`) REFERENCES `company_jobs` (`jcode`) ON DELETE CASCADE,
ADD CONSTRAINT `fk_regno14` FOREIGN KEY (`regno`) REFERENCES `general` (`regno`) ON DELETE CASCADE;
You need to drop the foreign key first, or recreate the table without the 1st foreign key
ALTER TABLE 'applied' DROP CONTRAINT 'applied_ibfk_2';

Error 1215: cannot add foreign key constraint in my SQL

I have the parent table which is gym_member, and I have the child which is medical_history.
In gym_member table there are two primary key which are gm_id and student_id.
In medical history table there are one primary which is mh_id, and I want to add a foreign key which is student_id but it show me this error.
ALTER TABLE `hct_gym`.`medical_history`
ADD CONSTRAINT `student_id`
FOREIGN KEY (`student_id`)
REFERENCES `hct_gym`.`gym_member` (`student_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1215: Cannot add foreign key constraint
SQL Statement:
ALTER TABLE `hct_gym`.`medical_history`
ADD CONSTRAINT `student_id`
FOREIGN KEY (`student_id`)
REFERENCES `hct_gym`.`gym_member` (`student_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
I want to know where is the problem?
Check if column medical_history.student_id contains values
which are not contained in gym_member.student_id
That would contradict the constraint before /while it is being created.

MySql Composite Foreign Key Referncing same column

I am attempting to create a composite foreign key in MySQL however both fields are referencing the same column in another table. I am not sure if this is the accurate approach since the sql is not executing. Under is the SQL statement
SQL
ALTER TABLE tableA ADD CONSTRAINT `comp_fk`
FOREIGN KEY (`a_id` , `b_id` )
REFERENCES `tabelB` (`p_id` , `p_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION;
Error
MySQL Database Error: Can't create table 'sep.#sql-984_8' (errno: 150)
Try this by individually applying constraint
ALTER TABLE `comp_fk`
ADD CONSTRAINT `test` FOREIGN KEY (`a_id`) REFERENCES `tabelB`(`id`) ON UPDATE NO ACTION ON DELETE NO ACTION,
ADD CONSTRAINT `test2` FOREIGN KEY (`b_id`) REFERENCES `tabelB`(`id`) ON UPDATE NO ACTION ON DELETE NO ACTION