how to make a one to one relationship in phpmyadmin? - mysql

I have two tables "donor" and " location", every donor has one location in a time.
how to make the keys for this relation?
I tried to make a foreign key in location table to the donor but it gives me this message:
Error
SQL query:
ALTER TABLE `location` ADD CONSTRAINT `location_donor` FOREIGN KEY (`donor_id`) REFERENCES `blood_donation`.`donor`(`id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
MySQL said: Documentation
#1452 - Cannot add or update a child row: a foreign key constraint fails (`blood_donation`.`#sql-23f8_2e`, CONSTRAINT `location_donor` FOREIGN KEY (`donor_id`) REFERENCES `donor` (`id`))

If you want to execute ALTER TABLE statment you should truncate the table in the first place.
Because Mysql can not add the constraint to the existing rows according to the error log :
#1452 - Cannot add or update a child row: a foreign key constraint fails

Related

Unable to add foreign key between two tables tables on fields signup (uid) and login (uid)

I can't add foreign key between the tables on fields signup and login because of getting error:
#1452 - Cannot add or update a child row:
a foreign key constraint fails
(`cems`.`#sql-109c_1ab`, CONSTRAINT `#sql-109c_1ab_ibfk_1` FOREIGN KEY (`uid`) REFERENCES `login` (`uid`) ON DELETE CASCADE ON UPDATE CASCADE)
The error comes when you are trying to add a row for which no matching row in in the other table.
The FOREIGN KEY clause is specified in the child table.
INSERT or UPDATE operation that attempts to create a foreign key value in a child table if there is no a matching candidate key value in the parent table it will reject

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

SQL FK constraint fails

I have 2 tables, Office and User. I want to make relation OneToMany (1 office has many users).
But when i run this sql
ALTER TABLE izo_user ADD CONSTRAINT FK_DA8075CFFA0C224 FOREIGN KEY (office_id) REFERENCES izo_office (id)
CREATE INDEX IDX_DA8075CFFA0C224 ON izo_user (office_id)
something goes wrong and i get error
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`izoplast`.`#sql-9842_1be9`, CONSTRAINT `FK_DA8075CFFA0C224` FOREIGN KEY (`office_id`) REFERENCES `izo_office` (`id`))
My Tables: http://oi57.tinypic.com/whhezr.jpg
try it if it works for you-
ALTER TABLE izo_user add index idx_office_id(office_id);
ALTER TABLE izo_user ADD CONSTRAINT FK_DA8075CFFA0C224 FOREIGN KEY (office_id) REFERENCES izo_office (id);

error code #1452 when adding foreign keys

Error
SQL query:
ALTER TABLE `bids` ADD FOREIGN KEY (`buyerID`) REFERENCES `e_trading_post`.`buyer`(`buyerID`)
ON DELETE CASCADE ON UPDATE CASCADE;
MySQL said:
1452 - Cannot add or update a child row: a foreign key constraint fails ('e_trading_post' .'#sql-15d48_6a8', CONSTRAINT `#sql-15d48_6a8_ibfk_1' FOREIGN KEY ('buyerID') REFERENCES 'buyer; ('buyerID') ON DELETE CASCADE ON UPDATE CASCADE)
This error means that this specific foreign key constraint cannot be created as it would be violated (is is violated right now).
The following query might help you to find the violating row:
select buyerID from bids where buyerID not in (select buyer from e_trading_post)

error adding a foreign key constraint

I have the following query:
ALTER TABLE ROUTE ADD FOREIGN KEY (RID) REFERENCES RESERVATION(RID) ON DELETE CASCADE
but it generates me an error:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`SmarTrek`.`#sql-91e_d09`, CONSTRAINT `FK_RID` FOREIGN KEY (`RID`) REFERENCES `RESERVATION` (`RID`) ON DELETE CASCADE)
In designer mode, here's what it looks like:
That would meant that you already have data in the ROUTE table that does not satisfy the foreign key constraint.
To find the offending records, so you can update them to some other value (that exists), you can use
select *
from route
where rid not in (select rid from reservation)
THERE may b 2 reasons
there may b some row in ROUTE TABLE which have RID that does not exist in RESERVATION(RID)
or check the DATATYPE OF ROUTE (RID) & RESERVATION(RID) both should be same ( unsigned/signed)