MySQL: Foreign key constraint is incorrectly formed - mysql

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`)

Related

I want to add an foreign key but everytime i try adding one i get error codes

Coding that i used to altar my table and change it.
ALTER TABLE `the-challenge`.`klant`
ADD CONSTRAINT `resultaat`
FOREIGN KEY (`resultaatid`)
REFERENCES `the-challenge`.`resultaat` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
The results when trying to apply the code:
Operation failed: There was an error while applying the SQL script to the database.
Executing:
ALTER TABLE `the-challenge`.`klant`
ADD CONSTRAINT `resultaat`
FOREIGN KEY (`resultaatid`)
REFERENCES `the-challenge`.`resultaat` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
ERROR 1826: Duplicate foreign key constraint name 'resultaat'
SQL Statement:
ALTER TABLE `the-challenge`.`klant`
ADD CONSTRAINT `resultaat`
FOREIGN KEY (`resultaatid`)
REFERENCES `the-challenge`.`resultaat` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION

How to refer primary key as Foreign to various table

I have a table called User Which contain user_id as primary key
I have other tables like Doctor ,patient,staff ...etc which contain column called created by which reffer to user_id of user table.
Here is my issue I cannot use same constrain name across different table for example
ALTER TABLE `emr#default`.`staff`
ADD CONSTRAINT `FK_created_by`
FOREIGN KEY (`created_by`)
REFERENCES `emr#default`.`users` (`user_id`)
ON DELETE CASCADE;
is created successfully
when I try with other table
ALTER TABLE `emr#default`.patient
ADD CONSTRAINT `FK_created_by`
FOREIGN KEY (`created_by`)
REFERENCES `emr#default`.`users` (`user_id`)
ON DELETE CASCADE;
generating error Error Code: 1022. Can't write; duplicate key in table '#sql-1_7a64'
if I try
ALTER TABLE `emr#default`.patient
ADD CONSTRAINT `FK_created_by1`
FOREIGN KEY (`created_by`)
REFERENCES `emr#default`.`users` (`user_id`)
ON DELETE CASCADE;
is creating fk
my Doubt is can we use same constrain name.if not can I create different constrain name that will not effect anythin
Constraint names can have conflicts between tables. That is why I include both the table and column names in the constraint name:
ALTER TABLE `emr#default`.patient
ADD CONSTRAINT `FK_patient_created_by`
FOREIGN KEY (`created_by`)
REFERENCES `emr#default`.`users` (`user_id`)
ON DELETE CASCADE;
ALTER TABLE `emr#default`.`staff`
ADD CONSTRAINT `FK_staff_created_by`
FOREIGN KEY (`created_by`)
REFERENCES `emr#default`.`users` (`user_id`)
ON DELETE CASCADE;

Foreign key constraint fails in phpmyadmin while creating foreign key

I am creating foreign key but continuously getting error
1452 - Cannot add or update a child row: a foreign key constraint fails (demo_db.#sql-271c_ac, CONSTRAINT company_state_id FOREIGN
KEY (company_state_id) REFERENCES company (Id))
SQL query is
ALTER TABLE `state` ADD CONSTRAINT `company_state_id` FOREIGN KEY (`company_state_id`) REFERENCES `company`(`Id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
Simply do this:
SET FOREIGN_KEY_CHECKS=0;
ALTER TABLE `state` ADD CONSTRAINT `company_state_id` FOREIGN KEY (`company_state_id`) REFERENCES `company`(`Id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
SET FOREIGN_KEY_CHECKS=1;
This will first ignore all foreign key checks, alter your table and again restore the check to 1
Hope this helps :)

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