MySql Composite Foreign Key Referncing same column - mysql

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

Related

errors adding foreign key constraints in MySQL Workbench

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?

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

FOREIGN KEY (`shiftid`, `groupid`, `sectionid`) REFERENCES `tbl_academic`(`id`, `id`, `id`) is not working in mysql database

ALTER TABLE `tbl_acc_payable` ADD CONSTRAINT `FK_APAY_SGS` FOREIGN KEY (`shiftid`, `groupid`, `sectionid`) REFERENCES `tbl_academic`(`id`, `id`, `id`) ON DELETE SET NULL ON UPDATE CASCADE
why I cannot apply multiple columns references another table multiple column in MySQL database?
return Error Message:
#1005 - Can't create table fastpay.tbl_acc_payable (errno: 150 "Foreign key constraint is incorrectly formed") (Details…)
A composite foreign key should reference a composite primary key. As you can't create a primary key on triple ID column, you should create 3 foreign keys, each of them referencing the same primary key (on the tbl_academic.id column).
FK references used the whole expression value or its prefix. The value - not the column name.
You need to reference to the values in 3 different rows which cannot be provided by composite index. So create 3 separate foreign keys referred to the same index tbl_academic(id):
ALTER TABLE tbl_acc_payable
ADD CONSTRAINT FK_APAY_SGS_1
FOREIGN KEY (shiftid)
REFERENCES tbl_academic(id)
ON DELETE SET NULL ON UPDATE CASCADE,
ADD CONSTRAINT FK_APAY_SGS_2
FOREIGN KEY (groupid)
REFERENCES tbl_academic(id)
ON DELETE SET NULL ON UPDATE CASCADE,
ADD CONSTRAINT FK_APAY_SGS_3
FOREIGN KEY (sectionid)
REFERENCES tbl_academic(id)
ON DELETE SET NULL ON UPDATE CASCADE;

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

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.