Mysql Issue Related to foreign key and on delete cascade - mysql

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';

Related

How to drop Primary key thats needed in a foreign key constraint in MySQL?

I want to drop the primary key in father. So I set the foreign key in child to delete cascade. Why this doesnt work ? Do I need to delete the foreign key first ? If so, then what the propurse of the delete on cascade statement ?
create database table_test;
use table_test;
create table father(
cod int,
primary key (cod)
);
create table child(
cod int,
cod_father int,
primary key(cod),
constraint fk_cod_father foreign key (cod_father) references father(cod)
on delete cascade
);
alter table father
drop primary key;
ON DELETE CASCADE constraint is used in MySQL to delete the rows from
the child table automatically, when the rows from the parent table are
deleted
You want to remove primary key and in your case first you need to unlink the foreign key reference. So drop foreign key first.

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;

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;

alter table for having foreign key and primary key in one table

I'm trying to have a foreign key (parent_id) and a primary key (id) in an already existing table.
I already used SET FOREIGN_KEY_CHECKS=0; and then I ran this query below but every time I get the same error.
ALTER TABLE categories
ADD CONSTRAINT fk_parent_id FOREIGN KEY ( parent_id ) REFERENCES
categories(id) on DELETE CASCADE
Cannot add or update a child row: a foreign key constraint fails

Cannot add or update a child row: a foreign key constraint fails while adding another foreign key

I am trying to add a foreign key constraint in my table. My table structure are:
table requisition
My next table where i want to add foreign key of requisition table
requisition_approval
When i try to add a foreign key constraint with following sql query:
ALTER TABLE `requisition_approval` ADD CONSTRAINT `requisition_id` FOREIGN KEY (`requisition_id`) REFERENCES `requisition`(`id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
It gives following error:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`proprompt`.`#sql-34e8_3d7`, CONSTRAINT `requisition_id` FOREIGN KEY (`requisition_id`) REFERENCES `requisition` (`id`))
What is the problem here and how can i solve it?
Error is in the requisition_approval table more specificly in the requisition_id column. You inserted id 0 which does not references any id in requisition table. Change the value to 1 and this will work :)