Foreign Key Constraints - mysql

ERROR 1452: 1452: Cannot add or update a child row: a foreign key constraint fails (`harris`.`MANAGER`, CONSTRAINT `fk_EMPLOYEE_has_PROJECT_PROJECT1` FOREIGN KEY (`PROJECT_project_ID`) REFERENCES `PROJECT` (`project_ID`) ON DELETE NO ACTION ON UPDATE NO ACTION)
SQL Statement:
INSERT INTO `harris`.`MANAGER` (`EMPLOYEE_employee_ID`, `PROJECT_project_ID`) VALUES ('EMPLOYEE_employee_ID', 'PROJECT_project_ID')
No matter what foreign key I use it won't work. For some reason it adds a "1" to the end IE: PROJECT_PROJECT when it should just be project_id and employye_num

Related

ERROR for INSERT #1452 - Cannot add or update a child row: a foreign key constraint fails

I always get an error if I want to insert value in my tables..
SQL:
INSERT INTO `login`( `lo_password`, `lo_userName`, `lo_eMail`) VALUES ("sdsdf!D","Test!s1","test#test.com")
Error:
1452 - Cannot add or update a child row: a foreign key constraint fails (splitthebilldb.login, CONSTRAINT login_ibfk_1 FOREIGN KEY (lo_id) REFERENCES users (lo_id_login))
Your constraint is not in the right order :
CONSTRAINT login_ibfk_1 FOREIGN KEY (lo_id) REFERENCES users (lo_id_login))
should be :
CONSTRAINT login_ibfk_1 FOREIGN KEY (lo_id_login) REFERENCES users (lo_id))
And you've to add this constraint into USERS table.

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

After applying DELETE and UPDATE cascade not able to DELETE/UPDATE from parent table

I have applied both DELETE CASCADE and UPDATE CASCADE constraints on child table.
alter table table_product_categories add constraint fk_product_id1 foreign key (product_id) references table_products (product_id) on delete cascade;
alter table table_product_categories add constraint fk_product_id2 foreign key (product_id) references table_products (product_id) on update cascade;
Now when trying to delete in parent table:
DELETE FROM table_products WHERE `table_products`.`product_id` = 1819
Getting this error:
MySQL said: Documentation
1451 - Cannot delete or update a parent row: a foreign key constraint fails (`table_product_categories`, CONSTRAINT `fk_product_id1` FOREIGN KEY (`product_id`) REFERENCES `table_products` (`product_id`) ON UPDATE CASCADE)
When trying to update parent table
UPDATE `wokoshop`.`table_products` SET `product_id` = '1' WHERE `table_products`.`product_id` =1819
Getting this error:
1452 - Cannot add or update a child row: a foreign key constraint fails (`wokoshop`.`table_product_categories`, CONSTRAINT `fk_product_id2` FOREIGN KEY (`product_id`) REFERENCES `table_products` (`product_id`) ON DELETE CASCADE)
What is the reason for errors and how to solve it?
Instead of creating two constraints, use a single constraint with both CASCADE options:
alter table table_product_categories
add constraint fk_product_id1 foreign key (product_id)
references table_products (product_id)
on delete cascade on update cascade;

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)

Always getting error with foreign key

Whenever I set a foreign key, I get the below error, and I'm not sure why. Can someone help me figure it out?
Error
SQL query: ALTER TABLE `vl_discount` ADD CONSTRAINT `vl_product_ibfk1` FOREIGN KEY ( `discountedProduct` )
REFERENCES `base`.`vl_product` (`autokey`) ON DELETE CASCADE ON UPDATE CASCADE ;
MySQL said: Documentation
#1452 - Cannot add or update a child row: a foreign key constraint fails
(`base`.`#sql41d_172`, CONSTRAINT `vl_product_ibfk1` FOREIGN KEY (`discountedProduct`)
REFERENCES `vl_product` (`autokey`) ON DELETE CASCADE ON UPDATE CASCADE)